EditRowTable.vue 8.0 KB

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