dk-number-input.js 7.5 KB

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