index.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. <template>
  2. <BsPageWrapper>
  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="middle">新增</a-button>
  17. <a-button danger size="middle">删除</a-button>
  18. </a-space>
  19. </template>
  20. <template #toolbarLeft>
  21. <a-space>
  22. <a-button type="primary" size="middle" @click="openModal"> 表格选择器弹窗 </a-button>
  23. <a-button type="primary" size="middle" @click="openModalBase"> 弹窗 </a-button>
  24. <a-button type="primary" size="middle" @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. </BsPageWrapper>
  39. </template>
  40. <script setup lang="jsx">
  41. import BsTable, { useBsTable } from '/@/components/BsUi/Table/index.js';
  42. import { h, 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. import { message } from 'ant-design-vue';
  47. import { BorderOuterOutlined, SearchOutlined } from '@ant-design/icons-vue';
  48. import { DISPLAY_STATE } from '/@/components/BsUi/constant.js';
  49. import { isFunction } from 'lodash';
  50. import { BsPageWrapper } from '/@/components/BsUi';
  51. const addOrEditDrawerRef = ref(null);
  52. const demoTableSelectorModal = ref(null);
  53. const demoBaseModal = ref(null);
  54. const openModal = () => {
  55. demoTableSelectorModal.value.showModal();
  56. };
  57. const openModalBase = () => {
  58. demoBaseModal.value.showModal();
  59. };
  60. const handleRefreshTable = () => {
  61. fetchTableData({ a: new Date() });
  62. };
  63. const handleEdit = (row) => {
  64. addOrEditDrawerRef.value.showDrawer({
  65. type: 'edit',
  66. row,
  67. });
  68. };
  69. const {
  70. tableOptions,
  71. setTablePropsValue: setValue,
  72. getTablePropsValue: getValue,
  73. fetchTableData,
  74. } = useBsTable({
  75. tableOptions: {
  76. toolbarTopConfig: {
  77. enable: true,
  78. buttons: [
  79. {
  80. code: 'btn1',
  81. title: '新增',
  82. props: {
  83. type: 'primary',
  84. disabled: false,
  85. icon: h(SearchOutlined),
  86. onClick(event) {
  87. message.success('ok');
  88. },
  89. },
  90. },
  91. {
  92. code: 'btn2',
  93. title: '禁用新增',
  94. props: {
  95. type: 'primary',
  96. danger: true,
  97. onClick(event) {
  98. setValue('toolbarTopConfig.buttons.0.props.disabled', true);
  99. },
  100. },
  101. },
  102. {
  103. code: 'btn3',
  104. title: '解禁新增',
  105. props: {
  106. type: 'text',
  107. danger: true,
  108. onClick(event) {
  109. setValue('toolbarTopConfig.buttons.0.props.disabled', false);
  110. },
  111. },
  112. },
  113. ],
  114. },
  115. gridOptions: {
  116. loading: false,
  117. height: '100%',
  118. columns: [
  119. {
  120. type: 'seq',
  121. width: 80,
  122. },
  123. {
  124. field: 'id',
  125. title: 'ID',
  126. },
  127. {
  128. field: 'slotVue',
  129. title: '插槽Vue写法',
  130. slots: {
  131. default: 'slotVue_default',
  132. },
  133. },
  134. {
  135. field: 'dictField',
  136. title: '字典',
  137. cellRender: {
  138. name: 'CellDict',
  139. },
  140. },
  141. {
  142. field: 'opt',
  143. title: '操作JSX写法',
  144. width: '120px',
  145. fixed: 'right',
  146. visible: true,
  147. slots: {
  148. default({ row, column }) {
  149. return (
  150. <a-button
  151. type='text'
  152. size='small'
  153. disabled={false}
  154. onClick={() => {
  155. handleEdit(row);
  156. }}
  157. >
  158. 编辑
  159. </a-button>
  160. );
  161. },
  162. },
  163. },
  164. {
  165. field: 'opt1',
  166. title: '操作Vue插槽写法',
  167. width: '120px',
  168. fixed: 'right',
  169. slots: {
  170. default: 'opt1_default',
  171. },
  172. },
  173. {
  174. field: 'name',
  175. title: 'JSX插槽渲染',
  176. visible: true,
  177. slots: {
  178. default({ row, column }) {
  179. return <span style={{ color: 'red' }}>{row.name}</span>;
  180. },
  181. },
  182. },
  183. {
  184. // fixed: 'right',
  185. cellRender: {
  186. name: 'CellOption',
  187. extraProps: {
  188. buttons: [
  189. {
  190. title: '新增',
  191. code: 'add',
  192. display: ({ row }) => {
  193. return row.name !== 'hanxiaohui0' ? DISPLAY_STATE.VISIBLE : DISPLAY_STATE.HIDDEN;
  194. },
  195. disabled({ row }) {
  196. return false;
  197. },
  198. onClick({ row }) {
  199. console.log('row', row);
  200. message.success(row.name);
  201. },
  202. extraProps: {},
  203. },
  204. {
  205. title: '禁用',
  206. code: 'disabled',
  207. display: ({ row }) => {
  208. return DISPLAY_STATE.VISIBLE;
  209. },
  210. disabled({ row }) {
  211. return true;
  212. },
  213. onClick({ row }) {},
  214. extraProps: {
  215. icon: h(BorderOuterOutlined),
  216. },
  217. },
  218. {
  219. title: '删除',
  220. code: 'delete',
  221. display: ({ row }) => {
  222. return DISPLAY_STATE.VISIBLE;
  223. },
  224. disabled({ row }) {
  225. return false;
  226. },
  227. onClick({ row }) {},
  228. extraProps: {
  229. danger: true,
  230. },
  231. },
  232. {
  233. title: '编辑',
  234. code: 'edit',
  235. display: ({ row }) => {
  236. return DISPLAY_STATE.VISIBLE;
  237. },
  238. disabled({ row }) {
  239. return false;
  240. },
  241. onClick({ row }) {},
  242. extraProps: {},
  243. },
  244. {
  245. title: '查看',
  246. code: 'view',
  247. display: ({ row }) => {
  248. return DISPLAY_STATE.VISIBLE;
  249. },
  250. disabled({ row }) {
  251. return false;
  252. },
  253. onClick({ row }) {},
  254. extraProps: {},
  255. },
  256. ],
  257. },
  258. },
  259. },
  260. ],
  261. },
  262. searchConfig: {
  263. fieldSpan: 6,
  264. fields: Array.from({ length: 10 }, (_, i) => {
  265. return {
  266. field: `name${i}`,
  267. label: `名称${i}`,
  268. component: 'a-input',
  269. componentProps: {
  270. placeholder: `请输入名称${i}`,
  271. },
  272. };
  273. }),
  274. },
  275. toolbarConfig: {
  276. displayToolbar: DISPLAY_STATE.VISIBLE,
  277. leftButtons: [
  278. {
  279. code: 'btn1',
  280. title: '左侧按钮',
  281. props: {
  282. type: 'info',
  283. disabled: false,
  284. onClick(event) {
  285. message.success('ok');
  286. },
  287. },
  288. },
  289. ],
  290. rightButtons: [
  291. {
  292. code: 'btn1',
  293. title: '右侧按钮',
  294. props: {
  295. type: 'info',
  296. disabled: false,
  297. onClick(event) {
  298. message.success('ok');
  299. },
  300. },
  301. },
  302. ],
  303. },
  304. // 每次查询接口之前,都会调用这个回调函数
  305. tableSearchBeforeBiz() {
  306. const searchParams = getValue('searchConfig.data');
  307. setValue('searchConfig.data', { ...searchParams, otherField: 'abc' });
  308. },
  309. // 表格初始化之前,只加载一次
  310. beforeMount() {
  311. console.log('表格加载前');
  312. },
  313. // 表格初始化完成,只加载一次
  314. mounted(gridRef) {
  315. console.log('表格加载后', gridRef, getValue('pagerConfig.total'));
  316. },
  317. tableSearchAfterBiz({gridRef}) {
  318. console.log('表格搜索后', gridRef, getValue('pagerConfig.total'));
  319. },
  320. async request(params) {
  321. console.log('params');
  322. return {
  323. list: Array.from({ length: 50 })
  324. .fill(1)
  325. .map((v, index) => ({ id: index })),
  326. total: 50,
  327. };
  328. },
  329. },
  330. });
  331. const openEditDrawer = () => {
  332. addOrEditDrawerRef.value.showDrawer({ type: 'add', row: null });
  333. };
  334. </script>
  335. <style scoped lang="scss">
  336. .sub-table-input {
  337. padding: 10px;
  338. }
  339. </style>