|
| 1 | +<template> |
| 2 | + <t-layout-page> |
| 3 | + <t-layout-page-item> |
| 4 | + <t-form |
| 5 | + :ref-obj.sync="formOpts.ref" |
| 6 | + :formOpts="formOpts" |
| 7 | + :widthSize="2" |
| 8 | + ref="formAllRefs" |
| 9 | + :all-ref.sync="allRefs" |
| 10 | + /> |
| 11 | + </t-layout-page-item> |
| 12 | + </t-layout-page> |
| 13 | +</template> |
| 14 | + |
| 15 | +<script> |
| 16 | +import cascaderData from './cascaderData.json' |
| 17 | +export default { |
| 18 | + name: 'dhFormDemo', |
| 19 | + data() { |
| 20 | + return { |
| 21 | + hobbyList: [], |
| 22 | + sexList: [], |
| 23 | + statusList: [], |
| 24 | + // form表单 |
| 25 | + formOpts: { |
| 26 | + ref: null, |
| 27 | + formData: { |
| 28 | + sex: null, // *性别: 0:男 1:女 |
| 29 | + hobby: null, // *爱好: 0:男 1:女 |
| 30 | + createDate: null, // 年份 |
| 31 | + valDate1: null, // 月份范围 |
| 32 | + valDate2: null, // 日期范围 |
| 33 | + valDate3: null, // 时间范围 |
| 34 | + desc: null, // 描述 |
| 35 | + deptCode: null, |
| 36 | + deptCode1: null, |
| 37 | + deptCode2: null, |
| 38 | + status1: null, |
| 39 | + status: null // *状态: 0:停用,1:启用(默认为1)', |
| 40 | + }, |
| 41 | + fieldList: [ |
| 42 | + { |
| 43 | + label: '性别', value: 'sex', placeholder: 'TSelect单选', ref: 'sexRef', comp: 't-select', isSelfCom: true, bind: { optionSource: [] }, eventHandle: { |
| 44 | + change: (val, scope) => this.changeSex(val, scope), |
| 45 | + } |
| 46 | + }, |
| 47 | + { label: '状态(继承list)', value: 'status1', placeholder: 'TSelect继承list方式', ref: 'status1Ref', type: 'select-arr', comp: 't-select', list: 'statusList', arrLabel: 'label', arrKey: 'key', bind: {} }, |
| 48 | + { label: '状态', value: 'status', placeholder: 'TSelect单选', comp: 't-select', ref: 'statusRef', isSelfCom: true, bind: { optionSource: [] } }, |
| 49 | + { label: '爱好', value: 'hobby', placeholder: 'TSelect多选', comp: 't-select', ref: 'hobbyRef', isSelfCom: true, bind: { multiple: true, optionSource: [] } }, |
| 50 | + { label: '部门', value: 'deptCode', placeholder: 'TCascader使用', comp: 't-cascader', ref: 'TCascaderRef', isSelfCom: true, bind: { props: { emitPath: false, children: 'children', label: 'deptName', value: 'deptNum', checkStrictly: true, }, options: [] } }, |
| 51 | + { label: '部门1', value: 'deptCode1', placeholder: 'el-cascader使用', comp: 'el-cascader', ref: 'ElCascaderRef', isSelfCom: true, bind: { props: { emitPath: false, children: 'children', label: 'deptName', value: 'deptNum', checkStrictly: true, }, options: [] } }, |
| 52 | + { |
| 53 | + label: '下拉选择表格', value: 'deptCode2', placeholder: 't-select-table使用', comp: 't-select-table', ref: 'selectTableRef-deptCode2', isSelfCom: true, bind: { |
| 54 | + isKeyup: true, |
| 55 | + maxHeight: 400, |
| 56 | + keywords: { label: 'name', value: 'id' }, |
| 57 | + table: { data: [] }, |
| 58 | + columns: [ |
| 59 | + { label: '物料编号', width: '100px', prop: 'code', align: 'left' }, |
| 60 | + { label: '物料名称', width: '149px', prop: 'name' }, |
| 61 | + { label: '规格', width: '149px', prop: 'spec' }, |
| 62 | + { label: '单位', width: '110px', prop: 'unitName' }, |
| 63 | + { label: '物料编号1', width: '149px', prop: 'code' }, |
| 64 | + { label: '物料名称1', width: '149px', prop: 'name' }, |
| 65 | + ], |
| 66 | + }, |
| 67 | + eventHandle: { |
| 68 | + radioChange: (val) => this.radioChange(val) |
| 69 | + } |
| 70 | + }, |
| 71 | + { label: '年份', value: 'createDate', placeholder: 'TDatePicker选择年份', type: 'year', ref: 'createDateRef', bind: { type: 'year' }, comp: 't-date-picker' }, |
| 72 | + { label: '月份范围', value: 'valDate1', comp: 't-date-picker', ref: 'valDate1Ref', bind: { type: 'monthrange', isPickerOptions: true } }, |
| 73 | + { label: '日期范围', value: 'valDate2', comp: 't-date-picker', ref: 'valDate2Ref', bind: { type: 'daterange', isPickerOptions: true } }, |
| 74 | + { label: '时间范围', value: 'valDate3', comp: 't-date-picker', ref: 'valDate3Ref', bind: { type: 'datetimerange', isPickerOptions: true } }, |
| 75 | + { label: '描述', value: 'desc', type: 'textarea', ref: 'descRef', comp: 'el-input', widthSize: 1 } |
| 76 | + ], |
| 77 | + operatorList: [ |
| 78 | + { label: '提交', type: 'danger', fun: this.submitForm }, |
| 79 | + { label: '重置', type: 'primary', fun: this.resetForm }, |
| 80 | + { |
| 81 | + label: "重置下拉表格", |
| 82 | + bind: { type: "primary" }, |
| 83 | + fun: () => this.resetValue("selectTableRef-deptCode2") |
| 84 | + }, |
| 85 | + { label: "重置性别", bind: { type: "primary" }, fun: () => this.resetValue("sexRef") }, |
| 86 | + { label: "重置状态1", bind: { type: "primary" }, fun: () => this.resetValue("status1Ref") }, |
| 87 | + { label: "重置状态", bind: { type: "primary" }, fun: () => this.resetValue("statusRef") }, |
| 88 | + { label: "重置爱好", bind: { type: "primary" }, fun: () => this.resetValue("hobbyRef") }, |
| 89 | + { label: "重置TCascader", bind: { type: "primary" }, fun: () => this.resetValue("TCascaderRef", 'deptCode') }, |
| 90 | + { label: "重置elCascader", bind: { type: "primary" }, fun: () => this.resetValue("ElCascaderRef", 'deptCode1') }, |
| 91 | + { label: "重置年份", bind: { type: "primary" }, fun: () => this.resetValue("createDateRef") }, |
| 92 | + { label: "重置月份范围", bind: { type: "primary" }, fun: () => this.resetValue("valDate1Ref") }, |
| 93 | + { label: "重置日期范围", bind: { type: "primary" }, fun: () => this.resetValue("valDate2Ref") }, |
| 94 | + { label: "重置时间范围", bind: { type: "primary" }, fun: () => this.resetValue("valDate3Ref") }, |
| 95 | + { label: "重置描述", bind: { type: "primary" }, fun: () => this.resetValue("descRef") }, |
| 96 | + ], |
| 97 | + listTypeInfo: { |
| 98 | + statusList: [], |
| 99 | + } |
| 100 | + }, |
| 101 | + allRefs: {} |
| 102 | + } |
| 103 | + }, |
| 104 | + async created() { |
| 105 | + await this.getSelectList() |
| 106 | + }, |
| 107 | + mounted() { |
| 108 | + console.log('所有refs', this.allRefs) |
| 109 | + }, |
| 110 | + methods: { |
| 111 | + getSelectList() { |
| 112 | + this.hobbyList = [ |
| 113 | + { label: '吉他', key: '0' }, |
| 114 | + { label: '看书', key: '1' }, |
| 115 | + { label: '美剧', key: '2' }, |
| 116 | + { label: '旅游', key: '3' }, |
| 117 | + { label: '音乐', key: '4' } |
| 118 | + ] |
| 119 | + this.sexList = [ |
| 120 | + { label: '女', key: 1 }, |
| 121 | + { label: '男', key: 0 } |
| 122 | + ] |
| 123 | + this.formOpts.listTypeInfo.statusList = [ |
| 124 | + { label: '启用', key: 1 }, |
| 125 | + { label: '停用', key: 0 } |
| 126 | + ] |
| 127 | + this.statusList = [ |
| 128 | + { label: '启用', key: 1 }, |
| 129 | + { label: '停用', key: 0 } |
| 130 | + ] |
| 131 | + const tableData = [ |
| 132 | + { id: 1, code: 1, name: '物料名称1', spec: '物料规格1', unitName: '吨' }, |
| 133 | + { id: 2, code: 2, name: '物料名称2', spec: '物料规格2', unitName: '吨' }, |
| 134 | + { id: 3, code: 3, name: '物料名称3', spec: '物料规格3', unitName: '吨' }, |
| 135 | + { id: 4, code: 4, name: '物料名称4', spec: '物料规格4', unitName: '吨' }, |
| 136 | + { id: 5, code: 5, name: '物料名称5', spec: '物料规格5', unitName: '吨' }, |
| 137 | + { id: 6, code: 6, name: '物料名称6', spec: '物料规格6', unitName: '吨' }, |
| 138 | + { id: 7, code: 7, name: '物料名称7', spec: '物料规格7', unitName: '吨' }, |
| 139 | + { id: 8, code: 8, name: '物料名称8', spec: '物料规格8', unitName: '吨' }, |
| 140 | + { id: 9, code: 9, name: '物料名称9', spec: '物料规格9', unitName: '吨' } |
| 141 | + ] |
| 142 | + const res = cascaderData |
| 143 | + this.formOpts.fieldList.map(val => { |
| 144 | + switch (val.value) { |
| 145 | + case 'sex': |
| 146 | + val.bind.optionSource = this.sexList |
| 147 | + break |
| 148 | + case 'status': |
| 149 | + val.bind.optionSource = this.statusList |
| 150 | + break |
| 151 | + case 'hobby': |
| 152 | + val.bind.optionSource = this.hobbyList |
| 153 | + break |
| 154 | + case 'deptCode': |
| 155 | + case 'deptCode1': |
| 156 | + val.bind.options = res.data |
| 157 | + break |
| 158 | + case 'deptCode2': |
| 159 | + val.bind.table.data = tableData |
| 160 | + // console.log('11--', val) |
| 161 | + break |
| 162 | + } |
| 163 | + }) |
| 164 | + }, |
| 165 | + changeSex(val, scope) { |
| 166 | + console.log('选择性别change事件', val, scope) |
| 167 | + }, |
| 168 | + // 下拉选择表格组件赋值 |
| 169 | + radioChange(val) { |
| 170 | + console.log('下拉选择表格组件赋值', val) |
| 171 | + this.formOpts.formData.deptCode2 = val.name |
| 172 | + }, |
| 173 | + // 提交form表单 |
| 174 | + submitForm() { |
| 175 | + this.formOpts.ref.validate((valid) => { |
| 176 | + console.log(88, valid) |
| 177 | + console.log(77, this.formOpts.formData) |
| 178 | + if (!valid) return |
| 179 | + console.log('最终数据', this.formOpts.formData) |
| 180 | + }) |
| 181 | + }, |
| 182 | + // 重置单个表单项的Ref |
| 183 | + resetValue(type, val) { |
| 184 | + if (type.includes('Cascader')) { |
| 185 | + this.formOpts.formData[val] = [] |
| 186 | + } else if (type == 'descRef') { |
| 187 | + this.formOpts.formData.desc = '' |
| 188 | + } else { |
| 189 | + this.allRefs[type][0].clear() |
| 190 | + } |
| 191 | + }, |
| 192 | + // 重置form表单 |
| 193 | + resetForm() { |
| 194 | + this.$refs.formAllRefs.resetFieldsSelf() |
| 195 | + } |
| 196 | + } |
| 197 | +} |
| 198 | +</script> |
0 commit comments