# 表格

name状态key性别AgeAddressTags
2222222ss1100
1xxx
323233223323New No. 1 Lake Parknicedeveloper34
wwe1200
1xxx
32New No. 1 Lake Parknicedeveloper67
wew1300
1xxx
32New No. 1 Lake Parknicedeveloper23
dff1400
1xxx
32New No. 1 Lake Parknicedeveloper3456
John0500
0xxx
32New No. 1 Lake Parknicedeveloper12
Jim1600
1xxx
42London No. 1 Lake Parkloser234
Joe0700
0xxx
32Sidney No. 1 Lake Parkcoolteacher345
name状态
2222222ss1
wwe1
wew1
dff1
John0
Jim1
Joe0
34
67
23
3456
12
234
345

# 案例

<template>
  <div>
    <b-table
      rowKey="idd"
      :bordered="true"
      :sourceData="sourceData"
      :columns="columns"
      :rowSelection="rowSelection"
      :scroll="{ x: 900, y: 300 }"
    ></b-table>
  </div>
</template>

<script>
export default {
  data() {
    return {
      // row: { b: 32 },
      // objabc: { a: 1 },
      defaultCheckedList: [200],
      defaultDisabledList: [100, 200],
      sourceData: [
        {
          key: 100,
          idd: 100,
          name: "2222222ss",
          ellipsis: true,
          status: "1",
          age: 323233223323,
          money: 34,
          address: "New No. 1 Lake Park",
          tags: ["nice", "developer"],
          sex: "男",
        },
        {
          key: 200,
          idd: 200,
          name: "wwe",
          status: "1",
          age: 32,
          money: 67,
          address: "New No. 1 Lake Park",
          tags: ["nice", "developer"],
          sex: "男",
        },
        {
          key: 300,
          idd: 300,
          name: "wew",
          status: "1",
          age: 32,
          money: 23,
          address: "New No. 1 Lake Park",
          tags: ["nice", "developer"],
          sex: "男",
        },
        {
          key: 400,
          idd: 400,
          name: "dff",
          status: "1",
          age: 32,
          money: 3456,
          address: "New No. 1 Lake Park",
          tags: ["nice", "developer"],
          sex: "男",
        },
        {
          key: 500,
          idd: 500,
          name: "John",
          status: "0",
          age: 32,
          address: "New No. 1 Lake Park",
          sex: "男",
          money: 12,

          tags: ["nice", "developer"],
        },
        {
          key: 600,
          idd: 600,
          name: "Jim",
          status: "1",
          age: 42,
          sex: "女",
          money: 234,

          address: "London No. 1 Lake Park",
          tags: ["loser"],
        },
        {
          key: 700,
          idd: 700,
          age: 32,
          status: "0",
          name: "Joe",
          address: "Sidney No. 1 Lake Park",
          sex: "男",
          money: 345,
          tags: ["cool", "teacher"],
        },
      ],

      rowSelection: {
        type: "checkbox",
        getCheckboxProps: (row) => {
          const key = row[this.getRowKey(row)];
          const prop = {
            defaultChecked: this.defaultCheckedList.indexOf(key) !== -1,
            disabled: this.defaultDisabledList.indexOf(key) !== -1,
          };
          return prop;
        },
      },

      columns: [
        {
          // fixed: "left",
          width: "100",
          title: "key",
          dataIndex: "idd", // 列数据在数据项中对应的key
          align: "center",
          // key: "key", //v-for遍历columns时的key,如果设置了唯一的dataIndex可忽略该属性
        },
        {
          fixed: "right",
          width: "100",
          title: "钱",
          dataIndex: "money",
          align: "center",
          // key: "money",
        },
        {
          // fixed: "left",
          // fixed: "right",
          // width: '120',
          title: "性别",
          dataIndex: "sex",
          align: "center",
          // key: "sex",
          // slots: { title: "customTitle" },
          // scopedSlots: { customRender: "name" },
          render(h, row) {
            // return <span>{row.status}</span>;
            // return <h-switch></h-switch>;
            /**
             * 下面的代码会解析成:h("div", [h("span", [row, "xxx"])])
             * 即h函数的第二个参数是数组,里面又嵌套了一个h函数,这个h函数的第二个参数是数组,
             * 数组里面的第一个参数row是对象,是不能通过h函数转成vnode的,会渲染成undefined,
             * 数组里面的第二个参数是数字,可以通过h函数转成vnode。
             */
            const obja = { ad: 323, obj: { dsg: 31 } };
            return (
              <div>
                <b>{row.status}xxx</b>
              </div>
            );
          },
        },
        {
          fixed: true,
          // fixed: "left",
          // ellipsis: true,
          width: "100",
          title: "name",
          dataIndex: "name",
          align: "center",
          // key: "name",
        },
        {
          fixed: true,
          // fixed: "right",
          width: "100",
          title: "状态",
          dataIndex: "status",
          // align: "right",
          // key: "status",
          // render: (h, row) => {
          //   console.log(row, "3434", 9132999);
          //   // return <div style="">{row.status}</div>;
          //   // return <div style="height:100px">{row.status}</div>;
          //   // return <h-switch></h-switch>;
          //   return <div>{row.status}</div>;
          // }
        },
        {
          // fixed: "right",
          width: "100",
          title: "Age",
          dataIndex: "age",
          // key: "age",
        },
        {
          width: "120",
          title: "Address",
          dataIndex: "address",
          // key: "address",
        },
        {
          // fixed: "left",
          width: "100",
          title: "Tags",
          // key: "tags",
          dataIndex: "tags",
          // render: (h, row) => {
          //   // console.log(row.name);
          //   // return h("div", {}, row.name);
          //   // return <div>{row.name}</div>;
          //   return <span>234</span>;
          // }
        },
        // {
        //   title: "Action",
        //   key: "action",
        //   scopedSlots: { customRender: "action" },
        // },
      ],
      theme: {
        default: {
          sss: 123,
          sgg: 3425,
        },
        default1: {
          sss: 3545,
          sgg: 9789,
        },
      },
      obj: {
        name: "hss",
        age: 22,
        sex: "男",
      },
      visible: false,
    };
  },
  computed: {},
  created() {},
  mounted() {
    Object.keys(this.theme).forEach((item) => {
      console.log(this.theme[item]);
    });
  },
  methods: {
    getColumnKey(column) {
      // columns的v-for时的key
      const columnKey = column.key || column.dataIndex;
      return columnKey;
    },
    // 获取对data-source的v-for时的key
    getRowKey(row) {
      // data-source的v-for时的key
      // console.log("columns的v-for时的key", row);
      const rowKey = this.rowKey || (row.key && "key");
      return rowKey;
    },
    clickSwitch(v) {
      console.log(v);
    },
    xxx() {
      console.log("234324");
    },
    changeModal() {
      this.visible = true;
    },
    visibleChange(v) {
      console.log("visibleChange", v);
    },
    cancelClick() {
      console.log("cancelClick,app组件里modal的cancel回调");
    },
    confirmClick() {
      console.log("cancelClick,app组件里modal的confirm回调");
    },
    closeClick() {
      console.log("closeClick,app组件里modal的close回调");
    },
  },
};
</script>
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
273
274
275
276
277
278
279
280

# API

# table

参数 说明 类型 默认值
bordered 是否展示外边框和列边框 boolean false
columns 表格列的配置描述 array -
dataSource 数据数组 any[] -
scroll 设置横向或纵向滚动 { x: number |true, y: number } -
rowKey 表格行 key 的取值 string 'key'
rowSelection 列表项是否可选择 object null

# columns

参数 说明 类型 默认值
align 设置列内容的对齐方式 'left' |'right' |'center' 'left'
dataIndex 列数据在数据项中对应的 key String
ellipsis 超过宽度将自动省略 boolean false
fixed 列是否固定,可选 true(等效于 left) 'left' 'right' boolean|string false
key Vue 需要的 key,如果已经设置了唯一的 dataIndex,可以忽略这个属性 string -
render 通过 render 函数设置列内容 Function(h,row) {} -
title 列头显示文字 string -
width 列宽度 number -

# rowSelection

参数 说明 类型 默认值
getCheckboxProps 选择框的默认属性配置,可配置默认选中以及默认禁用 Function(row) -
onChange 选中项发生变化时的回调 Function(newSelectedRowKeys, newSelectedRows,oldSelectedRowKeys,oldSelectedRows) {} -
onSelect 用户手动选择/取消选择某列的回调 Function(row, isSelected, selectedRows, event) {} -
onSelectAll 用户手动选择/取消选择所有列的回调 Function(isAll,oldSelectRows,nowSelectedRows,changeRows) {} -

# 注意

  1. 在 Table 中,dataSourcecolumns 里的数据值都需要指定 key 值。对于 dataSource 默认将每列数据的 key 属性作为唯一的标识,如果你的数据没有这个属性,务必使用 rowKey 来指定数据列的主键。若没有指定,控制台会出现缺少 key 的提示,表格组件也会出现各类奇怪的错误。
  2. 设置固定列时候,建议设置 width 属性,否则会有宽度问题(antdv 官方的也有此问题)。
  3. 固定头和列的时候,若列头与内容不对齐,请指定固定列的宽度 width。如果指定 width 不生效或出现白色垂直空隙,请尝试建议留一列不设宽度以适应弹性布局。(ps:如果全部 column 都设置了 width,且设置了 scroll.x,如果 scroll.x 大于所有 column 的和,就会出现白色空隙,这时候就留一列不设 width 即可。)