EditRowTable.vue 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. <template>
  2. <div class="p-4">
  3. <BasicTable @register="registerTable" @edit-change="onEditChange">
  4. <template #action="{ record, column }">
  5. <TableAction :actions="createActions(record, column)" />
  6. </template>
  7. </BasicTable>
  8. </div>
  9. </template>
  10. <script lang="ts">
  11. import { defineComponent, ref } from 'vue';
  12. import {
  13. BasicTable,
  14. useTable,
  15. TableAction,
  16. BasicColumn,
  17. ActionItem,
  18. EditRecordRow,
  19. } from '/@/components/Table';
  20. import { optionsListApi } from '/@/api/demo/select';
  21. import { demoListApi } from '/@/api/demo/table';
  22. import { treeOptionsListApi } from '/@/api/demo/tree';
  23. const columns: BasicColumn[] = [
  24. {
  25. title: '输入框',
  26. dataIndex: 'name',
  27. editRow: true,
  28. editComponentProps: {
  29. prefix: '$',
  30. },
  31. width: 150,
  32. },
  33. {
  34. title: '默认输入状态',
  35. dataIndex: 'name7',
  36. editRow: true,
  37. width: 150,
  38. },
  39. {
  40. title: '输入框校验',
  41. dataIndex: 'name1',
  42. editRow: true,
  43. align: 'left',
  44. // 默认必填校验
  45. editRule: true,
  46. width: 150,
  47. },
  48. {
  49. title: '输入框函数校验',
  50. dataIndex: 'name2',
  51. editRow: true,
  52. align: 'right',
  53. editRule: async (text) => {
  54. if (text === '2') {
  55. return '不能输入该值';
  56. }
  57. return '';
  58. },
  59. },
  60. {
  61. title: '数字输入框',
  62. dataIndex: 'id',
  63. editRow: true,
  64. editRule: true,
  65. editComponent: 'InputNumber',
  66. width: 150,
  67. },
  68. {
  69. title: '下拉框',
  70. dataIndex: 'name3',
  71. editRow: true,
  72. editComponent: 'Select',
  73. editComponentProps: {
  74. options: [
  75. {
  76. label: 'Option1',
  77. value: '1',
  78. },
  79. {
  80. label: 'Option2',
  81. value: '2',
  82. },
  83. {
  84. label: 'Option3',
  85. value: '3',
  86. },
  87. ],
  88. },
  89. width: 200,
  90. },
  91. {
  92. title: '远程下拉',
  93. dataIndex: 'name4',
  94. editRow: true,
  95. editComponent: 'ApiSelect',
  96. editComponentProps: {
  97. api: optionsListApi,
  98. resultField: 'list',
  99. labelField: 'name',
  100. valueField: 'id',
  101. },
  102. width: 200,
  103. },
  104. {
  105. title: '远程下拉树',
  106. dataIndex: 'name8',
  107. editRow: true,
  108. editComponent: 'ApiTreeSelect',
  109. editRule: false,
  110. editComponentProps: {
  111. api: treeOptionsListApi,
  112. resultField: 'list',
  113. },
  114. width: 200,
  115. },
  116. {
  117. title: '日期选择',
  118. dataIndex: 'date',
  119. editRow: true,
  120. editComponent: 'DatePicker',
  121. editComponentProps: {
  122. valueFormat: 'YYYY-MM-DD',
  123. format: 'YYYY-MM-DD',
  124. },
  125. width: 150,
  126. },
  127. {
  128. title: '时间选择',
  129. dataIndex: 'time',
  130. editRow: true,
  131. editComponent: 'TimePicker',
  132. editComponentProps: {
  133. valueFormat: 'HH:mm',
  134. format: 'HH:mm',
  135. },
  136. width: 100,
  137. },
  138. {
  139. title: '勾选框',
  140. dataIndex: 'name5',
  141. editRow: true,
  142. editComponent: 'Checkbox',
  143. editValueMap: (value) => {
  144. return value ? '是' : '否';
  145. },
  146. width: 100,
  147. },
  148. {
  149. title: '开关',
  150. dataIndex: 'name6',
  151. editRow: true,
  152. editComponent: 'Switch',
  153. editValueMap: (value) => {
  154. return value ? '开' : '关';
  155. },
  156. width: 100,
  157. },
  158. ];
  159. export default defineComponent({
  160. components: { BasicTable, TableAction },
  161. setup() {
  162. const currentEditKeyRef = ref('');
  163. const [registerTable] = useTable({
  164. title: '可编辑行示例',
  165. titleHelpMessage: [
  166. '本例中修改[数字输入框]这一列时,同一行的[远程下拉]列的当前编辑数据也会同步发生改变',
  167. ],
  168. api: demoListApi,
  169. columns: columns,
  170. showIndexColumn: false,
  171. showTableSetting: true,
  172. tableSetting: { fullScreen: true },
  173. actionColumn: {
  174. width: 160,
  175. title: 'Action',
  176. dataIndex: 'action',
  177. slots: { customRender: 'action' },
  178. },
  179. });
  180. function handleEdit(record: EditRecordRow) {
  181. currentEditKeyRef.value = record.key;
  182. record.onEdit?.(true);
  183. }
  184. function handleCancel(record: EditRecordRow) {
  185. currentEditKeyRef.value = '';
  186. record.onEdit?.(false, false);
  187. }
  188. async function handleSave(record: EditRecordRow) {
  189. const pass = await record.onEdit?.(false, true);
  190. if (pass) {
  191. currentEditKeyRef.value = '';
  192. }
  193. }
  194. function createActions(record: EditRecordRow, column: BasicColumn): ActionItem[] {
  195. if (!record.editable) {
  196. return [
  197. {
  198. label: '编辑',
  199. disabled: currentEditKeyRef.value ? currentEditKeyRef.value !== record.key : false,
  200. onClick: handleEdit.bind(null, record),
  201. },
  202. ];
  203. }
  204. return [
  205. {
  206. label: '保存',
  207. onClick: handleSave.bind(null, record, column),
  208. },
  209. {
  210. label: '取消',
  211. popConfirm: {
  212. title: '是否取消编辑',
  213. confirm: handleCancel.bind(null, record, column),
  214. },
  215. },
  216. ];
  217. }
  218. function onEditChange({ column, value, record }) {
  219. // 本例
  220. if (column.dataIndex === 'id') {
  221. record.editValueRefs.name4.value = `${value}`;
  222. }
  223. console.log(column, value, record);
  224. }
  225. return {
  226. registerTable,
  227. handleEdit,
  228. createActions,
  229. onEditChange,
  230. };
  231. },
  232. });
  233. </script>