tj
2025-03-20 a35f4b4d0c555493cc464bfd36d037230547f1aa
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
<template>
  <div ref="container">
    <a-modal
      :title="title"
      :width="1000"
      :visible="visible"
      :confirmLoading="confirmLoading"
      :getContainer="() => $refs.container"
      :maskStyle="{'top':'93px','left':'154px'}"
      :wrapClassName="wrapClassNameInfo()"
      :mask="isDesktop()"
      :maskClosable="false"
      @ok="handleOk"
      @cancel="handleCancel"
      cancelText="关闭">
      <template slot="footer">
        <a-button key="back" @click="handleCancel">关闭</a-button>
      </template>
      <a-card :bordered="false">
        <!-- 操作按钮区域 -->
        <div class="table-operator">
          <a-button type="primary" icon="plus" @click="handleAdd">新增</a-button>
        </div>
        <!-- table区域-begin -->
        <div>
          <a-table
            ref="table"
            size="middle"
            bordered
            rowKey="id"
            :columns="columns"
            :dataSource="dataSource"
            :pagination="false"
            :loading="loading">
            <span slot="action" slot-scope="text, record">
              <a @click="handleEdit(record)">编辑</a>
              <a-divider type="vertical" />
              <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)">
                <a>删除</a>
              </a-popconfirm>
            </span>
            <template slot="statusSlot" slot-scope="text">
              <a-tag :color="text === 1 ? 'green' : 'orange'">
                {{ text === 1 ? '启用' : '停用' }}
              </a-tag>
            </template>
          </a-table>
        </div>
        <!-- table区域-end -->
      </a-card>
      
      <a-modal
        :title="itemTitle"
        :width="600"
        :visible="itemVisible"
        :confirmLoading="itemConfirmLoading"
        @ok="handleItemOk"
        @cancel="handleItemCancel"
        cancelText="取消"
        okText="保存">
        <a-form :form="itemForm">
          <a-form-item label="字典项文本" :labelCol="labelCol" :wrapperCol="wrapperCol">
            <a-input placeholder="请输入字典项文本" v-decorator="['itemText', validatorRules.itemText]" />
          </a-form-item>
          <a-form-item label="字典项值" :labelCol="labelCol" :wrapperCol="wrapperCol">
            <a-input placeholder="请输入字典项值" v-decorator="['itemValue', validatorRules.itemValue]" />
          </a-form-item>
          <a-form-item label="描述" :labelCol="labelCol" :wrapperCol="wrapperCol">
            <a-textarea placeholder="请输入描述" v-decorator="['description']" :rows="2" />
          </a-form-item>
          <a-form-item label="排序" :labelCol="labelCol" :wrapperCol="wrapperCol">
            <a-input-number placeholder="请输入排序" v-decorator="['sortOrder', {initialValue: 0}]" :min="0" style="width: 100%" />
          </a-form-item>
          <a-form-item label="状态" :labelCol="labelCol" :wrapperCol="wrapperCol">
            <a-radio-group v-decorator="['status', {initialValue: 1}]">
              <a-radio :value="1">启用</a-radio>
              <a-radio :value="0">停用</a-radio>
            </a-radio-group>
          </a-form-item>
        </a-form>
      </a-modal>
    </a-modal>
  </div>
</template>
 
<script>
  import { mixinDevice } from '@/utils/mixin'
  import { getAction, postAction, putAction, deleteAction } from '@/api/manage'
  import pick from 'lodash.pick'
 
  export default {
    name: "SysDictItemModal",
    mixins: [mixinDevice],
    data () {
      return {
        title:"字典项管理",
        visible: false,
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        loading: false,
        dataSource: [],
        
        // 字典项表单相关
        itemTitle: "",
        itemVisible: false,
        itemConfirmLoading: false,
        itemModel: {},
        itemForm: this.$form.createForm(this),
        validatorRules: {
          itemText: {
            rules: [{
              required: true,
              message: '请输入字典项文本!'
            }]
          },
          itemValue: {
            rules: [{
              required: true,
              message: '请输入字典项值!'
            }]
          }
        },
        
        // 表头
        columns: [
          {
            title: '#',
            dataIndex: '',
            key:'rowIndex',
            width:60,
            align:"center",
            customRender:function (t,r,index) {
              return parseInt(index)+1;
            }
          },
          {
            title: '操作',
            dataIndex: 'action',
            width:120,
            align:"center",
            scopedSlots: { customRender: 'action' }
          },
          {
            title: '文本',
            align:"left",
            dataIndex: 'itemText'
          },
          {
            title: '值',
            align:"left",
            dataIndex: 'itemValue'
          },
          {
            title: '描述',
            align:"left",
            dataIndex: 'description'
          },
          {
            title: '排序',
            align:"center",
            dataIndex: 'sortOrder',
            width: 80
          },
          {
            title: '状态',
            align:"center",
            dataIndex: 'status',
            width: 80,
            scopedSlots: { customRender: 'statusSlot' }
          }
        ]
      }
    },
    methods: {
      show (record) {
        this.model = Object.assign({}, record)
        this.visible = true
        this.loadData()
      },
      loadData () {
        this.loading = true
        getAction(`/sysDict/items/${this.model.id}`).then((res) => {
          if (res.code === 200) {
            this.dataSource = res.data
          }
          this.loading = false
        })
      },
      handleAdd () {
        this.itemTitle = "新增字典项"
        this.itemForm.resetFields()
        this.itemModel = {}
        this.itemVisible = true
      },
      handleEdit (record) {
        this.itemTitle = "编辑字典项"
        this.itemForm.resetFields()
        this.itemModel = Object.assign({}, record)
        this.itemVisible = true
        this.$nextTick(() => {
          this.itemForm.setFieldsValue(pick(this.itemModel,'itemText','itemValue','description','sortOrder','status'))
        })
      },
      handleDelete (id) {
        deleteAction(`/sysDict/item/${id}`).then((res) => {
          if (res.code === 200) {
            this.$message.success(res.msg)
            this.loadData()
          } else {
            this.$message.warning(res.msg)
          }
        })
      },
      handleItemOk () {
        const that = this
        this.itemForm.validateFields((err, values) => {
          if (!err) {
            that.itemConfirmLoading = true
            let formData = Object.assign(this.itemModel, values)
            formData.dictId = this.model.id
            let obj
            if(!this.itemModel.id){
              obj = postAction('/sysDict/item', formData)
            }else{
              obj = putAction('/sysDict/item', formData)
            }
            obj.then((res)=>{
              if(res.code === 200){
                that.$message.success(res.msg)
                that.loadData()
                that.itemVisible = false
              }else{
                that.$message.warning(res.msg)
              }
            }).finally(() => {
              that.itemConfirmLoading = false
            })
          }
        })
      },
      handleItemCancel () {
        this.itemVisible = false
      },
      handleCancel () {
        this.close()
      },
      close () {
        this.$emit('close')
        this.visible = false
      }
    }
  }
</script>
 
<style lang="less" scoped>
  .table-operator {
    margin-bottom: 18px;
  }
</style>