dk-number-input.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. /*******************************************************************************
  2. * Copyright(c) 2022 dongke All rights reserved. / Confidential
  3. * 类的信息:
  4. * 1.程序名称:数字组件(支持千分位,金额符)
  5. * 2.功能描述:dk-number-input组件
  6. * 编辑履历:
  7. * 作者 日期 版本 修改内容
  8. * 周兴 2022-6-30 1.00 新建
  9. *******************************************************************************/
  10. const common = require('../../../utils/common.js')
  11. Component({
  12. options: {
  13. addGlobalClass: true,
  14. },
  15. properties: {
  16. titleValue: {
  17. type: String,
  18. value: ''
  19. },
  20. required: Boolean,//是否显示表单必填星号
  21. requiredLeft:{
  22. type:String,
  23. value:'50rpx'
  24. },
  25. titleWidth: {
  26. type: String,
  27. value: '164rpx'
  28. },
  29. titleColor: {
  30. type: String,
  31. value: '#1B365D'
  32. },
  33. titleFontWeight: {
  34. type: String,
  35. value: 'normal'
  36. },
  37. inputColor: {
  38. type: String,
  39. value: '#1B365D'
  40. },
  41. readonly: {
  42. type: Boolean,
  43. value: false
  44. },
  45. // 内容是否居右,默认是false
  46. contentRight:{
  47. type: Boolean,
  48. value: false
  49. },
  50. /**
  51. * 输入的数据
  52. */
  53. inputValue: {
  54. type: Float32Array,
  55. value: 0,
  56. },
  57. /**
  58. * 是否支持千分位
  59. */
  60. formatThousandth: {
  61. type: Boolean,
  62. value: true
  63. },
  64. /**
  65. * 是否可用
  66. */
  67. disabled: {
  68. type: Boolean,
  69. value: false
  70. },
  71. /**
  72. * 金额符
  73. */
  74. sign: {
  75. type: String,
  76. value: '¥'
  77. },
  78. percent: {
  79. type: String,
  80. value: '%'
  81. },
  82. percentSignFlag: {
  83. type: Boolean,
  84. value: false
  85. },
  86. /**
  87. * 标识的大小
  88. */
  89. signSize: {
  90. type: String,
  91. value: ''
  92. },
  93. /**
  94. * 是否居中
  95. */
  96. center: {
  97. type: String,
  98. value: 'center'
  99. },
  100. /**
  101. * 小数位数
  102. */
  103. digits: {
  104. type: Number,
  105. value: 2
  106. },
  107. /**
  108. * 最大值
  109. */
  110. max: {
  111. type: Number,
  112. value: 10000000
  113. },
  114. /**
  115. * 字体大小
  116. */
  117. fontSize: {
  118. type: Number,
  119. value: 13
  120. },
  121. /**
  122. * 字体大小
  123. */
  124. fontWeight: {
  125. type: String,
  126. value: 'nomal'
  127. },
  128. /**
  129. * 是否是负数处理(输入整数,自动变为负数)
  130. */
  131. negative: {
  132. type: Boolean,
  133. value: false
  134. },
  135. /**
  136. * 提示信息
  137. */
  138. placeholder: {
  139. type: String,
  140. value: ''
  141. },
  142. maxFlag: {
  143. type: Boolean,
  144. value: true
  145. },
  146. minFlag: {
  147. type: Boolean,
  148. value: true
  149. },
  150. dataIndex: {
  151. type: Number,
  152. },
  153. dataIndex2: {
  154. type: Number,
  155. },
  156. left: {
  157. type: String,
  158. value: '1rpx'
  159. },
  160. /**
  161. * 错误提示信息
  162. */
  163. errorMessage: {
  164. type: String,
  165. },
  166. /**
  167. * 提示信息
  168. */
  169. tip:{
  170. type:String,
  171. }
  172. },
  173. data: {
  174. focus: false,
  175. displayValue: '',
  176. clearFlag: false,
  177. },
  178. /**
  179. * 组件的方法列表
  180. */
  181. methods: {
  182. /**
  183. * @desc : 显示tip
  184. * @author : 周兴
  185. * @date : 2024/4/25 11:46
  186. */
  187. showTip(){
  188. let tip = this.data.tip
  189. if(tip){
  190. wx.showToast({
  191. title: tip,
  192. icon: 'none',
  193. duration: 2000
  194. })
  195. }
  196. },
  197. /**
  198. * @desc : 光标进入选中
  199. * @author : 周兴
  200. * @date : 2022/6/30 16:16
  201. */
  202. handleInput(e) {
  203. if (this.data.readonly) {
  204. return
  205. }
  206. setTimeout(() => {
  207. this.setData({
  208. inputValue: parseFloat(this.data.inputValue),
  209. displayValue: this.data.inputValue,
  210. focus: true
  211. })
  212. }, 30)
  213. },
  214. /**
  215. * @desc : 光标进入输入框
  216. * @author : 周兴
  217. * @date : 2022/6/30 16:16
  218. */
  219. bindFocus(e){
  220. if(isNaN(this.data.inputValue)){
  221. this.setData({
  222. inputValue:0
  223. })
  224. }
  225. },
  226. /**
  227. * @desc : 输入时数据的处理
  228. * @author : 周兴
  229. * @date : 2022/6/30 16:16
  230. */
  231. bindInput(e) {
  232. let flag = true;
  233. let values = e.detail.split('.')
  234. // 如果输入的是小数点,那么需要判断是否已经有小数点
  235. if (e.detail.endsWith('.')) {
  236. // 说明有多个小数点
  237. // console.log('zzz',values,this.data.inputValue,(this.data.inputValue + '').indexOf('.'));
  238. if (values && values.length > 2) {
  239. if ((this.data.inputValue + '').indexOf('.') >= 0) {
  240. return this.data.inputValue;
  241. } else {
  242. // 可能连续输入2个小数点
  243. return this.data.inputValue + '.';
  244. }
  245. }
  246. // 如果inputValue是空,那么当成0处理
  247. if (e.detail == '.') {
  248. return '0.';
  249. }
  250. flag = false;
  251. }
  252. //控制小数点后数字的输入
  253. let digits = this.data.digits
  254. if (values && values.length === 2 && values[1].length > digits) {
  255. flag = false;
  256. return this.data.inputValue;
  257. }
  258. if (flag) {
  259. let value = 0;
  260. // 判断是否超过最大值
  261. if (this.data.maxFlag) {
  262. if (e.detail > this.data.max) {
  263. value = this.data.max
  264. } else {
  265. value = e.detail
  266. }
  267. } else {
  268. value = e.detail
  269. }
  270. if (this.data.minFlag) {
  271. if (e.detail <= this.data.min) {
  272. value = this.data.min
  273. } else {
  274. value = value
  275. }
  276. } else {
  277. value = value
  278. }
  279. this.setData({
  280. clearFlag: false,
  281. inputValue: value,
  282. })
  283. this.handleData()
  284. }
  285. },
  286. /**
  287. * @desc : 光标离开处理数据的显示
  288. * @author : 周兴
  289. * @date : 2022/6/30 16:16
  290. */
  291. bindBlur() {
  292. if (this.data.clearFlag) {
  293. this.setData({
  294. inputValue: 0,
  295. displayValue: 0,
  296. clearFlag: false
  297. })
  298. }
  299. // 处理数据
  300. if (this.data.inputValue) {
  301. this.setData({
  302. inputValue: Number(this.data.inputValue)
  303. })
  304. }
  305. // 离开的时候进行数据处理
  306. // this.handleDisplayValue();
  307. this.triggerEvent('triggerBindBlur', {
  308. inputValue: this.data.inputValue,
  309. index: this.data.dataIndex,
  310. index2: this.data.dataIndex2,
  311. })
  312. this.setData({
  313. focus: false
  314. })
  315. },
  316. /**
  317. * @desc : 绑定值
  318. * @author : 周兴
  319. * @date : 2022/6/30 16:16
  320. */
  321. handleData() {
  322. let value = this.data.inputValue;
  323. if (value == null || value == '') {
  324. this.setData({
  325. clearFlag: true
  326. })
  327. value = 0
  328. }
  329. if (isNaN(value)) {
  330. value = 0
  331. }
  332. // 如果负数标识开启
  333. if (this.data.negative) {
  334. if (value > 0) {
  335. value = -1 * value;
  336. }
  337. } else {
  338. if (value < 0) {
  339. value = -1 * value;
  340. }
  341. }
  342. this.triggerEvent('triggerBindValue', {
  343. value: value
  344. })
  345. },
  346. /**
  347. * @desc : 处理数据的显示
  348. * @author : 周兴
  349. * @date : 2022/6/30 16:16
  350. */
  351. handleDisplayValue() {
  352. let inputValue = this.data.inputValue;
  353. let displayValue = ''
  354. // if(inputValue != undefined && inputValue != 0){
  355. //添加判断条件,当用户输入空数据问题会出现NaN 于继渤2022/06/30
  356. if (inputValue != undefined && inputValue && inputValue != 0) {
  357. // 判断是否有千分位
  358. if (this.data.formatThousandth) {
  359. displayValue = common.toThousandCents(inputValue);
  360. }
  361. // 如果是负数显示
  362. if (this.data.negative && displayValue.indexOf("-") < 0) {
  363. // displayValue = this.data.sign + "-" + displayValue;
  364. displayValue = "-" + displayValue;
  365. } else {
  366. // 显示金额符号
  367. // displayValue = this.data.sign + displayValue;
  368. }
  369. } else {
  370. displayValue = inputValue;
  371. }
  372. this.setData({
  373. displayValue: displayValue
  374. })
  375. },
  376. },
  377. /**
  378. * 组件生命周期
  379. */
  380. lifetimes: {
  381. attached: function () {
  382. //处理数据的显示
  383. this.handleDisplayValue();
  384. },
  385. detached: function () {
  386. // 在组件实例被从页面节点树移除时执行
  387. },
  388. },
  389. })