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