EditRowTable.vue 8.8 KB

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