Skip to content

Table 表格

用于展示多条结构类似的数据, 可对数据进行排序、筛选、对比或其他自定义操作。

基本用法

使用 <ImTable> 包裹 <thead><tbody> 来构建表格。

姓名性别年龄操作
张三 120
张三 220
张三 320
张三 420
张三 520
张三 620
张三 720
张三 820
张三 920
张三 1020
vue
<script setup lang="ts">
import { ref, computed } from 'vue';

const checkedList: any = ref([]);
const isCheckAll = ref(false);
const indeterminate = computed(
  () =>
    checkedList.value.length > 0 &&
    list.length > checkedList.value.length &&
    !isCheckAll.value
);
const list: Array<number> = new Array(10).fill(null).map((_, i) => i + 1);

const onCheckAll = () => {
  if (isCheckAll.value) {
    checkedList.value = [...list];
  } else {
    checkedList.value = [];
  }
};
const onItemChange = () => {
  isCheckAll.value = checkedList.value.length === list.length;
};
</script>
<template>
  <ImTable>
    <thead>
      <tr>
        <th class="w_48">
          <ImCheckbox
            v-model="isCheckAll"
            :indeterminate="indeterminate"
            @change="onCheckAll" />
        </th>
        <th>姓名</th>
        <th>性别</th>
        <th>年龄</th>
        <th>操作</th>
      </tr>
    </thead>
    <tbody>
      <tr v-for="i in list">
        <td class="w_48">
          <ImCheckbox
            v-model="checkedList"
            label=""
            :value="i"
            @change="onItemChange" />
        </td>
        <td>张三 {{ i }}</td>
        <td>男</td>
        <td>20</td>
        <td>
          <ImButton>编辑</ImButton>
        </td>
      </tr>
    </tbody>
  </ImTable>
</template>

固定表头

<ImTable> 组件上设置 fixed-header 属性,可以固定表头。

姓名性别年龄操作
张三 120
张三 220
张三 320
张三 420
张三 520
张三 620
张三 720
张三 820
张三 920
张三 1020

基于 MIT 协议发布