这是用户在 2024-5-20 14:13 为 https://www.surely.cool/doc/tree#expand-children 保存的双语快照页面,由 沉浸式翻译 提供双语支持。了解如何保存?
Jim Green
Joe Black
John Brown

My name is John Brown, I am 32 years old, living in New York No. 1 Lake Park.

My name is Jim Green, I am 42 years old, living in London No. 1 Lake Park.

42
London No. 1 Lake Park
32
Sidney No. 1 Lake Park
32
New York No. 1 Lake Park
可展开

当表格内容较多不能一次性完全展示时。

TSexpand codeexpand code
<template>
  <s-table :columns="columns" :data-source="data" :scroll="{ x: 2000 }">
    <template #bodyCell="{ column, text }">
      <template v-if="column.key === 'action'">
        <a>Delete</a>
      </template>
      <template v-else>{{ text }}</template>
    </template>
    <template #expandedRowRender="{ record }">
      <p style="margin: 0">
        {{ record.description }}
      </p>
    </template>
  </s-table>
</template>
<script lang="ts">
import { defineComponent } from 'vue';

export default defineComponent({
  setup() {
    const columns = [
      { title: 'Name', dataIndex: 'name', key: 'name', fixed: true },
      { title: 'Age', dataIndex: 'age', key: 'age' },
      { title: 'Address', dataIndex: 'address', key: 'address' },
      { title: 'Action', key: 'action' },
    ];

    const data = [
      {
        key: 1,
        name: 'John Brown',
        age: 32,
        address: 'New York No. 1 Lake Park',
        description:
          'My name is John Brown, I am 32 years old, living in New York No. 1 Lake Park.',
      },
      {
        key: 2,
        name: 'Jim Green',
        age: 42,
        address: 'London No. 1 Lake Park',
        description: 'My name is Jim Green, I am 42 years old, living in London No. 1 Lake Park.',
      },
      {
        key: 3,
        name: 'Joe Black',
        age: 32,
        address: 'Sidney No. 1 Lake Park',
        description: 'My name is Joe Black, I am 32 years old, living in Sidney No. 1 Lake Park.',
      },
    ];
    return {
      data,
      columns,
    };
  },
});
</script>
CheckStrictly:
Joe Black
32
Sidney No. 1 Lake Park
John Brown sr.
60
New York No. 1 Lake Park
John Brown jr.
30
New York No. 3 Lake Park
Jim Green
42
London No. 2 Lake Park
Jim Green sr.
72
London No. 1 Lake Park
Jimmy Green sr.
18
London No. 4 Lake Park
Jim Green jr.
25
London No. 3 Lake Park
John Brown
42
New York No. 2 Lake Park
树形数据展示

表格支持树形数据的展示,当数据中有 children 字段时会自动展示为树形表格,如果不需要或配置为其他字段可以用 childrenColumnName 进行配置。 可以通过设置 indentSize 以控制每一层的缩进宽度。

TSexpand codeexpand code
<template>
  <a-space align="center" style="margin-bottom: 16px">
    CheckStrictly:
    <a-switch v-model:checked="rowSelection.checkStrictly"></a-switch>
  </a-space>
  <s-table
    :columns="columns"
    :data-source="data"
    :row-selection="rowSelection"
    expand-row-by-click
  />
</template>
<script lang="ts">
import { defineComponent, ref } from 'vue';

interface DataItem {
  key: number;
  name: string;
  age: number;
  address: string;
  children?: DataItem[];
}

export default defineComponent({
  setup() {
    const columns = [
      {
        title: 'Name',
        dataIndex: 'name',
        key: 'name',
      },
      {
        title: 'Age',
        dataIndex: 'age',
        key: 'age',
        width: '12%',
      },
      {
        title: 'Address',
        dataIndex: 'address',
        width: '30%',
        key: 'address',
      },
    ];
    const data: DataItem[] = [
      {
        key: 1,
        name: 'John Brown sr.',
        age: 60,
        address: 'New York No. 1 Lake Park',
        children: [
          {
            key: 11,
            name: 'John Brown',
            age: 42,
            address: 'New York No. 2 Lake Park',
          },
          {
            key: 12,
            name: 'John Brown jr.',
            age: 30,
            address: 'New York No. 3 Lake Park',
            children: [
              {
                key: 121,
                name: 'Jimmy Brown',
                age: 16,
                address: 'New York No. 3 Lake Park',
              },
            ],
          },
          {
            key: 13,
            name: 'Jim Green sr.',
            age: 72,
            address: 'London No. 1 Lake Park',
            children: [
              {
                key: 131,
                name: 'Jim Green',
                age: 42,
                address: 'London No. 2 Lake Park',
                children: [
                  {
                    key: 1311,
                    name: 'Jim Green jr.',
                    age: 25,
                    address: 'London No. 3 Lake Park',
                  },
                  {
                    key: 1312,
                    name: 'Jimmy Green sr.',
                    age: 18,
                    address: 'London No. 4 Lake Park',
                  },
                ],
              },
            ],
          },
        ],
      },
      {
        key: 2,
        name: 'Joe Black',
        age: 32,
        address: 'Sidney No. 1 Lake Park',
      },
    ];

    const rowSelection = ref({
      checkStrictly: false,
      onChange: (selectedRowKeys: (string | number)[], selectedRows: DataItem[]) => {
        console.log(`selectedRowKeys: ${selectedRowKeys}`, 'selectedRows: ', selectedRows);
      },
      onSelect: (record: DataItem, selected: boolean, selectedRows: DataItem[]) => {
        console.log(record, selected, selectedRows);
      },
      onSelectAll: (selected: boolean, selectedRows: DataItem[], changeRows: DataItem[]) => {
        console.log(selected, selectedRows, changeRows);
      },
    });
    return {
      data,
      columns,
      rowSelection,
    };
  },
});
</script>
Screem 2
iOS
10.3.4.5654
500
Jack
2014-12-24 23:12:00
Screem 3
iOS
10.3.4.5654
500
Jack
2014-12-24 23:12:00
Screem 1
iOS
10.3.4.5654
500
Jack
2014-12-24 23:12:00
2014-12-24 23:12:00
This is production name 2
Finished
Upgraded: 56
2014-12-24 23:12:00
This is production name 3
Finished
Upgraded: 56
2014-12-24 23:12:00
This is production name 1
Finished
Upgraded: 56
2014-12-24 23:12:00
This is production name 2
Finished
Upgraded: 56
2014-12-24 23:12:00
This is production name 3
Finished
Upgraded: 56
2014-12-24 23:12:00
This is production name 1
Finished
Upgraded: 56
2014-12-24 23:12:00
This is production name 2
Finished
Upgraded: 56
2014-12-24 23:12:00
This is production name 3
Finished
Upgraded: 56
2014-12-24 23:12:00
This is production name 1
Finished
Upgraded: 56
嵌套子表格

展示每行数据更详细的信息。

TSexpand codeexpand code
<template>
  <s-table :columns="columns" :data-source="data" class="components-table-demo-nested">
    <template #bodyCell="{ column, text }">
      <template v-if="column.key === 'operation'">
        <a>Publish</a>
      </template>
      <template v-else>{{ text }}</template>
    </template>
    <template #expandedRowRender>
      <s-table :columns="innerColumns" :data-source="innerData" :pagination="false">
        <template #bodyCell="{ column, text }">
          <template v-if="column.key === 'state'">
            <span>
              <a-badge status="success" />
              Finished
            </span>
          </template>
          <template v-else-if="column.key === 'operation'">
            <span class="table-operation">
              <a>Pause</a>
              <a>Stop</a>
              <a-dropdown>
                <template #overlay>
                  <a-menu>
                    <a-menu-item>Action 1</a-menu-item>
                    <a-menu-item>Action 2</a-menu-item>
                  </a-menu>
                </template>
                <a>
                  More
                  <down-outlined />
                </a>
              </a-dropdown>
            </span>
          </template>
          <template v-else>{{ text }}</template>
        </template>
      </s-table>
    </template>
  </s-table>
</template>
<script lang="ts">
import { DownOutlined } from '@ant-design/icons-vue';
import { defineComponent } from 'vue';

interface DataItem {
  key: number;
  name: string;
  platform: string;
  version: string;
  upgradeNum: number;
  creator: string;
  createdAt: string;
}
interface innerDataItem {
  key: number;
  date: string;
  name: string;
  upgradeNum: string;
}
export default defineComponent({
  components: {
    DownOutlined,
  },
  setup() {
    const columns = [
      { title: 'Name', dataIndex: 'name', key: 'name' },
      { title: 'Platform', dataIndex: 'platform', key: 'platform' },
      { title: 'Version', dataIndex: 'version', key: 'version' },
      { title: 'Upgraded', dataIndex: 'upgradeNum', key: 'upgradeNum' },
      { title: 'Creator', dataIndex: 'creator', key: 'creator' },
      { title: 'Date', dataIndex: 'createdAt', key: 'createdAt' },
      { title: 'Action', key: 'operation' },
    ];

    const data: DataItem[] = [];
    for (let i = 0; i < 3; ++i) {
      data.push({
        key: i,
        name: `Screem ${i + 1}`,
        platform: 'iOS',
        version: '10.3.4.5654',
        upgradeNum: 500,
        creator: 'Jack',
        createdAt: '2014-12-24 23:12:00',
      });
    }

    const innerColumns = [
      { title: 'Date', dataIndex: 'date', key: 'date' },
      { title: 'Name', dataIndex: 'name', key: 'name' },
      { title: 'Status', key: 'state' },
      { title: 'Upgrade Status', dataIndex: 'upgradeNum', key: 'upgradeNum' },
      {
        title: 'Action',
        dataIndex: 'operation',
        key: 'operation',
      },
    ];

    const innerData: innerDataItem[] = [];
    for (let i = 0; i < 3; ++i) {
      innerData.push({
        key: i,
        date: '2014-12-24 23:12:00',
        name: `This is production name ${i + 1}`,
        upgradeNum: 'Upgraded: 56',
      });
    }
    return {
      data,
      columns,
      innerColumns,
      innerData,
    };
  },
});
</script>