EditRowTable.vue 7.6 KB

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