riku
2024-08-12 65124213af664a68ad88ce7f6dcb133116d7702f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
Component({
  properties: {
    /**
     * 表单输入框采用T-design框架下的Input组件
     * 表单属性数组,数组每项属性如下
     * required: 是否必填,
     * label: 条目标签名,
     * placeholder: 默认提示,
     * name: 对应字段名,
     * value: 真实值,
     * status: 输入框状态。可选项:success/warning/error,
     * tips: 输入框下方提示文本,会根据不同的 status 呈现不同的样式,
     * inputType: 输入类型 (text: 输入框;switch:切换按钮; picker: 下拉框选项; cascader: 级联选择),
     * options: 当输入类型为picker或cascader时,提供可选项,
     * cascaderTitles: 当输入类型为cascader时,提供每个选项的标题,
     */
    formArr: {
      type: Array,
      value: [],
      observer(v) {
        this.setData({ formArray: v });
      },
    },
  },
 
  data: {
    formArray: [],
  },
 
  methods: {
    // 输入框
    onInputChange(e) {
      const { index } = e.currentTarget.dataset;
      const { value } = e.detail;
      this.setData({
        [`formArray[${index}].value`]: value,
      });
    },
    // 开关
    onSwitchChange(e) {
      const { index } = e.currentTarget.dataset;
      const { value } = e.detail;
      // const { formArray } = this.data;
      // formArray[index].value = value;
      // this.setData({ formArray });
      this.setData({
        [`formArray[${index}].value`]: value,
      });
    },
    // 下拉框
    showPicker(e) {
      const { index } = e.currentTarget.dataset;
      this.setData({ [`formArray[${index}].visible`]: true });
    },
    onPickerChange(e) {
      const { index } = e.currentTarget.dataset;
      const { label, value } = e.detail;
      this.setData({
        [`formArray[${index}].visible`]: false,
        [`formArray[${index}].value`]: {
          label: label[0],
          value: value[0],
        },
      });
    },
    onPickerCancel(e) {
      const { index } = e.currentTarget.dataset;
      this.setData({
        [`formArray[${index}].visible`]: false,
      });
    },
    // 级联选择
    showCascader(e) {
      const { index } = e.currentTarget.dataset;
      this.setData({ [`formArray[${index}].visible`]: true });
    },
    onCascaderChange(e) {
      const { selectedOptions, value } = e.detail;
      console.log(selectedOptions);
      console.log(value);
    },
 
    // 保存
    onSubmit() {
      const formObj = {};
      this.data.formArray.forEach(e => {
        if (e.inputType == 'picker') {
          formObj[e.name] = e.value.value;
        } else {
          formObj[e.name] = e.value;
        }
      });
      this.triggerEvent('submit', formObj);
    },
    // 取消
    onCancel() {
      this.triggerEvent('cancel');
    },
 
    _note(v) {
      let note = '';
      v.forEach(o => {
        if (note != o.label && o.value > 0) {
          if (note != '') {
            note += '/';
          }
          note += o.label;
        }
      });
      return note;
    },
  },
});