ChooseModal.vue 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <template>
  2. <BasicModal
  3. width="800px"
  4. :title="t('component.upload.choose')"
  5. v-bind="$attrs"
  6. @register="register"
  7. :showOkBtn="false"
  8. cancelText="关闭"
  9. >
  10. <a-button type="primary" preIcon="mdi:reload" iconSize="20" @click="reload" />
  11. <a-button
  12. v-if="type === 'images'"
  13. class="check-btn"
  14. :disabled="disable_btn"
  15. type="success"
  16. preIcon="fa-solid:check"
  17. @click="checkedBatches"
  18. >{{ t('component.upload.choose') }}</a-button
  19. >
  20. <BasicTable
  21. ref="tableRef"
  22. @register="registerTable"
  23. @selectionChange="selectionChange"
  24. @rowClick="selectionChange"
  25. rowKey="id"
  26. >
  27. <template #action="{ record, column }">
  28. <TableAction :actions="createActions(record, column)" />
  29. </template>
  30. </BasicTable>
  31. </BasicModal>
  32. </template>
  33. <script lang="ts">
  34. import { defineComponent, reactive, ref, toRefs, unref } from 'vue';
  35. import { BasicModal, useModalInner } from '/@/components/Modal';
  36. import { adapt } from '/@/utils/adapt';
  37. import { useI18n } from '/@/hooks/web/useI18n';
  38. import {
  39. BasicTable,
  40. useTable,
  41. TableAction,
  42. ActionItem,
  43. EditRecordRow,
  44. TableActionType,
  45. } from '/@/components/Table';
  46. import { columns } from './chooseModalData';
  47. interface Btn {
  48. disable_btn: boolean;
  49. }
  50. const fakeData = [
  51. {
  52. id: 1,
  53. imgUrl: 'https://zos.alipayobjects.com/rmsportal/jkjgkEfvpUPVyRjUImniVslZfWPnJuuZ.png',
  54. imagewidth: 120,
  55. imageheight: 120,
  56. createtime: '2020-10-20 21:22:22',
  57. },
  58. {
  59. id: 2,
  60. imgUrl: 'http://jetbill.cn/uploads/20210713/a05d360766d30868cdb878b4c0aac77d.jpg',
  61. imagewidth: 120,
  62. imageheight: 120,
  63. },
  64. {
  65. id: 3,
  66. imgUrl: 'http://jetbill.cn/uploads/20210712/51ae92cdb0bb22a88d6e73e62bcb1cad.jpg',
  67. imagewidth: 120,
  68. imageheight: 120,
  69. },
  70. ];
  71. const props = {
  72. value: { type: String, default: '' },
  73. type: { type: String, default: '' },
  74. };
  75. export default defineComponent({
  76. components: { BasicModal, BasicTable, TableAction },
  77. props,
  78. emits: ['checked', 'checkedBatches'],
  79. setup(props, { emit }) {
  80. const { t } = useI18n();
  81. const tableRef = ref<Nullable<TableActionType>>(null);
  82. const adaptWidth = adapt();
  83. const [register, { closeModal }] = useModalInner();
  84. const btn = reactive<Btn>({
  85. disable_btn: true,
  86. });
  87. const [registerTable] = useTable({
  88. // title: '管理员日志',
  89. // titleHelpMessage: '',
  90. rowSelection: { type: 'checkbox' },
  91. columns: columns,
  92. // clickToRowSelect: false, // 点击行不勾选
  93. // api: getUserList,
  94. dataSource: fakeData,
  95. actionColumn: {
  96. width: 80,
  97. title: '操作',
  98. dataIndex: 'action',
  99. slots: { customRender: 'action' },
  100. fixed: undefined,
  101. },
  102. showIndexColumn: false,
  103. bordered: true,
  104. });
  105. function getTableAction() {
  106. // 获取组件
  107. const tableAction = unref(tableRef);
  108. if (!tableAction) {
  109. throw new Error('tableAction is null');
  110. }
  111. return tableAction;
  112. }
  113. function reload() {
  114. console.log('========reload======');
  115. }
  116. function selectionChange() {
  117. const keys = getTableAction().getSelectRowKeys();
  118. if (keys.length) {
  119. btn.disable_btn = false;
  120. } else {
  121. btn.disable_btn = true;
  122. }
  123. }
  124. function handleChecked(record) {
  125. console.log(`record`, record);
  126. emit('checked', record.id, closeModal);
  127. }
  128. async function checkedBatches() {
  129. const keys = await getTableAction().getSelectRowKeys();
  130. emit('checkedBatches', keys, closeModal);
  131. }
  132. function createActions(record: EditRecordRow): ActionItem[] {
  133. return [
  134. {
  135. label: '选择',
  136. icon: 'fa-solid:check',
  137. color: 'success',
  138. onClick: handleChecked.bind(null, record),
  139. },
  140. ];
  141. }
  142. return {
  143. register,
  144. tableRef,
  145. registerTable,
  146. adaptWidth,
  147. checkedBatches,
  148. selectionChange,
  149. reload,
  150. createActions,
  151. getTableAction,
  152. t,
  153. ...toRefs(btn),
  154. ...toRefs(props),
  155. };
  156. },
  157. });
  158. </script>
  159. <style lang="less">
  160. .check-btn {
  161. margin-left: 5px !important;
  162. }
  163. </style>