# Table 表格

TIP

该组件是基于vxe-table (opens new window)二次封装,主要用于项目中列表分页功能,简化处理分页逻辑部分代码, 当然你也可以当作普通表格使用。

# 使用

表格配置项参考 vxe-table全局参数 (opens new window)

import Vue from "vue";
import VueWidget from "@vensst/vue-widget";
// 全部引入及配置
Vue.use(VueWidget, {
  table: {
    components: ["Button"], // 按需引入vxe-table里组件
    // components:'all', // 为 all 则会全部引入
    options: {}, // 表格配置项 参考 https://vxetable.cn/v3/#/table/start/global
  },
});

// 按需引入及配置
// Vue.use(VueWidget.Table, {
//   components: ["Button"], // 按需引入vxe-table里组件
//   // components:'all', // 为 all 则会全部引入
//   options: {}, // 表格配置项 参考 https://vxetable.cn/v3/#/table/start/global
// });
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

<template>
  <div>
    <VenTable
        :tableDataApi="listApi"
        :formData="formData"
        :tableColumn="tableColumn"
        :rowConfig="{ isCurrent: false, isHover: true }"
        :stripe="true"
        :edit-config="{ trigger: 'click', mode: 'cell' }"
        :pageFieldOptions="{
        dataPath: 'data',
        data: 'records',
      }"
        :mergeRowFields="['age']"
        @row-click="cellClickEvent"
    >
      <template #age_edit="{ row }">
        <vxe-input v-model="row.age"></vxe-input>
      </template>
      <template #operation="scope">
        <vxe-button @click="getRow(scope)">按钮</vxe-button>
        <vxe-button @click="getRow(scope)">按钮</vxe-button>
      </template>
    </VenTable>
    <VenTable
        :tableData="tableData"
        :tableColumn="tableColumn"
        ref="tableRef"
        :tree-config="{
        transform: true,
        rowField: 'id',
        parentField: 'parentId',
      }"
        :edit-config="{ trigger: 'click', mode: 'cell' }"
        :isPage="false"
    >
      <template #age_edit="{ row }">
        <vxe-input v-model="row.age"></vxe-input>
      </template>
      <template #operation="scope">
        <vxe-button @click="getRow(scope)">按钮</vxe-button>
        <vxe-button @click="getRow(scope)">按钮</vxe-button>
      </template>
    </VenTable>
  </div>
</template>

<script>
  const {random} = require("@vensst/js-toolkit");

  // 模拟分页请求
  const listApi = function ({size = 10, current = 1}) {
    return new Promise((resolve) => {
      let arr = [];
      for (let i = 0; i < 100; i++) {
        arr.push({
          id: i,
          name: "会飞的鱼" + i,
          name2: "会飞的鱼" + i,
          name3: "会飞的鱼" + i,
          age: random(28, 30, false),
          contact1: "会跑的鱼" + random(0, 5, false),
          contact2: "会跳的鱼" + random(0, 5, false),
        });
      }
      setTimeout(() => {
        resolve({
          data: {
            records: arr.slice((current - 1) * size, (current - 1) * size + size),
            total: arr.length,
            size: size,
            current: current,
            pages:
                arr.length % size === 0 ? arr.length / size : arr.length / size + 1,
          },
          // data: {
          //   records: [],
          //   total: arr.length,
          //   size: size,
          //   current: current,
          //   pages: arr.length % size === 0 ? arr.length / size : arr.length / size + 1,
          // },
        });
      }, Math.floor(Math.random() * 1000));
    });
  };

  export default {
    name: "Table",
    title: "表格",
    components: {},
    mixins: [],
    props: {},
    data() {
      return {
        formData: {
          cjmc: "",
          cjbs: "",
          dateTime: [],
        },
        listApi,
        tableColumn: [
          {
            type: "checkbox",
            width: 60,
          },
          {
            type: "seq",
            title: "序号",
            width: 60,
          },
          {
            field: "name",
            title: "姓名",
            minWidth: 160,
            "show-overflow": "ellipsis",
            "tree-node": true,
          },
          {
            field: "name2",
            title: "姓名2",
            minWidth: 160,
            formatter: function ({cellValue}) {
              return cellValue + 66;
            },
          },
          {
            field: "name3",
            title: "姓名3",
            minWidth: 160,
            "show-overflow": "title",
          },
          {
            field: "age",
            title: "年龄",
            minWidth: 100,
            sortable: true,
            slots: {
              edit: "age_edit",
            },
            "edit-render": {},
          },
          {
            title: "紧急联系人",

            children: [
              {
                field: "contact1",
                title: "名称3",
                minWidth: 120,
              },
              {
                field: "contact2",
                title: "名称4",
                minWidth: 120,
              },
            ],
          },
          {
            title: "操作",
            width: 160,
            fixed: "right",
            slots: {
              default: "operation",
            },
          },
        ],
        tableData: [
          {
            id: "1",
            parentId: null,
            name: "张**",
            name2: "张**",
            name3: "张**",
            age: 26,
            contact1: "王**",
            contact2: "李**",
          },
          {
            id: "11",
            parentId: "1",
            name: "张**",
            name2: "张**",
            name3: "张**",
            age: 26,
            contact1: "王**",
            contact2: "李**",
          },
          {
            id: "12",
            parentId: "1",
            name: "张**",
            name2: "张**",
            name3: "张**",
            age: 26,
            contact1: "王**",
            contact2: "李**",
          },
          {
            id: "2",
            parentId: null,
            name: "李**",
            name2: "李**",
            name3: "李**",
            age: 24,
            contact1: "王**",
            contact2: "李**",
          },
          {
            id: "21",
            parentId: "2",
            name: "李**",
            name2: "李**",
            name3: "李**",
            age: 24,
            contact1: "王**",
            contact2: "李**",
          },
          {
            id: "5",
            parentId: null,
            name: "溢出省略,李**李**李**李**李**李**李**李**李**李**李**李**李**",
            name2: "换行,李**李**李**李**李**李**李**李**李**李**李**李**李**",
            name3:
                "溢出省略,鼠标悬浮显示tooltip,李**李**李**李**李**李**李**李**李**李**李**李**李**",
            age: 24,
            contact1: "王**",
            contact2: "李**",
          },
          {
            id: "3",
            parentId: null,
            name: "王**",
            name2: "王**",
            name3: "王**",
            age: 28,
            contact1: "王**",
            contact2: "李**",
          },
          {
            id: "4",
            parentId: null,
            name: "赵**",
            name2: "赵**",
            name3: "赵**",
            age: 28,
            contact1: "王**",
            contact2: "李**",
          },
        ],
      };
    },
    computed: {},
    watch: {},
    created() {
    },
    mounted() {
    },
    methods: {
      cellClickEvent(e) {
        console.log("点击行", e);
      },
      getRow(scope) {
        console.log("点击操作按钮:", scope);
      },
    },
  };
</script>

<style scoped></style>

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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
显示代码 复制代码

# 自定义主题

与官网一致,你可以通过修改 scss 变量来自定义主题,具体参考vxe-table 主题 (opens new window)

// 修改scss变量
$vxe-font-size: 14px;
$vxe-font-color: #666;
$vxe-primary-color: #409eff;
$vxe-table-font-color: $vxe-font-color;
$vxe-table-border-color: #e8eaec;
$vxe-table-border-radius: 4px;
// ...

@import "@vensst/vue-widget/src/styles/table.scss";
1
2
3
4
5
6
7
8
9
10

# 属性

除以下属性你还可以使用vxe-table 插件 API 中的 vxe-table 的所有 Props (opens new window)

参数 说明 类型 可选值 默认值
isPage 是否显示分页 Boolean -- true
tableDataApi 获取表格数据接口函数 Function -- --
formData 表单数据(接口参数) Object -- --
tableData 表格数据 Array -- --
tableColumn 表格列配置,参考vxe-column (opens new window)属性 Array -- --
mergeRowFields 合并行字段 Array<string> -- --
pageFieldOptions 分页组件字段配置,参考如下 Object -- --
pageOptions 分页组件配置,参考如下 Object -- --

# pageFieldOptions

参数 说明 类型 可选值 默认值
dataPath 数据路径,例如:'a.b' String -- --
data 数据字段 String -- data
total 总数字段 String -- total
size 每页条数字段 String -- size
current 当前页字段 String -- current
pages 总页数字段 String -- pages
defaultCurrentPage 默认当前页值 Number -- 1
defaultSize 默认分页大小值 Number -- 10

# pageOptions

除以下属性你还可以使用vxe-table 插件 API 中的 vxe-pager 的所有 Props (opens new window)

参数 说明 类型 可选值 默认值
background 背景颜色 String -- --
layouts 自定义布局 Array -- ["PrevPage", "JumpNumber", "NextPage", "FullJump", "Sizes", "Total"],

# 插槽

name 说明
empty 自定义表格空数据时显示模板
loading 自定义表格加载中模板
left 自定义分页左侧模板
right 自定义分页右侧模板