EditRowTable.vue 9.2 KB

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