dk-number-input.js 7.6 KB

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