dk-number-input.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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. bindInput(e) {
  190. // console.log('ddd1',e,this.data.inputValue,e.detail.value.endsWith('.'));
  191. let flag = true;
  192. let values = e.detail.value.split('.')
  193. // 如果输入的是小数点,那么需要判断是否已经有小数点
  194. if (e.detail.value.endsWith('.')) {
  195. // 说明有多个小数点
  196. // console.log('zzz',values,this.data.inputValue,(this.data.inputValue + '').indexOf('.'));
  197. if (values && values.length > 2) {
  198. if ((this.data.inputValue + '').indexOf('.') >= 0) {
  199. return this.data.inputValue;
  200. } else {
  201. // 可能连续输入2个小数点
  202. return this.data.inputValue + '.';
  203. }
  204. }
  205. // 如果inputValue是空,那么当成0处理
  206. if (e.detail.value == '.') {
  207. return '0.';
  208. }
  209. flag = false;
  210. }
  211. //控制小数点后数字的输入
  212. let digits = this.data.digits
  213. if (values && values.length === 2 && values[1].length > digits) {
  214. flag = false;
  215. return this.data.inputValue;
  216. }
  217. if (flag) {
  218. let value = 0;
  219. // 判断是否超过最大值
  220. if (this.data.maxFlag) {
  221. if (e.detail.value > this.data.max) {
  222. value = this.data.max
  223. } else {
  224. value = e.detail.value
  225. }
  226. } else {
  227. value = e.detail.value
  228. }
  229. this.setData({
  230. clearFlag: false,
  231. inputValue: value,
  232. })
  233. this.handleData()
  234. }
  235. },
  236. /**
  237. * @desc : 光标离开处理数据的显示
  238. * @author : 周兴
  239. * @date : 2022/6/30 16:16
  240. */
  241. bindBlur() {
  242. if (this.data.clearFlag) {
  243. this.setData({
  244. inputValue: 0,
  245. displayValue: 0,
  246. clearFlag: false
  247. })
  248. }
  249. // 处理数据
  250. if (this.data.inputValue) {
  251. this.setData({
  252. inputValue: Number(this.data.inputValue)
  253. })
  254. }
  255. // 离开的时候进行数据处理
  256. // this.handleDisplayValue();
  257. this.triggerEvent('triggerBindBlur', {
  258. inputValue: this.data.inputValue,
  259. index: this.data.dataIndex,
  260. index2: this.data.dataIndex2,
  261. })
  262. this.setData({
  263. focus: false
  264. })
  265. },
  266. /**
  267. * @desc : 绑定值
  268. * @author : 周兴
  269. * @date : 2022/6/30 16:16
  270. */
  271. handleData() {
  272. let value = this.data.inputValue;
  273. if (value == null || value == '') {
  274. this.setData({
  275. clearFlag: true
  276. })
  277. value = 0
  278. }
  279. if (isNaN(value)) {
  280. value = 0
  281. }
  282. // 如果负数标识开启
  283. if (this.data.negative) {
  284. if (value > 0) {
  285. value = -1 * value;
  286. }
  287. } else {
  288. if (value < 0) {
  289. value = -1 * value;
  290. }
  291. }
  292. this.triggerEvent('triggerBindValue', {
  293. value: value
  294. })
  295. },
  296. /**
  297. * @desc : 处理数据的显示
  298. * @author : 周兴
  299. * @date : 2022/6/30 16:16
  300. */
  301. handleDisplayValue() {
  302. let inputValue = this.data.inputValue;
  303. let displayValue = ''
  304. // if(inputValue != undefined && inputValue != 0){
  305. //添加判断条件,当用户输入空数据问题会出现NaN 于继渤2022/06/30
  306. if (inputValue != undefined && inputValue && inputValue != 0) {
  307. // 判断是否有千分位
  308. console.log('ttt',this.data.formatThousandth);
  309. if (this.data.formatThousandth) {
  310. displayValue = common.toThousandCents(inputValue);
  311. }
  312. // 如果是负数显示
  313. if (this.data.negative && displayValue.indexOf("-") < 0) {
  314. // displayValue = this.data.sign + "-" + displayValue;
  315. displayValue = "-" + displayValue;
  316. } else {
  317. // 显示金额符号
  318. // displayValue = this.data.sign + displayValue;
  319. }
  320. } else {
  321. displayValue = inputValue;
  322. }
  323. this.setData({
  324. displayValue: displayValue
  325. })
  326. },
  327. },
  328. /**
  329. * 组件生命周期
  330. */
  331. lifetimes: {
  332. attached: function () {
  333. //处理数据的显示
  334. this.handleDisplayValue();
  335. },
  336. detached: function () {
  337. // 在组件实例被从页面节点树移除时执行
  338. },
  339. },
  340. })