Blame view

src/views/project/order/FieldDetail.vue 5.32 KB
sanmu authored
1
2
3
4
<template>
  <BasicDrawer
    @register="register"
    v-bind="$attrs"
sanmu authored
5
    title="字段自定义"
sanmu authored
6
    :destroyOnClose="true"
sanmu authored
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
    width="60%"
    :isDetail="true"
    :showDetailBack="false"
  >
    <div>
      <BasicTable @register="registerTable" @edit-change="handleEditChange">
        <template #bodyCell="{ column, record }">
          <template v-if="column.key === 'action'">
            <TableAction :actions="createActions(record, column)" />
          </template>
        </template>
      </BasicTable>
      <a-button block class="mt-5" type="dashed" @click="handleAdd"> 新增选项 </a-button>
    </div>
    <!-- <template #titleToolbar> <a-button type="primary"> 申请编辑权限 </a-button></template> -->
  </BasicDrawer>
</template>
<script lang="ts">
  import { defineComponent, ref, toRaw } from 'vue';
  import {
    BasicTable,
    useTable,
    TableAction,
    BasicColumn,
    ActionItem,
    EditRecordRow,
  } from '/@/components/Table';
  import { BasicDrawer, useDrawerInner } from '/@/components/Drawer';
  import { useOrderStoreWithOut } from '/@/store/modules/order';
  import { dictCreate, dictDelete, dictList, dictUpdate } from '/@/api/project/order';

  const orderStore = useOrderStoreWithOut();

  const columns: BasicColumn[] = [
    {
      title: '筛选项',
      dataIndex: 'dictValue',
      editRow: true,
    },
  ];

  export default defineComponent({
    components: { BasicDrawer, BasicTable, TableAction },
    props: {
      onGoFormDetail: {
        type: Function,
      },
    },
    setup() {
      const dataSource = ref([]);
      const key = ref('');
sanmu authored
58
      const title = ref('12');
sanmu authored
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74

      const [registerTable, { getDataSource, reload }] = useTable({
        columns: columns,
        showIndexColumn: false,
        dataSource: toRaw(dataSource),
        actionColumn: {
          width: 160,
          title: '操作',
          dataIndex: 'action',
          // slots: { customRender: 'action' },
        },
        scroll: { y: '100%' },
        pagination: false,
      });

      const [register] = useDrawerInner((data) => {
sanmu authored
75
76
77
78
79
80
        let { dataIndex, customTitle } = data;

        // 如果dataIndex= 手工初型1或者2,则等于手工初型
        if (dataIndex === 'manualPreform1' || dataIndex == 'manualPreform2') {
          dataIndex = 'manualPreform';
        }
sanmu authored
81
82
83
        const dicts = orderStore.getDictInfo;
        const dict = dicts[dataIndex];
        dataSource.value = dict;
sanmu authored
84
sanmu authored
85
        title.value = customTitle;
sanmu authored
86
sanmu authored
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
        key.value = dataIndex;
      });

      function handleEdit(record: EditRecordRow) {
        record.onEdit?.(true);
      }

      function handleCancel(record: EditRecordRow) {
        record.onEdit?.(false);
        if (record.isNew) {
          const data = getDataSource();
          const index = data.findIndex((item) => item.key === record.key);
          data.splice(index, 1);
        }
      }

      async function handleSave(record: EditRecordRow) {
        if (record.id) {
sanmu authored
105
          await dictUpdate({ id: record.id, dictCode: key.value, dictValue: record.dictValue });
sanmu authored
106
107
108
109
110
        } else {
          await dictCreate({
            dictName: title.value,
            dictCode: key.value,
            dictValue: record.dictValue,
sanmu authored
111
            sort: (dataSource.value?.length || 0) + 1,
sanmu authored
112
113
114
115
116
117
118
          });
        }

        await orderStore.getDict();

        setTimeout(async () => {
          const res = await dictList({ dictCode: key.value });
119
120
121
122
          dataSource.value =
            res.records?.sort((x, y) => {
              return x.dictValue.localeCompare(y.dictValue);
            }) || [];
sanmu authored
123
124
125
126
127
128
129
130
131
132
          reload();
        }, 300);
      }

      async function handleDelete(record) {
        await dictDelete({ ids: [record.id] });
        await orderStore.getDict();
        record.onEdit?.(false, true);
        setTimeout(async () => {
          const res = await dictList({ dictCode: key.value });
sanmu authored
133
134
135
136
          dataSource.value =
            res.records?.sort((x, y) => {
              return x.dictValue.localeCompare(y.dictValue);
            }) || [];
sanmu authored
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
          reload();
        }, 300);
      }

      function handleEditChange(data) {
        console.log(data);
      }

      function handleAdd() {
        const data = getDataSource();
        const addRow: EditRecordRow = {
          name: '',
          no: '',
          dept: '',
          editable: true,
          isNew: true,
          key: `${Date.now()}`,
        };
        data.push(addRow);
      }

      function createActions(record: EditRecordRow, column: BasicColumn): ActionItem[] {
        if (!record.editable) {
          return [
            {
              label: '编辑',
              onClick: handleEdit.bind(null, record),
            },
            {
              label: '删除',
              onClick: handleDelete.bind(null, record),
            },
          ];
        }
        return [
          {
            label: '保存',
            onClick: handleSave.bind(null, record, column),
          },
          {
            label: '取消',
            popConfirm: {
              title: '是否取消编辑',
              confirm: handleCancel.bind(null, record, column),
            },
          },
        ];
      }
      return {
        title,
        register,
        registerTable,
        handleEdit,
        createActions,
        handleAdd,
        getDataSource,
        handleEditChange,
      };
    },
  });
</script>