Ant table表格合并单元格使用

Ant 合并单元格+覆盖样式

项目需求:

最后一列用来展示详情,表格左侧点击行切换;要求选中行的样式和详情信息用一个边框凸显出来

结果如下:

Ant table表格合并单元格使用_第1张图片

 

切换效果展示如下:

Ant table表格合并单元格使用_第2张图片

 

实现过程:

在ant合并单元格demo中,找到在线编辑器实现初始版本,实现合并单元格

去除多余的表头和行、列简化项目代码:

const columns = [
 {
    title:'name'
    dataIndex:'name',
    render:(text) => {text}
  },
  {
    title: 'Age',
    dataIndex: 'age',
  },
   {
    title:'合并'
      dataIndex: 'tel',
      onCell:(_,index)=>{
        if(index === 0){
        return {
          rowSpan:5
        };
        }
        return {rowSpan:0}
      }
  },
]

改造了column配置,onCell配置用第一行数据的单元格,平铺占满:效果如下图,

下面是做表格行点击选择,rowClassName属性设置单行class,通过设置这个方法对选中行添加单独的class

onRow方法可以控制表格每行的鼠标交互事件,因此点击选择行的核心代码如下:

​
import React, {useState} from 'react';
import { Table } from 'antd';
​
const App = () =>{
const [selectIndex,setSelectIndex] = useState(0);
return(
  {
    return{
     //点击行
     onClick:event=>{
     setSelectIndex(index)
     }
    }
    }
    }
    rowClassName={(record,index)=>{
      if(index === selectIndex) return 'selectedRow';
      return '';
    }}
   /> 
)
}
​

接下来就来到CSS覆盖频道了,实现选中行包围详情背景+边框了:

.ant-table-tbody > :first-child > :last-child {
  background: #f1f1ff;
  border: 1px solid red;
}
​
.selectedRow .ant-table-cell {
  background-color: #f1f1ff;
  background: #f1f1ff;
  border: 1px solid red;
  border-left: none;
  border-right: none;
  position: relative;
}
​
.selectedRow>:first-child {
  border-left: 1px solid red;
  border-radius: 2px 0 0 2px;
}
​
.selectedRow .ant-table-cell::after {
  content:' ';
  position:absolute;
  width: 4px;
  height: 54px;
  background: #f1f1ff;
  right: -2px;
  top: 0px;
  z-index: 999;
}
​
.ant-table-tbody > :first-child > :last-child::after {
 display: none;
}
​
.selectedRow .ant-table-cell-row-hover {
  background-color: #f1f1ff !important;
  background: #f1f1ff !important;
}

上面这些CSS都是常规操作,:1- 核心逻辑是给selectedRow下面的每个单元格加边框、加背景2 -然后通过每个单元格的after伪类覆盖掉不需要的边框

最后是在右侧单元格上显示选中行的详情了,1- 这一步实现思路是直接把columns改造成函数,传入选中行下标作为参数;2- 最后在右侧单元格的render方法中获取到选中行的数据,然后展示详细信息

const columns = (index)=>[
  {
    title: 'Name',
    dataIndex: 'name',
    render: (text) => {text},
  },
  {
    title: 'Age',
    dataIndex: 'age',
  },
  {
    title: 'Home phone',
    dataIndex: 'tel',
    onCell: (_, index) => {
      if (index === 0) {
        return {
          rowSpan: 5,
        };
      }
      return {rowSpan: 0};
    },
    render:()=>{
      const current = data[index];
      return 
     
name:{current.name}
     
age:{current.age}
     
phone:{current.phone}
     
address:{current.address}
   
;   } }, ]; // 调用时columns传入唯一标识就好了 //

这样改造之后,点击就可以显示具体详情了,实际项目中还是要改回 id 之类的唯一健

最后附上demo.js文件,全部代码

demo.js

import React, { useState } from "react";
import "antd/dist/antd.css";
import "./index.css";
import { Table } from "antd";
​
const data = [
  {
    key: "1",
    name: "张三",
    age: 32,
    tel: "0571-22098909",
    phone: 18889898989,
    address: "New York No. 1 Lake Park"
  },
  {
    key: "2",
    name: "晓余",
    tel: "0571-22098333",
    phone: 18889898888,
    age: 42,
    address: "London No. 1 Lake Park"
  },
  {
    key: "3",
    name: "布布",
    age: 32,
    tel: "0575-22098909",
    phone: 18900010002,
    address: "Sidney No. 1 Lake Park"
  },
  {
    key: "4",
    name: "小红",
    age: 18,
    tel: "0575-22098909",
    phone: 18900010002,
    address: "London No. 2 Lake Park"
  },
  {
    key: "5",
    name: "小张",
    age: 18,
    tel: "0575-22098909",
    phone: 18900010002,
    address: "Dublin No. 2 Lake Park"
  }
];
​
const columns = (index) => [
  {
    title: "姓名",
    dataIndex: "name",
    render: (text) => {text}
  },
  {
    title: "年龄",
    dataIndex: "age"
  },
  {
    title: "详情信息",
    dataIndex: "tel",
    onCell: (_, index) => {
      if (index === 0) {
        return {
          rowSpan: 5
        };
      }
      return { rowSpan: 0 };
    },
     render: () => {
      const current = data[index];
      return (
        
       
姓名:{current.name}
       
年龄:{current.age}
       
联系方式:{current.phone}
       
联系地址:{current.address}
     
    );   } } ]; ​ const App = () => { const [selectIndex, setSelectIndex] = useState(0); return (  
{       return {         // 点击行         onClick: (event) => {           setSelectIndex(index);         }       };     }}     rowClassName={(record, index) => {       if (index === selectIndex) return "selectedRow";       return "";     }}   /> ); }; ​ export default App; ​

index.css

.ant-table-tbody > :first-child > :last-child {
  background: #f1f1ff;
  border: 1px solid red;
}
​
.selectedRow .ant-table-cell {
  background-color: #f1f1ff;
  background: #f1f1ff;
  border: 1px solid red;
  border-left: none;
  border-right: none;
  position: relative;
}
​
.selectedRow > :first-child {
  border-left: 1px solid red;
  border-radius: 2px 0 0 2px;
}
​
.selectedRow .ant-table-cell::after {
  content: " ";
  position: absolute;
  width: 4px;
  height: 54px;
  background: #f1f1ff;
  right: -2px;
  top: 0px;
  z-index: 999;
}
​
.ant-table-tbody > :first-child > :last-child::after {
  display: none;
}
​
.selectedRow .ant-table-cell-row-hover {
  background: #f1f1ff !important;
}
​

index.html



  
    
    
    
  
  
    
 

index.js

​
import React from 'react';
import ReactDOM from 'react-dom';
import Demo from './demo';
​
ReactDOM.render(, document.getElementById('container'));
​

效果图附上:

Ant table表格合并单元格使用_第3张图片

 

Ant table表格合并单元格使用_第4张图片

目录

Ant 合并单元格+覆盖样式

实现过程:

demo.js

index.css

index.html

index.js


 

你可能感兴趣的:(javascript,前端,vue.js,anti-design-vue)