index.vue 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. <template>
  2. <div class="p-4">
  3. <BasicTable ref="tableRef" @register="registerTable" :bordered="true" rowKey="id">
  4. <template #action="{ record, column }">
  5. <TableAction :actions="createActions(record, column)" />
  6. </template>
  7. <!-- <template #action="{ record, column }">
  8. </template> -->
  9. <template #form-custom> custom-slot </template>
  10. <template #toolbar>
  11. <Upload action="http://localhost:8000/api/upload/" method="POST" :showUploadList="false" />
  12. </template>
  13. </BasicTable>
  14. <!-- <Model @register="addRegister" :modelData="modelData" /> -->
  15. </div>
  16. </template>
  17. <script lang="ts">
  18. import { defineComponent, reactive, ref, unref } from 'vue';
  19. import { useModal } from '/@/components/Modal';
  20. import {
  21. createTableColumns,
  22. createPreviewActionColumn,
  23. } from '/@/components/customComponents/fileData';
  24. import { FileItem } from '/@/components/Upload/src/types';
  25. import Upload from '/@/components/customComponents/upload.vue';
  26. import {
  27. BasicTable,
  28. useTable,
  29. TableAction,
  30. BasicColumn,
  31. ActionItem,
  32. EditRecordRow,
  33. TableActionType,
  34. } from '/@/components/Table';
  35. interface ModelData {
  36. title: string;
  37. }
  38. export default defineComponent({
  39. components: { BasicTable, TableAction, Upload },
  40. setup() {
  41. const modelData = reactive<ModelData>({
  42. title: '',
  43. });
  44. const tableRef = ref<Nullable<TableActionType>>(null);
  45. const currentEditKeyRef = ref('');
  46. const [registerTable] = useTable({
  47. title: '基础示例',
  48. titleHelpMessage: '温馨提醒',
  49. rowSelection: { type: 'checkbox' },
  50. columns: createTableColumns(),
  51. clickToRowSelect: false, // 点击行不勾选
  52. // showTableSetting: true,
  53. // columns: [{title:'Id',dataIndex:'id',width:150},{title:'姓名',dataIndex:'name',width:200},{title:'地址',dataIndex:'addr',width:250}],
  54. dataSource: [
  55. {
  56. id: 1,
  57. name: '文件1',
  58. thumbUrl:
  59. 'https://dss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=1999921673,816131569&fm=26&gp=0.jpg',
  60. size: 1504,
  61. },
  62. {
  63. id: 2,
  64. thumbUrl:
  65. 'https://dss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=2496571732,442429806&fm=26&gp=0.jpg',
  66. name: '未知',
  67. size: 351,
  68. },
  69. { id: 3, name: '文件2', addr: '未知地点', size: 155528 },
  70. ],
  71. showIndexColumn: false,
  72. actionColumn: createPreviewActionColumn(handleRemove, handleDownload),
  73. });
  74. const [addRegister, { openModal: openAdd }] = useModal();
  75. const rowClick = (e: any) => {
  76. console.log(e);
  77. };
  78. function getTableAction() {
  79. // 获取组件
  80. const tableAction = unref(tableRef);
  81. if (!tableAction) {
  82. throw new Error('tableAction is null');
  83. }
  84. return tableAction;
  85. }
  86. function getSelectRowList() {
  87. // 获取选中行
  88. console.log(getTableAction().getSelectRows());
  89. }
  90. function getSelectRowKeyList() {
  91. // 获取选中行的key --- id
  92. console.log(getTableAction().getSelectRowKeys());
  93. }
  94. function addColumn() {
  95. modelData.title = '添加';
  96. openAdd(true, {
  97. name: '',
  98. age: '',
  99. addr: '',
  100. });
  101. }
  102. function handleSubmit() {
  103. console.log('handleSubmit');
  104. // console.log(data)
  105. }
  106. function deleteSelect() {
  107. console.log('删除选中行');
  108. let data = getTableAction().getSelectRowKeys();
  109. // console.log(getTableAction().getSelectRowKeys());
  110. data.map((item) => {
  111. console.log(item);
  112. });
  113. }
  114. function handleCancel(record: EditRecordRow) {
  115. currentEditKeyRef.value = '';
  116. record.onEdit?.(false, false);
  117. }
  118. async function handleSave(record: EditRecordRow) {
  119. const pass = await record.onEdit?.(false, true);
  120. console.log('------- 保存 ----------');
  121. console.log(record);
  122. console.log('------- 保存 ----------');
  123. if (pass) {
  124. currentEditKeyRef.value = '';
  125. }
  126. }
  127. // 删除
  128. function handleRemove(record: FileItem) {
  129. console.log('点击了删除');
  130. console.log(record);
  131. console.log('点击了删除');
  132. }
  133. // 下载
  134. function handleDownload(record: FileItem) {
  135. console.log('点击了下载');
  136. console.log(record);
  137. console.log('点击了下载');
  138. }
  139. function handleDelete(record: Recordable) {
  140. console.log('点击了删除', record);
  141. const data = getTableAction().getDataSource();
  142. getTableAction().setTableData(data.filter((item) => item.id !== record.id));
  143. }
  144. function createActions(record: EditRecordRow, column: BasicColumn): ActionItem[] {
  145. if (!record.editable) {
  146. return [
  147. {
  148. label: '删除',
  149. color: 'error',
  150. icon: 'ic:outline-delete-outline',
  151. disabled: currentEditKeyRef.value ? currentEditKeyRef.value !== record.key : false,
  152. popConfirm: {
  153. title: '是否确认删除',
  154. confirm: handleDelete.bind(null, record),
  155. },
  156. },
  157. ];
  158. }
  159. return [
  160. {
  161. label: '保存',
  162. onClick: handleSave.bind(null, record, column),
  163. },
  164. {
  165. label: '取消',
  166. popConfirm: {
  167. title: '是否取消编辑',
  168. confirm: handleCancel.bind(null, record, column),
  169. },
  170. },
  171. ];
  172. }
  173. return {
  174. modelData,
  175. // ...toRefs(state),
  176. tableRef,
  177. registerTable,
  178. rowClick,
  179. addColumn,
  180. deleteSelect,
  181. createActions,
  182. getTableAction,
  183. getSelectRowList,
  184. getSelectRowKeyList,
  185. addRegister,
  186. handleSubmit,
  187. };
  188. },
  189. });
  190. </script>