ChooseModal.vue 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <template>
  2. <BasicModal
  3. width="800px"
  4. title="选择图片"
  5. v-bind="$attrs"
  6. @register="register"
  7. cancelText="关闭"
  8. @ok="handleSumbit"
  9. >
  10. <BasicTable
  11. ref="tableRef"
  12. rowKey="id"
  13. @register="registerTable"
  14. :rowSelection="{ type: type === 'images' || type === 'files' ? 'checkbox' : 'radio' }"
  15. >
  16. <template #action="{ record, column }">
  17. <TableAction :actions="createActions(record, column)" />
  18. </template>
  19. </BasicTable>
  20. </BasicModal>
  21. </template>
  22. <script lang="ts">
  23. import { defineComponent, onUpdated, ref, toRefs, unref } from 'vue';
  24. import { BasicModal, useModalInner } from '/@/components/Modal';
  25. import { BasicTable, useTable, TableAction, TableActionType } from '/@/components/Table';
  26. import { columns } from '../../attachment/data';
  27. import { useMessage } from '/@/hooks/web/useMessage';
  28. import { getAttachmentList } from '/@/api/sys/general';
  29. const props = {
  30. value: { type: String, default: '' },
  31. type: { type: String, default: '' },
  32. };
  33. export default defineComponent({
  34. components: { BasicModal, BasicTable, TableAction },
  35. props,
  36. emits: ['checked'],
  37. setup(props, { emit }) {
  38. const { createMessage } = useMessage();
  39. const { error } = createMessage;
  40. const tableRef = ref<Nullable<TableActionType>>(null);
  41. const [register, { closeModal }] = useModalInner();
  42. const [registerTable] = useTable({
  43. columns: columns,
  44. api: getAttachmentList,
  45. showIndexColumn: false,
  46. bordered: true,
  47. maxHeight: 300,
  48. });
  49. function getTableAction() {
  50. // 获取组件
  51. const tableAction = unref(tableRef);
  52. if (!tableAction) {
  53. throw new Error('tableAction is null');
  54. }
  55. return tableAction;
  56. }
  57. async function handleSumbit() {
  58. const rows = await getTableAction().getSelectRows();
  59. if (rows.length) {
  60. emit('checked', rows);
  61. closeModal();
  62. getTableAction().clearSelectedRowKeys();
  63. } else {
  64. error('请先选择文件!');
  65. }
  66. }
  67. onUpdated(() => {
  68. document.onkeydown = function (e) {
  69. if (e.key === 'Enter') {
  70. handleSumbit();
  71. }
  72. };
  73. });
  74. return {
  75. register,
  76. tableRef,
  77. registerTable,
  78. handleSumbit,
  79. getTableAction,
  80. ...toRefs(props),
  81. };
  82. },
  83. });
  84. </script>
  85. <style lang="less">
  86. .check-btn {
  87. margin-left: 5px !important;
  88. }
  89. </style>