dk-number-input.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  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. dataIndex: {
  147. type: Number,
  148. },
  149. dataIndex2: {
  150. type: Number,
  151. },
  152. left: {
  153. type: String,
  154. value: '1rpx'
  155. },
  156. /**
  157. * 错误提示信息
  158. */
  159. errorMessage: {
  160. type: String,
  161. },
  162. /**
  163. * 提示信息
  164. */
  165. tip:{
  166. type:String,
  167. }
  168. },
  169. data: {
  170. focus: false,
  171. displayValue: '',
  172. clearFlag: false,
  173. },
  174. /**
  175. * 组件的方法列表
  176. */
  177. methods: {
  178. /**
  179. * @desc : 显示tip
  180. * @author : 周兴
  181. * @date : 2024/4/25 11:46
  182. */
  183. showTip(){
  184. let tip = this.data.tip
  185. if(tip){
  186. wx.showToast({
  187. title: tip,
  188. icon: 'none',
  189. duration: 2000
  190. })
  191. }
  192. },
  193. /**
  194. * @desc : 光标进入选中
  195. * @author : 周兴
  196. * @date : 2022/6/30 16:16
  197. */
  198. handleInput(e) {
  199. if (this.data.readonly) {
  200. return
  201. }
  202. setTimeout(() => {
  203. this.setData({
  204. inputValue: parseFloat(this.data.inputValue),
  205. displayValue: this.data.inputValue,
  206. focus: true
  207. })
  208. }, 30)
  209. },
  210. /**
  211. * @desc : 光标进入输入框
  212. * @author : 周兴
  213. * @date : 2022/6/30 16:16
  214. */
  215. bindFocus(e){
  216. if(isNaN(this.data.inputValue)){
  217. this.setData({
  218. inputValue:0
  219. })
  220. }
  221. },
  222. /**
  223. * @desc : 输入时数据的处理
  224. * @author : 周兴
  225. * @date : 2022/6/30 16:16
  226. */
  227. bindInput(e) {
  228. let flag = true;
  229. let values = e.detail.split('.')
  230. // 如果输入的是小数点,那么需要判断是否已经有小数点
  231. if (e.detail.endsWith('.')) {
  232. // 说明有多个小数点
  233. // console.log('zzz',values,this.data.inputValue,(this.data.inputValue + '').indexOf('.'));
  234. if (values && values.length > 2) {
  235. if ((this.data.inputValue + '').indexOf('.') >= 0) {
  236. return this.data.inputValue;
  237. } else {
  238. // 可能连续输入2个小数点
  239. return this.data.inputValue + '.';
  240. }
  241. }
  242. // 如果inputValue是空,那么当成0处理
  243. if (e.detail == '.') {
  244. return '0.';
  245. }
  246. flag = false;
  247. }
  248. //控制小数点后数字的输入
  249. let digits = this.data.digits
  250. if (values && values.length === 2 && values[1].length > digits) {
  251. flag = false;
  252. return this.data.inputValue;
  253. }
  254. if (flag) {
  255. let value = 0;
  256. // 判断是否超过最大值
  257. if (this.data.maxFlag) {
  258. if (e.detail > this.data.max) {
  259. value = this.data.max
  260. } else {
  261. value = e.detail
  262. }
  263. } else {
  264. value = e.detail
  265. }
  266. this.setData({
  267. clearFlag: false,
  268. inputValue: value,
  269. })
  270. this.handleData()
  271. }
  272. },
  273. /**
  274. * @desc : 光标离开处理数据的显示
  275. * @author : 周兴
  276. * @date : 2022/6/30 16:16
  277. */
  278. bindBlur() {
  279. if (this.data.clearFlag) {
  280. this.setData({
  281. inputValue: 0,
  282. displayValue: 0,
  283. clearFlag: false
  284. })
  285. }
  286. // 处理数据
  287. if (this.data.inputValue) {
  288. this.setData({
  289. inputValue: Number(this.data.inputValue)
  290. })
  291. }
  292. // 离开的时候进行数据处理
  293. // this.handleDisplayValue();
  294. this.triggerEvent('triggerBindBlur', {
  295. inputValue: this.data.inputValue,
  296. index: this.data.dataIndex,
  297. index2: this.data.dataIndex2,
  298. })
  299. this.setData({
  300. focus: false
  301. })
  302. },
  303. /**
  304. * @desc : 绑定值
  305. * @author : 周兴
  306. * @date : 2022/6/30 16:16
  307. */
  308. handleData() {
  309. let value = this.data.inputValue;
  310. if (value == null || value == '') {
  311. this.setData({
  312. clearFlag: true
  313. })
  314. value = 0
  315. }
  316. if (isNaN(value)) {
  317. value = 0
  318. }
  319. // 如果负数标识开启
  320. if (this.data.negative) {
  321. if (value > 0) {
  322. value = -1 * value;
  323. }
  324. } else {
  325. if (value < 0) {
  326. value = -1 * value;
  327. }
  328. }
  329. this.triggerEvent('triggerBindValue', {
  330. value: value
  331. })
  332. },
  333. /**
  334. * @desc : 处理数据的显示
  335. * @author : 周兴
  336. * @date : 2022/6/30 16:16
  337. */
  338. handleDisplayValue() {
  339. let inputValue = this.data.inputValue;
  340. let displayValue = ''
  341. // if(inputValue != undefined && inputValue != 0){
  342. //添加判断条件,当用户输入空数据问题会出现NaN 于继渤2022/06/30
  343. if (inputValue != undefined && inputValue && inputValue != 0) {
  344. // 判断是否有千分位
  345. if (this.data.formatThousandth) {
  346. displayValue = common.toThousandCents(inputValue);
  347. }
  348. // 如果是负数显示
  349. if (this.data.negative && displayValue.indexOf("-") < 0) {
  350. // displayValue = this.data.sign + "-" + displayValue;
  351. displayValue = "-" + displayValue;
  352. } else {
  353. // 显示金额符号
  354. // displayValue = this.data.sign + displayValue;
  355. }
  356. } else {
  357. displayValue = inputValue;
  358. }
  359. this.setData({
  360. displayValue: displayValue
  361. })
  362. },
  363. },
  364. /**
  365. * 组件生命周期
  366. */
  367. lifetimes: {
  368. attached: function () {
  369. //处理数据的显示
  370. this.handleDisplayValue();
  371. },
  372. detached: function () {
  373. // 在组件实例被从页面节点树移除时执行
  374. },
  375. },
  376. })