zhuoyuan.wang
2024-06-19 15ebe96f28cadec6a726c5324593a40bbf56205f
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
<template>
  <app-collapse-form title="工作表配置">
    <template #header-button="scope">
      <el-button
        icon="Plus"
        size="small"
        type="primary"
        @click="onOpen({})"
      >
        新增
      </el-button>
    </template>
    <app-table-list v-bind="tableListProps">
      <template #table-sort="scope">
        <el-button
          size="small"
          icon="Rank"
          link
        >
        </el-button>
      </template>
      <template #table-operate="scope">
        <el-button
          icon="Edit"
          size="small"
          type="primary"
          @click="onOpen(scope.row)">
        </el-button>
        <el-button
          size="small"
          type="danger"
          icon="Delete"
          @click="onDelete(scope.row)"
        >
        </el-button>
      </template>
    </app-table-list>
  </app-collapse-form>
  <form-dialog v-bind="formDialogProps"/>
</template>
 
<script setup>
import FormDialog from './FormDialog';
import Sortable from "sortablejs";
 
const formDialogRef = ref();
const tableListRef = ref();
 
import {meta} from "@/hooks";
import * as tools from "@/utils/tools.js";
import {modal} from "@/plugins";
 
const {useMetaData} = meta;
 
const [columns, filters, fields] = useMetaData(
  'fee_model_workbook',
  ({columnsTools}) => {
    columnsTools.splice(0, 0,
      {
        type: 'slot',
        label: "排序",
        prop: 'sort',
        table: {
          fixed: true,
          width: 55
        }
      }
    );
    columnsTools.splice(columns.length, 0,
      {
        type: 'slot',
        label: "操作",
        prop: 'operate',
        table: {
          fixed: 'right',
          width: 120
        }
      }
    );
    columns.forEach((e, index) => {
      if (index !== 0) {
        e.table.className = 'ignore-elements';
      }
    });
    tableListProps.columns = columns;
    tableListRef.value.$forceUpdate(tableListProps);
  }
);
 
const formDialogProps = {
  ref: formDialogRef,
  subSubmit(row) {
    let isNew = true;
    const tableList = tableListRef.value.getTableData();
    tableList.forEach((e, i) => {
      if (e.id === row.id) {
        isNew = false;
        tableList[i] = row;
      }
    });
    if (isNew) {
      tableList.push(row);
    }
 
    tableList.forEach((e, index) => {
      e.indexno = index + 1;
    });
 
    tableListRef.value.setTableData([...tableList]);
  }
};
 
const tableListProps = {
  class: 'fields-sort-table-class',
  ref: tableListRef,
  title: '列表',
  complementHeight: 0,
  columns,
};
 
const onDelete = async (row) => {
  await modal.confirm("是否确认删除?");
  let tableList = tableListRef.value.getTableData();
  tableList.forEach((e, i) => {
    if (e.id === row.id) {
      tableList[i] = null;
    }
  });
  tableList = tableList.filter(Boolean);
  tableList.forEach((e, index) => {
    e.orderno = index + 1;
  });
  tableListRef.value.setTableData([...tableList]);
}
 
const onOpen = (row) => {
  formDialogRef.value.onOpen(row);
};
 
const onSetData = (list) => {
  tableListRef.value.setTableData(tools.listSort(list, 'orderno'));
  dragSortHandler();
}
 
const onGetData = () => {
  return tableListRef.value.getTableData().map((row) => {
    delete row.id;
    row.orderby_segment = row.orderby_segment || '';
    return row;
  });
}
 
let isSortable = false;
 
const dragSortHandler = async () => {
  window.setTimeout(() => {
 
    if (!isSortable) {
      isSortable = true;
      const elNode = document.querySelector(`.${tableListProps.class} tbody`);
      Sortable.create(elNode, {
        animation: 500,
        forceFallback: true,
        filter: ".ignore-elements",
        onEnd({newIndex, oldIndex}) {
          let list = tableListRef.value.getTableData();
          const currRow = list.splice(oldIndex, 1)[0];
          list.splice(newIndex, 0, currRow);
          list.forEach((e, index) => {
            e.orderno = index + 1;
          });
          tableListRef.value.setTableData([...list]);
        }
      });
    }
  }, 200);
};
 
defineExpose({
  /**
   * 初始化
   */
  onSetData,
  onGetData
});
 
</script>