RefTable.vue 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <template>
  2. <div class="p-4">
  3. <div class="mb-4">
  4. <a-button class="mr-2" @click="reloadTable"> 还原 </a-button>
  5. <a-button class="mr-2" @click="changeLoading"> 开启loading </a-button>
  6. <a-button class="mr-2" @click="changeColumns"> 更改Columns </a-button>
  7. <a-button class="mr-2" @click="getColumn"> 获取Columns </a-button>
  8. <a-button class="mr-2" @click="getTableData"> 获取表格数据 </a-button>
  9. <a-button class="mr-2" @click="getTableRawData"> 获取接口原始数据 </a-button>
  10. <a-button class="mr-2" @click="setPaginationInfo"> 跳转到第2页 </a-button>
  11. </div>
  12. <div class="mb-4">
  13. <a-button class="mr-2" @click="getSelectRowList"> 获取选中行 </a-button>
  14. <a-button class="mr-2" @click="getSelectRowKeyList"> 获取选中行Key </a-button>
  15. <a-button class="mr-2" @click="setSelectedRowKeyList"> 设置选中行 </a-button>
  16. <a-button class="mr-2" @click="clearSelect"> 清空选中行 </a-button>
  17. <a-button class="mr-2" @click="getPagination"> 获取分页信息 </a-button>
  18. </div>
  19. <BasicTable
  20. :canResize="false"
  21. title="RefTable示例"
  22. titleHelpMessage="使用Ref调用表格内方法"
  23. ref="tableRef"
  24. :api="demoListApi"
  25. :columns="columns"
  26. rowKey="id"
  27. :rowSelection="{ type: 'checkbox' }"
  28. />
  29. </div>
  30. </template>
  31. <script lang="ts" setup>
  32. import { ref, unref } from 'vue';
  33. import { BasicTable, TableActionType } from '@/components/Table';
  34. import { getBasicColumns, getBasicShortColumns } from './tableData';
  35. import { useMessage } from '@/hooks/web/useMessage';
  36. import { demoListApi } from '@/api/demo/table';
  37. import { type Nullable } from '@vben/types';
  38. const tableRef = ref<Nullable<TableActionType>>(null);
  39. const { createMessage } = useMessage();
  40. const columns = getBasicColumns();
  41. function getTableAction() {
  42. const tableAction = unref(tableRef);
  43. if (!tableAction) {
  44. throw new Error('tableAction is null');
  45. }
  46. return tableAction;
  47. }
  48. function changeLoading() {
  49. getTableAction().setLoading(true);
  50. setTimeout(() => {
  51. getTableAction().setLoading(false);
  52. }, 1000);
  53. }
  54. function changeColumns() {
  55. getTableAction().setProps({
  56. columns: getBasicShortColumns(),
  57. rowSelection: {
  58. type: 'checkbox',
  59. },
  60. showIndexColumn: true,
  61. });
  62. }
  63. function reloadTable() {
  64. getTableAction().setProps({
  65. columns: getBasicColumns(),
  66. rowSelection: {
  67. type: 'checkbox',
  68. },
  69. showIndexColumn: true,
  70. });
  71. getTableAction().reload({
  72. page: 1,
  73. });
  74. }
  75. function getColumn() {
  76. createMessage.info('请在控制台查看!');
  77. console.log(getTableAction().getColumns());
  78. }
  79. function getTableData() {
  80. createMessage.info('请在控制台查看!');
  81. console.log(getTableAction().getDataSource());
  82. }
  83. function getTableRawData() {
  84. createMessage.info('请在控制台查看!');
  85. console.log(getTableAction().getRawDataSource());
  86. }
  87. function getPagination() {
  88. createMessage.info('请在控制台查看!');
  89. console.log(getTableAction().getPaginationRef());
  90. }
  91. function setPaginationInfo() {
  92. getTableAction().setPagination({
  93. current: 2,
  94. });
  95. getTableAction().reload();
  96. }
  97. function getSelectRowList() {
  98. createMessage.info('请在控制台查看!');
  99. console.log(getTableAction().getSelectRows());
  100. }
  101. function getSelectRowKeyList() {
  102. createMessage.info('请在控制台查看!');
  103. console.log(getTableAction().getSelectRowKeys());
  104. }
  105. function setSelectedRowKeyList() {
  106. getTableAction().setSelectedRowKeys(['0', '1', '2']);
  107. }
  108. function clearSelect() {
  109. getTableAction().clearSelectedRowKeys();
  110. }
  111. </script>