ant design of vue学习之表格Table

Ant design vue学习之表格Table

columns.js

表格列的配置,处理数据用于表格展示

const columns = [
	{
		title: '序号',
	    width: 80,
	    dataIndex: 'index',
	    customRender: (text, record, index) => {
	      return index + 1
	    },
	    align: 'center',
	    fixed: 'left'
	},
	{
        title: '标题',
        dataIndex: 'title',
        align: 'center',
        scopedSlots: {
          customRender: "myTitle",
        }
    },
    {
	    title: '类型',
	    dataIndex: 'type',
	    align: 'center',
	    customRender: (text, record, index) => {
	      return text === 1 ? '图文' : '视频'
	    }
	},
	{
	    title: '操作',
	    width: 200,
	    dataIndex: 'action',
	    align: 'center',
	    scopedSlots: {
	      customRender: 'action'
	    },
	    fixed: 'right'
	 }
]

使用

1、rowKey=“id” (每一行的标识,rowSelection获取的就是选中行的rowKey)
2、:rowSelection=“{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}” (选中行控制)

<a-table    rowKey="id"	
			size="small"
			bordered
			:columns="columns"
			:dataSource="dataList"
			:scroll="{x:1500}"	
	        :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"/>
	        <template slot="myTitle"
		              slot-scope="title,record">
		      <a href="javascript:;">{{title}}</a>
		    </template>
	        <span slot="action" slot-scope="text,record">
				<a href="javascript:;" @click="openDetailModal(record)">详情</a>
				<a-divider type="vertical"/>
				<a-dropdown>
					<a-menu slot="overlay">
						<a-menu-item>
							<a href="javascript:;" @click="openChangeModal(record)">修改</a>
						</a-menu-item>
						<a-menu-item>
							<a-popconfirm title="是否删除这条数据?"
										  @confirm="remove(record)"
										  okText=""
										  cancelText=""
										  placement="topRight">
								<a class="txt-danger" href="javascript:;">删除</a>
							</a-popconfirm>
						</a-menu-item>
					</a-menu>
					<a href="javascript:;">更多<a-icon type="down"/></a>
				</a-dropdown>
			</span>
</a-table>
data(){
	columns,
	dataList:[],
	selectedRowKeys:[]
},
methods:{
	onSelectChange(selectedRowKeys){
		this.selectedRowKeys = selectedRowKeys;
	}
}

2、columns.js 复杂
在这里插入图片描述

表头分组:可以内嵌 children,以渲染分组表头。
过滤:使用 filters以及onFilter
排序:使用 sorter: (a, b) => a.age - b.age,

<template>
  <a-table
    :columns="columns"
    :dataSource="data"
    bordered
    size="middle"
    :scroll="{ x: '130%', y: 240 }"
  />
</template>
<script>
  const columns = [
    {
      title: 'Name',
      dataIndex: 'name',
      key: 'name',
      width: 100,
      fixed: 'left',
      //过滤
      //筛选条件编辑
      filters: [
        {
          text: 'Joe',
          value: 'Joe',
        },
        {
          text: 'John',
          value: 'John',
        },
      ],
      //获取value, record  根据return  true 或者 false 进行筛选
      onFilter: (value, record) => record.name.indexOf(value) === 0,
    },
    {
      title: 'Other',
      children: [
        {
          title: 'Age',
          dataIndex: 'age',
          key: 'age',
          width: 200,
          //排序
          sorter: (a, b) => a.age - b.age,
        },
        {
          title: 'Address',
          children: [
            {
              title: 'Street',
              dataIndex: 'street',
              key: 'street',
              width: 200,
            },
            {
              title: 'Block',
              children: [
                {
                  title: 'Building',
                  dataIndex: 'building',
                  key: 'building',
                  width: 100,
                },
                {
                  title: 'Door No.',
                  dataIndex: 'number',
                  key: 'number',
                  width: 100,
                },
              ],
            },
          ],
        },
      ],
    },
    {
      title: 'Company',
      children: [
        {
          title: 'Company Address',
          dataIndex: 'companyAddress',
          key: 'companyAddress',
        },
        {
          title: 'Company Name',
          dataIndex: 'companyName',
          key: 'companyName',
        },
      ],
    },
    {
      title: 'Gender',
      dataIndex: 'gender',
      key: 'gender',
      width: 80,
      fixed: 'right',
    },
  ];

  const data = [];
  for (let i = 0; i < 100; i++) {
    data.push({
      key: i,
      name: 'John Brown',
      age: i + 1,
      street: 'Lake Park',
      building: 'C',
      number: 2035,
      companyAddress: 'Lake Street 42',
      companyName: 'SoftLake Co',
      gender: 'M',
    });
  }

  export default {
    data() {
      return {
        data,
        columns,
      };
    },
  };
</script>

完整实例

<template>
  <a-table :columns="columns"
           :dataSource="dataList"
           bordered>
    <template slot="name"
              slot-scope="name,record">
      <a href="javascript:;">{{record.name}}</a>
    </template>
    <span slot="action"
          slot-scope="record">
      <a href="javascript:;">详情</a>
      <a-divider type="vertical" />
      <a-dropdown>
        <a-menu slot="overlay">
          <a-menu-item>
            <a href="javascript:;">修改</a>
          </a-menu-item>
          <a-menu-item>
            <a-popconfirm title="是否删除?"
						 @confirm="remove(record)"
						  okText=""
						  cancelText=""
						  placement="topRight">
				<a class="txt-danger" href="javascript:;">删除</a>
			</a-popconfirm>
          </a-menu-item>
          <a-menu-item>
            <a href="javascript:;"
               @click="exportToExcel(record.key)">导出</a>
          </a-menu-item>
        </a-menu>
        <a href="javascript:;">更多
          <a-icon type="down" /></a>
      </a-dropdown>
    </span>
  </a-table>
</template>

<script>

const dataList = [
  {
    key: '1',
    name: 'Brown John',
    age: 32,
    tel: '0571-22098909',
    phone: 18889898989,
    address: 'New York No. 1 Lake Park',
  },
  {
    key: '2',
    name: 'Jim Green',
    tel: '0571-22098333',
    phone: 18889898888,
    age: 42,
    address: 'London No. 1 Lake Park',
  },
  {
    key: '3',
    name: 'Joe Black',
    age: 32,
    tel: '0575-22098909',
    phone: 18900010002,
    address: 'Sidney No. 1 Lake Park',
  },
  {
    key: '4',
    name: 'Jim Red',
    age: 18,
    tel: '0575-22098909',
    phone: 18900010002,
    address: 'London No. 2 Lake Park',
  },
  {
    key: '5',
    name: 'Jake White',
    age: 18,
    tel: '0575-22098909',
    phone: 18900010002,
    address: 'Dublin No. 2 Lake Park',
  },
];
const columns = [
  {
    title: '姓名',
    dataIndex: 'name',
    filters: [
      {
        text: 'Joe',
        value: 'Joe',
      },
      {
        text: 'John',
        value: 'John',
      },
    ],
    onFilter: (value, record) => record.name.indexOf(value) != -1,
    scopedSlots: {
      customRender: "name",
    }
  },
  {
    title: 'Age',
    dataIndex: 'age',
    sorter: (a, b) => a.age - b.age,
  },
  {
    title: "手机",
    children: [
      {
        title: 'Home phone',
        dataIndex: 'tel',
      },
      {
        title: 'Phone',
        dataIndex: 'phone',
      },
    ]
  },
  {
    title: 'Address',
    dataIndex: 'address',
  },
  {
    title: "操作",
    key: "action",
    width: 160,
    scopedSlots: {
      customRender: "action",
    }
  }
];
export default {
  data () {
    return {
      dataList,
      columns,
    };
  },
  methods: {
    exportToExcel (id) {
      console.log(id)
    }
  }
};
</script>

Ant Design of Vue 提供了一个`<a-table>`组件,可以用来展示和操作表格数据。在这个组件中,合并单元格需要使用`customRow`属性,结合自定义的方法来实现。 具体步骤如下: 1. 定义表格数据源: ```js data() { return { dataSource: [ { key: '1', name: 'John Brown', age: 32, address: 'New York No. 1 Lake Park', children: [ { name: 'Lucy Brown', age: 6 }, { name: 'Tom Brown', age: 4 }, ], }, { key: '2', name: 'Joe Black', age: 42, address: 'London No. 1 Lake Park', children: [ { name: 'Jim Black', age: 6 }, ], }, { key: '3', name: 'Jim Green', age: 32, address: 'Sidney No. 1 Lake Park', children: [], }, ], }; }, ``` 2. 定义表格列的配置项: ```js columns: [ { title: 'Name', dataIndex: 'name', key: 'name', customRow(row, index) { const { dataSource } = this; const { children } = dataSource[index]; const rowSpan = children.length + 1; return { props: { rowSpan } }; }, }, { title: 'Age', dataIndex: 'age', key: 'age', }, { title: 'Address', dataIndex: 'address', key: 'address', }, { title: 'Children', dataIndex: 'children', key: 'children', slots: { customRender: 'children' }, }, ], ``` 在上面的配置项中,`customRow`属性定义了如何合并单元格。具体来说,它会在渲染每一行数据时调用,传入两个参数:`row`表示当前行的数据,`index`表示当前行的索引。在方法体内,我们可以通过`this.dataSource`获取表格数据源,从而计算出当前行的`rowSpan`值,即合并的行数。 3. 在表格中使用自定义的列配置项: ```html <a-table :columns="columns" :data-source="dataSource"> <template #children="{ text }"> <span v-for="child in text" :key="child.name"> {{ child.name }} <a-divider type="vertical" /> {{ child.age }} </span> </template> </a-table> ``` 在上面的代码中,我们使用`<template>`标签来定义自定义的列渲染方式。其中,`#children`表示渲染`children`列,并传入`{ text }`作为参数,`text`表示当前行的`children`属性。在模板内容中,我们可以通过`v-for`来遍历`children`数组,渲染出每个子元素的信息。 以上就是使用 Ant Design of Vue 实现合并表格的方法。需要注意的是,如果表格数据源中有嵌套的数组,我们需要在自定义的列渲染方式中进行特殊处理,以展示嵌套数据的信息。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值