表格

用于展示多条结构类似的数据。本组件基于 View Design Tableopen in new window 封装。

基础用法

table-data 为数据,table-column 为列。

<template>
  <ui-table :table-columns="tableColumns" :table-data="tableData" />
</template>

<script setup>
  import { ref } from 'vue';

  const tableColumns = ref([
    {
      title: 'Name',
      key: 'name',
    },
    {
      title: 'Age',
      key: 'age',
    },
    {
      title: 'Address',
      key: 'address',
    },
  ]);

  const tableData = ref([
    {
      name: 'John Brown',
      age: 18,
      address: 'New York No. 1 Lake Park',
      date: '2016-10-03',
    },
    {
      name: 'Jim Green',
      age: 24,
      address: 'London No. 1 Lake Park',
      date: '2016-10-01',
    },
    {
      name: 'Joe Black',
      age: 30,
      address: 'Sydney No. 1 Lake Park',
      date: '2016-10-02',
    },
    {
      name: 'Jon Snow',
      age: 26,
      address: 'Ottawa No. 2 Lake Park',
      date: '2016-10-04',
    },
  ]);
</script>

插槽传入

tableColumn 中设置某一列 slot 的值,在 ui-table 中传入对应插槽为 slot 的值即可。slot 的参数有 3 个:当前行数据 row,当前列数据 column,当前行序号 index

<template>
  <ui-table :table-columns="tableColumns" :table-data="tableData">
    <template #address="{ row }">
      <span>{{ row.name }} is {{ row.address }}</span>
    </template>
  </ui-table>
</template>

<script setup>
  import { ref } from 'vue';

  const tableColumns = ref([
    {
      title: 'Name',
      key: 'name',
    },
    {
      title: 'Age',
      key: 'age',
    },
    {
      title: 'Address',
      slot: 'address',
    },
  ]);

  const tableData = ref([
    {
      name: 'John Brown',
      age: 18,
      address: 'New York No. 1 Lake Park',
    },
    {
      name: 'Jim Green',
      age: 24,
      address: 'London No. 1 Lake Park',
    },
    {
      name: 'Joe Black',
      age: 30,
      address: 'Sydney No. 1 Lake Park',
    },
    {
      name: 'Jon Snow',
      age: 26,
      address: 'Ottawa No. 2 Lake Park',
    },
  ]);
</script>

设置最大高度

设置 max-height 超出高度将滚动。

<template>
  <Button class="mb-sm" @click="add">添加数据</Button>
  <ui-table
    :table-columns="tableColumns"
    :table-data="tableData"
    :max-height="300"
    :table-scroll="true"
  />
</template>

<script setup>
  import { reactive, ref } from 'vue';

  const tableColumns = ref([
    {
      title: 'Name',
      key: 'name',
    },
    {
      title: 'Age',
      key: 'age',
    },
    {
      title: 'Address',
      key: 'address',
    },
  ]);

  const tableData = reactive([
    {
      name: 'John Brown',
      age: 18,
      address: 'New York No. 1 Lake Park',
      date: '2016-10-03',
    },
    {
      name: 'Jim Green',
      age: 24,
      address: 'London No. 1 Lake Park',
      date: '2016-10-01',
    },
    {
      name: 'Joe Black',
      age: 30,
      address: 'Sydney No. 1 Lake Park',
      date: '2016-10-02',
    },
  ]);

  const add = () => {
    tableData.push({
      name: 'Jon Snow',
      age: 26,
      address: 'Ottawa No. 2 Lake Park',
      date: '2016-10-04',
    });
  };
</script>

根据上下文自适应高度

需要设置其父级元素 height: 100%; display: flex; flex-direction: column; flex: 1;

ui-table 设置 display: flex; flex-direction: column; flex: 1;

此处已经通过公用 class 设置 css

4条记录
  • 1
  • 请选择
  • 共1页
  • 跳至
<template>
  <div class="content">
    <div class="height-full auto-fill">
      <div class="mb-sm">
        <ui-label label="设备名称">
          <el-input class="width-sm" />
        </ui-label>
      </div>
      <ui-table class="auto-fill" :table-columns="tableColumns" :table-data="tableData" />
      <ui-page :total-count="tableData.length" />
    </div>
  </div>
</template>

<script setup>
  import { ref } from 'vue';

  const tableColumns = ref([
    {
      title: 'Name',
      key: 'name',
    },
    {
      title: 'Age',
      key: 'age',
    },
    {
      title: 'Address',
      key: 'address',
    },
  ]);

  const tableData = ref([
    {
      name: 'John Brown',
      age: 18,
      address: 'New York No. 1 Lake Park',
      date: '2016-10-03',
    },
    {
      name: 'Jim Green',
      age: 24,
      address: 'London No. 1 Lake Park',
      date: '2016-10-01',
    },
    {
      name: 'Joe Black',
      age: 30,
      address: 'Sydney No. 1 Lake Park',
      date: '2016-10-02',
    },
    {
      name: 'Jon Snow',
      age: 26,
      address: 'Ottawa No. 2 Lake Park',
      date: '2016-10-04',
    },
  ]);
</script>
<style lang="scss" scoped>
  .content {
    height: 500px;
  }
</style>

翻页选中

全选所有页传入 is-alltrue 即可,全选所有页之后禁止取消选中单项。需要配合 reserve-selection 属性一起使用,设置为 true 将启用翻页勾选,row-key 默认值为 id 作为翻页选中的标识符。

0条记录
  • 1
  • 请选择
  • 共1页
  • 跳至
<template>
  <el-checkbox v-model="checkAll">全选</el-checkbox>
  <ui-table
    row-key="age"
    :is-all="checkAll"
    :reserve-selection="true"
    :table-columns="tableColumns"
    :table-data="tableData"
  />
  <ui-page
    v-model:pageSize="pageData.pageSize"
    v-model:pageNum="pageData.pageNum"
    :total-count="pageData.totalCount"
  />
</template>

<script setup>
  import { onMounted, reactive, ref, watch } from 'vue';
  import { pagination } from '@web-basic-doc/utils';

  let checkAll = ref(false);
  const tableColumns = ref([
    {
      type: 'selection',
      width: 60,
    },
    {
      title: 'Name',
      key: 'name',
    },
    {
      title: 'Age',
      key: 'age',
    },
    {
      title: 'Address',
      key: 'address',
    },
  ]);

  const data = reactive([
    {
      name: 'John Brown',
      age: 1,
      address: 'New York No. 1 Lake Park',
      date: '2016-10-03',
    },
    {
      name: 'Jim Green',
      age: 2,
      address: 'London No. 1 Lake Park',
      date: '2016-10-01',
    },
    {
      name: 'Joe Black',
      age: 3,
      address: 'Sydney No. 1 Lake Park',
      date: '2016-10-02',
    },
    {
      name: 'Jon Snow',
      age: 4,
      address: 'Ottawa No. 2 Lake Park',
      date: '2016-10-04',
    },
    {
      name: 'John Brown',
      age: 5,
      address: 'New York No. 1 Lake Park',
      date: '2016-10-03',
    },
    {
      name: 'Jim Green',
      age: 6,
      address: 'London No. 1 Lake Park',
      date: '2016-10-01',
    },
    {
      name: 'Joe Black',
      age: 7,
      address: 'Sydney No. 1 Lake Park',
      date: '2016-10-02',
    },
    {
      name: 'Jon Snow',
      age: 8,
      address: 'Ottawa No. 2 Lake Park',
      date: '2016-10-04',
    },
    {
      name: 'John Brown',
      age: 9,
      address: 'New York No. 1 Lake Park',
      date: '2016-10-03',
    },
    {
      name: 'Jim Green',
      age: 10,
      address: 'London No. 1 Lake Park',
      date: '2016-10-01',
    },
    {
      name: 'Joe Black',
      age: 11,
      address: 'Sydney No. 1 Lake Park',
      date: '2016-10-02',
    },
    {
      name: 'Jon Snow',
      age: 12,
      address: 'Ottawa No. 2 Lake Park',
      date: '2016-10-04',
    },
  ]);

  const pageData = reactive({
    pageNum: 1,
    pageSize: 10,
    totalCount: 0,
  });

  let tableData = ref([]);

  watch(
    () => pageData.pageNum,
    (val) => {
      tableData.value = pagination(data, val, pageData.pageSize);
    },
  );

  watch(
    () => pageData.pageSize,
    (val) => {
      pageData.pageNum = 1;
      tableData.value = pagination(data, pageData.pageNum, val);
    },
  );

  onMounted(() => {
    pageData.totalCount = data.length;
    tableData.value = pagination(data, pageData.pageNum, pageData.pageSize);
  });
</script>

属性

属性名说明类型可选值默认值
table-columns表格列的配置描述array————
table-data显示的结构化数据array————
loading加载中boolean——false
max-height最大高度number————
reserve-selection是否开启翻页选中boolean——false
row-key配合 reserve-selection 使用,作为翻页选中的标识符string——id
default-storeData配合 reserve-selection 使用,默认选中的数据array————
is-all配合 reserve-selection 使用,全选所有页boolean——false
special-jsx空数据显示的内容string————
special-jsx空数据显示的内容string————
透传属性View Design Tableopen in new window——————

事件

事件名说明参数
cancelSelectTable取消选中某一项selection 已选项数据 row 取消选择的数据
storeSelectList配合 reserve-selection 使用storeSelectTable 翻页选中的数据
cacelAllSelectTable点击取消全选时触发selection 已选项数据
onSelectAllTable点击全选时触发selection 已选项数据
oneSelected选中某一项selection 已选项数据 row 刚选择的项数据
透传事件View Design Tableopen in new window——

插槽

名称说明
——对应 table-columns 单个项中 slot 的值
上次更新:
贡献者: zml