React+antd:为表格添加成组标记

最近在做前端开发的时候遇到一个需求:对后端传回的数据中满足条件的相邻行用符号连接起来,表示关联关系。我是个前端新人,因项目需要接手前端开发工作,为了实现这个需求颇费了一番功夫。现在记录下来方便后来者。

这篇文章首先用原生CSS实现了这个需求,然后在React工程中的antd表格组件中实际应用。

用CSS实现表格的成组标记

本文以在相同国家的人名前加上成组标记为例

CodoPen在线演示

效果如下图所示
这里写图片描述

<style>
    table {
        width: 800px;
        margin: 20px auto;
    }

    table td,
    table th {
        border-bottom: 1px solid #E5E5E5;
        padding: 10px 6px;
    }

    .grp {
        position: relative;
    }

    .grp::before {
        position: absolute;
        content: '';
        width: 8px;
        height: 50%;
        left: -10px;
        border-left: 2px solid #A2A0A0;
    }

    .top::before {
        border-top: 2px solid #A2A0A0;
        top: 50%;
    }

    .bottom::before {
        border-bottom: 2px solid #A2A0A0;
        bottom: 50%;
    }

    .center::before {
        height: 100%;
        top: 0;
    }
</style>

<table cellspacing="0px">
    <thead>
        <tr>
            <th>name</th>
            <th>age</th>
            <th>country</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td class='grp top'>张一</td>
            <td>12</td>
            <td>China</td>
        </tr>
        <tr>
            <td class='grp bottom'>张二</td>
            <td>12</td>
            <td>China</td>
        </tr>
        <tr>
            <td>山本</td>
            <td>12</td>
            <td>Japan</td>
        </tr>
        <tr>
            <td class='grp top'>张三</td>
            <td>12</td>
            <td>France</td>
        </tr>
        <tr>
            <td class='grp center'>张四</td>
            <td>12</td>
            <td>France</td>
        </tr>
        <tr>
            <td class='grp bottom'>张五</td>
            <td>12</td>
            <td>France</td>
        </tr>
    </tbody>
</table>

在React+antd中实现表格的成组标记

在antd的表格组件中没有办法对指定行的指定td添加指定类名,通过查阅官方文档发现可以通过组件的rowClassNamecolumns.className实现我们的需求

  • rowClassName 通过函数设置指定行的类名
  • columns.className 指定表格某一列的类名,只能接受字符串类名

主要分为两个步骤
1. 对表格的数据data进行解析,对每一条数据的前后行比对contry字段,计算出在成组标记中的位置并记录在对象flagObj
2. 编写rowClassName函数,对每一行指定相应的类名

CodoPen在线演示

js

const {
    Table,
    Divider,
    Tag
} = antd;

const data = [{
    id: 1,
    name: 'zhangsan',
    age: 42,
    contry: 'China'
}, {
    id: 2,
    name: 'lisi',
    age: 42,
    contry: 'China'
}, {
    id: 3,
    name: 'shanben',
    age: 42,
    contry: 'Japan'
}, {
    id: 4,
    name: 'John Brown',
    age: 32,
    contry: 'France'
}, {
    id: 5,
    name: 'Jim Green',
    age: 42,
    contry: 'France'
}, {
    id: 6,
    name: 'Joe Black',
    age: 32,
    contry: 'France'
}];

let flagObj = {}

const doFlag = (items) => {
    items.forEach((e, i) => {
        const contryName = e.contry ? e.contry : 'none'
        let preflag = false, nextflag = false
        if (items[i - 1] && items[i - 1].contry === contryName) {
            preflag = true
        }
        if (items[i + 1] && items[i + 1].contry === contryName) {
            nextflag = true
        }

        if (preflag && nextflag) {
            flagObj[e.id] = 'center flag'
        } else if (preflag && !nextflag) {
            flagObj[e.id] = 'bottom flag'
        } else if (!preflag && nextflag) {
            flagObj[e.id] = 'top flag'
        }else{
          flagObj[e.id] = ' noflag'
        }
    })
}

data && doFlag(data)

const columns = [{
    title: 'Name',
    dataIndex: 'name',
    key: 'name',
    className: 'grp',   //
}, {
    title: 'Age',
    dataIndex: 'age',
    key: 'age'
},
{
    title: 'Contry',
    dataIndex: 'contry',
    key: 'contry'
}];

//成组标记
const handleRowClassName = (record, index) => {
    return record && flagObj[(record.id)] && (flagObj[(record.id)])
}

ReactDOM.render(
    < Table
        columns={columns}
        dataSource={data}
        pagination={false}
        rowClassName={handleRowClassName}
    />, mountNode);

less

.flag {
  .grp {
    position: relative;
    &::before {
      position: absolute;
      content: '';
      width: 8px;
      height: 55%;
      left: -10px;
      border-left: 2px solid;
      border-color: #A2A0A0;
    }
  }
}
.top .grp::before {
  border-top: 2px solid;
  top: 50%;
}
.bottom .grp::before {
  border-bottom: 2px solid;
  bottom: 50%;
}
.center .grp::before {
  height: 100%;
  top: 0;
}

参考资料

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
React中,使用Ant Design的Table组件,在表格添加操作列并结合Dropdown和Menu组件可以实现更丰富的交互效果。 首先,我们需要导入相关的组件和样式: ```jsx import { Table, Dropdown, Menu, Button } from 'antd'; import { DownOutlined } from '@ant-design/icons'; import 'antd/dist/antd.css'; ``` 然后,在表格的columns中定义操作列,并使用Dropdown和Menu组件来定制操作菜单: ```jsx const columns = [ ...其他列定义, { title: '操作', key: 'action', render: (text, record) => ( <Dropdown overlay={menu}> <Button> 操作 <DownOutlined /> </Button> </Dropdown> ), }, ]; const menu = ( <Menu> <Menu.Item key="1"> <a target="_blank" rel="noopener noreferrer" href="#"> 编辑 </a> </Menu.Item> <Menu.Item key="2"> <a target="_blank" rel="noopener noreferrer" href="#"> 删除 </a> </Menu.Item> </Menu> ); ``` 以上代码中,columns定义了表格的列,其中操作列使用render属性来渲染。在render方法中,我们使用Dropdown组件来包裹Button,并将Menu作为overlay传入。 menu定义了操作菜单的内容,其中使用Menu.Item来定义菜单项,通过设置key属性来区分不同的菜单项。菜单项可以是一段文本或者其他React元素,这里我们使用a标签来模拟菜单项的操作。 最后,在Table组件中将columns配置表格的列: ```jsx <Table columns={columns} dataSource={data} /> ``` 以上是在React中使用Ant Design的Table组件添加操作列并结合Dropdown和Menu的写法,通过这种方式,我们可以方便地在表格中定制各种操作的菜单。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值