index.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. const getTime = require("../../utils/getTime");
  2. Component({
  3. /**
  4. * 组件的属性列表
  5. */
  6. properties: {
  7. fromList: {
  8. type: Array,
  9. value: []
  10. },
  11. CompletedOrNot: Function, //完成与否回调
  12. },
  13. options: {
  14. multipleSlots: true
  15. },
  16. /**
  17. * 组件的初始数据
  18. */
  19. data: {
  20. endTime: "", //结束时间
  21. selectTime: "", //选择时间
  22. },
  23. lifetimes: {
  24. attached: function () {
  25. this.setData({
  26. endTime: getTime.formatTime(new Date(), '-').split(' ')[0]
  27. })
  28. }
  29. },
  30. /**
  31. * 组件的方法列表
  32. */
  33. methods: {
  34. /* 输入 */
  35. inputChange(e) {
  36. let {
  37. index,
  38. item
  39. } = e.currentTarget.dataset;
  40. item.value = e.detail;
  41. item.error = item.required && item.value == '' ? true : false;
  42. let fromList = this.data.fromList;
  43. fromList[index] = item;
  44. this.setData({
  45. fromList
  46. })
  47. this.statistics();
  48. },
  49. /* 日期选择器 */
  50. bindDateChange(e) {
  51. const {
  52. index
  53. } = e.currentTarget.dataset;
  54. let fromList = this.data.fromList;
  55. fromList[index].value = e.detail.value;
  56. fromList[index].error = false;
  57. this.setData({
  58. fromList
  59. })
  60. this.statistics();
  61. },
  62. /* 清空输入框 */
  63. inputClear(e) {
  64. console.log("清空", e.target.dataset.item.label)
  65. this.statistics();
  66. },
  67. /* 统计是否完成全部必填项 */
  68. statistics() {
  69. let list = this.data.fromList,
  70. sumCount = 0,
  71. count = 0;
  72. for (let i = 0; i < list.length; i++) {
  73. if (list[i].required) {
  74. sumCount++;
  75. if (list[i].value != "") count++;
  76. }
  77. }
  78. // console.log("必填总数", sumCount, '已填', count)
  79. this.triggerEvent("CompletedOrNot", sumCount == count)
  80. },
  81. /* 提交 */
  82. getData() {
  83. let list = this.data.fromList,
  84. returnData = {},
  85. isReturn = true;
  86. for (let i = 0; i < list.length; i++) {
  87. if (list[i].required && list[i].value == "") {
  88. list[i].error = true;
  89. isReturn = false;
  90. } else {
  91. returnData[list[i].valueName] = list[i].value;
  92. }
  93. }
  94. this.setData({
  95. fromList: list
  96. })
  97. return {
  98. returnData,
  99. isReturn
  100. }
  101. }
  102. }
  103. })