index.vue 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. <template>
  2. <div class="table-demo">
  3. <bs-table v-bind="tableOptions">
  4. <template #searchRight>
  5. <a-space>
  6. <a-button type="primary" @click="openEditDrawer">
  7. <template #icon>
  8. <PlusOutlined />
  9. </template>
  10. <span>新增</span>
  11. </a-button>
  12. </a-space>
  13. </template>
  14. <template #toolbarTop>
  15. <a-space>
  16. <a-button type="primary" size="small">新增</a-button>
  17. <a-button danger size="small">删除</a-button>
  18. </a-space>
  19. </template>
  20. <template #toolbarLeft>
  21. <a-space>
  22. <a-button type="primary" size="small" @click="openModal"> 表格选择器弹窗 </a-button>
  23. <a-button type="primary" size="small" @click="openModalBase"> 弹窗 </a-button>
  24. <a-button type="primary" size="small" @click="handleRefreshTable"> 刷新表格事件 </a-button>
  25. <span>累计客户:XXX|黑名单客户:XXX|S级客户 </span>
  26. </a-space>
  27. </template>
  28. <template #slotVue_default="{ row, column }">
  29. <span style="color: blue">{{ row[column?.field] }}</span>
  30. </template>
  31. <template #opt1_default="{ row, column }">
  32. <a-button type="primary" :disabled="true" size="small">编辑</a-button>
  33. </template>
  34. </bs-table>
  35. <add-or-edit-drawer ref="addOrEditDrawerRef" />
  36. <demo-table-selector-modal ref="demoTableSelectorModal" />
  37. <demo-base-modal ref="demoBaseModal" />
  38. </div>
  39. </template>
  40. <script setup lang="jsx">
  41. import BsTable, { useBsTable } from '/@/components/BsUi/Table/index.js';
  42. import { onMounted, ref } from 'vue';
  43. import AddOrEditDrawer from '/@/views/table-demo/components/AddOrEditDrawer.vue';
  44. import DemoTableSelectorModal from '/@/views/table-demo/components/DemoTableSelectorModal.vue';
  45. import DemoBaseModal from '/@/views/table-demo/components/DemoBaseModal.vue';
  46. const addOrEditDrawerRef = ref(null);
  47. const demoTableSelectorModal = ref(null);
  48. const demoBaseModal = ref(null);
  49. const openModal = () => {
  50. demoTableSelectorModal.value.showModal();
  51. };
  52. const openModalBase = () => {
  53. console.log('demoBaseModal', demoBaseModal);
  54. demoBaseModal.value.showModal();
  55. };
  56. const handleRefreshTable = () => {
  57. fetchTableData({ a: new Date() });
  58. };
  59. const handleEdit = (row) => {
  60. addOrEditDrawerRef.value.showDrawer({
  61. type: 'edit',
  62. row,
  63. });
  64. };
  65. const {
  66. tableOptions,
  67. setTablePropsValue: setValue,
  68. getTablePropsValue: getValue,
  69. fetchTableData,
  70. } = useBsTable({
  71. tableOptions: {
  72. url: '/api/get-table',
  73. gridOptions: {
  74. loading: false,
  75. columns: [
  76. {
  77. type: 'seq',
  78. width: 80,
  79. },
  80. {
  81. field: 'id',
  82. title: 'ID',
  83. },
  84. {
  85. field: 'slotVue',
  86. title: '插槽Vue写法',
  87. slots: {
  88. default: 'slotVue_default',
  89. },
  90. },
  91. {
  92. field: 'dictField',
  93. title: '字典',
  94. cellRender: {
  95. name: 'CellDict',
  96. },
  97. },
  98. {
  99. field: 'name',
  100. title: 'JSX插槽渲染',
  101. visible: true,
  102. slots: {
  103. default({ row, column }) {
  104. return <span style={{ color: 'red' }}>{row.name}</span>;
  105. },
  106. },
  107. },
  108. {
  109. field: 'opt',
  110. title: '操作JSX写法',
  111. width: '120px',
  112. fixed: 'right',
  113. visible: true,
  114. slots: {
  115. default({ row, column }) {
  116. return (
  117. <a-button
  118. type='text'
  119. size='small'
  120. disabled={false}
  121. onClick={() => {
  122. handleEdit(row);
  123. }}
  124. >
  125. 编辑
  126. </a-button>
  127. );
  128. },
  129. },
  130. },
  131. {
  132. field: 'opt1',
  133. title: '操作Vue插槽写法',
  134. width: '120px',
  135. fixed: 'right',
  136. slots: {
  137. default: 'opt1_default',
  138. },
  139. },
  140. ],
  141. data: [
  142. {
  143. id: 1,
  144. name: '测试数据',
  145. dictField: [
  146. {
  147. dictKeyId: '',
  148. dictValueId: '',
  149. remark: '',
  150. sort: 3,
  151. status: 1,
  152. valueCode: '03',
  153. valueName: '微信公众号',
  154. slotVue: '1111',
  155. },
  156. ],
  157. },
  158. ], // 模拟数据源
  159. },
  160. searchConfig: {
  161. enabled: true,
  162. fieldSpan: 6,
  163. fields: Array.from({ length: 10 }, (_, i) => {
  164. return {
  165. field: `name${i}`,
  166. label: `名称${i}`,
  167. component: 'a-input',
  168. componentProps: {
  169. placeholder: `请输入名称${i}`,
  170. },
  171. };
  172. }),
  173. },
  174. pagerConfig: {
  175. enabled: true,
  176. pageSize: 10,
  177. pageNum: 1,
  178. },
  179. toolbarConfig: {},
  180. // 每次查询接口之前,都会调用这个回调函数
  181. tableSearchBeforeBiz() {
  182. const searchParams = getValue('searchConfig.data');
  183. setValue('searchConfig.data', { ...searchParams, otherField: 'abc' });
  184. },
  185. // 表格初始化之前,只加载一次
  186. beforeMount() {
  187. console.log('表格加载前');
  188. },
  189. // 表格初始化完成,只加载一次
  190. mounted(gridRef) {
  191. console.log('表格加载后', gridRef);
  192. },
  193. },
  194. });
  195. const openEditDrawer = () => {
  196. addOrEditDrawerRef.value.showDrawer({ type: 'add', row: null });
  197. };
  198. </script>
  199. <style scoped lang="scss">
  200. .table-demo {
  201. }
  202. </style>