EditRowTable.vue 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. <template>
  2. <div class="p-4" v-if="refresh">
  3. <a-button v-if="isAdd" type="primary" @click="addRow"> 新增 </a-button>
  4. <BasicTable @register="registerTable" @edit-change="onEditChange">
  5. <template #action="{ record, column }">
  6. <div class="vent-flex-row">
  7. <TableAction :actions="createActions(record, column)" />
  8. </div>
  9. </template>
  10. <template #bodyCell="{ column, record }">
  11. <slot name="filterCell" v-bind="{ column, record }"> </slot>
  12. </template>
  13. </BasicTable>
  14. </div>
  15. </template>
  16. <script lang="ts">
  17. import { defineComponent, ref, nextTick, watch } from 'vue';
  18. import { BasicTable, useTable, TableAction, BasicColumn, ActionItem, EditRecordRow } from '/@/components/Table';
  19. import { useMessage } from '/@/hooks/web/useMessage';
  20. // import { nextTick } from 'process';
  21. export default defineComponent({
  22. components: { BasicTable, TableAction },
  23. props: {
  24. columns: {
  25. type: Array,
  26. requried: true,
  27. },
  28. dataSource: {
  29. type: Array,
  30. },
  31. searchFormSchema: {
  32. type: Array,
  33. default: () => [],
  34. },
  35. list: {
  36. type: Function,
  37. },
  38. isAdd: {
  39. type: Boolean,
  40. },
  41. isRadio: {
  42. type: Boolean,
  43. },
  44. scroll: {
  45. type: Object,
  46. default: {},
  47. },
  48. },
  49. emits: ['saveOrUpdate', 'deleteById', 'rowChange'],
  50. setup(props, { emit, expose }) {
  51. const { createMessage: msg } = useMessage();
  52. const currentEditKeyRef = ref('');
  53. const dataList = ref<any[]>([]);
  54. const refresh = ref(true);
  55. const tableScroll = props.scroll.y ? ref({ y: props.scroll.y }) : ref({});
  56. const [registerTable, { insertTableDataRecord, reload, getSelectRows, getDataSource, setTableData, clearSelectedRowKeys }] = !props.list
  57. ? useTable({
  58. title: '',
  59. dataSource: props.dataSource,
  60. rowKey: 'id',
  61. clickToRowSelect: false,
  62. columns: props.columns as BasicColumn[],
  63. showIndexColumn: false,
  64. showTableSetting: false,
  65. rowSelection: !props.isRadio ? undefined : { type: 'radio', onChange: rowChange },
  66. tableSetting: { fullScreen: true },
  67. scroll: tableScroll,
  68. formConfig: {
  69. labelWidth: 120,
  70. schemas: props.searchFormSchema,
  71. autoSubmitOnEnter: true,
  72. },
  73. actionColumn: {
  74. width: 160,
  75. title: '操作',
  76. dataIndex: 'action',
  77. slots: { customRender: 'action' },
  78. },
  79. })
  80. : useTable({
  81. title: '',
  82. api: props.list,
  83. rowKey: 'id',
  84. clickToRowSelect: false,
  85. columns: props.columns as BasicColumn[],
  86. showIndexColumn: false,
  87. showTableSetting: false,
  88. scroll: tableScroll,
  89. rowSelection: !props.isRadio ? undefined : { type: 'radio', onChange: rowChange },
  90. tableSetting: { fullScreen: true },
  91. actionColumn: {
  92. width: 160,
  93. title: '操作',
  94. dataIndex: 'action',
  95. slots: { customRender: 'action' },
  96. },
  97. });
  98. function rowChange(e) {
  99. emit('rowChange', e[0], getSelectRows()[0]);
  100. }
  101. function addRow() {
  102. const record = {} as EditRecordRow;
  103. insertTableDataRecord(record);
  104. nextTick(() => {
  105. handleEdit(record);
  106. });
  107. }
  108. function handleEdit(record: EditRecordRow) {
  109. currentEditKeyRef.value = record.key;
  110. record.onEdit?.(true);
  111. }
  112. function handleCancel(record: EditRecordRow) {
  113. currentEditKeyRef.value = '';
  114. record.onEdit?.(false, false);
  115. if (!record.id) {
  116. const dataSource = getDataSource();
  117. const res = dataSource.filter((item) => {
  118. return item.id != undefined;
  119. });
  120. setTableData(res);
  121. }
  122. }
  123. function handleDelete(record: EditRecordRow) {
  124. if (record.id) emit('deleteById', record.id, reload);
  125. }
  126. async function handleSave(record: EditRecordRow) {
  127. // 校验
  128. msg.loading({ content: '正在保存...', duration: 0, key: 'saving' });
  129. const valid = await record.onValid?.();
  130. if (valid) {
  131. try {
  132. //TODO 此处将数据提交给服务器保存
  133. emit('saveOrUpdate', Object.assign(record, record.editValueRefs), reload);
  134. refresh.value = false;
  135. // 保存之后提交编辑状态
  136. const pass = await record.onEdit?.(false, true);
  137. if (pass) {
  138. currentEditKeyRef.value = '';
  139. }
  140. msg.success({ content: '数据已保存', key: 'saving' });
  141. } catch (error) {
  142. msg.error({ content: '保存失败', key: 'saving' });
  143. }
  144. } else {
  145. msg.error({ content: '请填写正确的数据', key: 'saving' });
  146. }
  147. nextTick(() => {
  148. refresh.value = true;
  149. clearSelectedRowKeys();
  150. });
  151. }
  152. function createActions(record: EditRecordRow, column: BasicColumn): ActionItem[] {
  153. if (!record.editable) {
  154. if (props.isAdd) {
  155. return [
  156. {
  157. label: '编辑',
  158. disabled: currentEditKeyRef.value ? currentEditKeyRef.value !== record.key : false,
  159. onClick: handleEdit.bind(null, record),
  160. },
  161. {
  162. label: '删除',
  163. disabled: currentEditKeyRef.value ? currentEditKeyRef.value !== record.key : false,
  164. popConfirm: {
  165. title: '是否删除',
  166. confirm: handleDelete.bind(null, record),
  167. },
  168. },
  169. ];
  170. } else {
  171. return [
  172. {
  173. label: '编辑',
  174. disabled: currentEditKeyRef.value ? currentEditKeyRef.value !== record.key : false,
  175. onClick: handleEdit.bind(null, record),
  176. },
  177. {
  178. label: '删除',
  179. popConfirm: {
  180. title: '是否删除',
  181. confirm: handleDelete.bind(null, record),
  182. },
  183. },
  184. ];
  185. }
  186. }
  187. return [
  188. {
  189. label: '保存',
  190. popConfirm: {
  191. title: '是否保存',
  192. confirm: handleSave.bind(null, record),
  193. },
  194. },
  195. {
  196. label: '取消',
  197. popConfirm: {
  198. title: '是否取消编辑',
  199. confirm: handleCancel.bind(null, record),
  200. },
  201. },
  202. ];
  203. }
  204. function onEditChange({ column, value, record }) {
  205. // 本例
  206. // if (column.dataIndex === 'deviceid') {
  207. // record.editValueRefs.name.value = `${value}`;
  208. // }
  209. // console.log(column, value, record);
  210. }
  211. watch(
  212. () => props.dataSource,
  213. (newVal: any[]) => {
  214. setTableData(newVal);
  215. }
  216. );
  217. expose({
  218. reload,
  219. });
  220. return {
  221. refresh,
  222. registerTable,
  223. handleEdit,
  224. createActions,
  225. onEditChange,
  226. addRow,
  227. reload,
  228. getDataSource,
  229. dataList,
  230. };
  231. },
  232. });
  233. </script>
  234. <style scoped lang="less">
  235. @ventSpace: zxm;
  236. @vent-table-no-hover: #00bfff10;
  237. :deep(.@{ventSpace}-table-body) {
  238. height: auto !important;
  239. }
  240. :deep(.@{ventSpace}-table-cell-row-hover) {
  241. background: #264d8833 !important;
  242. }
  243. :deep(.@{ventSpace}-table-row-selected) {
  244. background: #268bc522 !important;
  245. }
  246. :deep(.@{ventSpace}-table-tbody > tr > td) {
  247. background-color: #0dc3ff05;
  248. }
  249. :deep(.jeecg-basic-table-row__striped) {
  250. td {
  251. background-color: @vent-table-no-hover !important;
  252. }
  253. }
  254. :deep(.@{ventSpace}-select-dropdown) {
  255. .@{ventSpace}-select-item-option-selected,
  256. .@{ventSpace}-select-item-option-active {
  257. background-color: #ffffff33 !important;
  258. }
  259. .@{ventSpace}-select-item:hover {
  260. background-color: #ffffff33 !important;
  261. }
  262. }
  263. </style>