主要功能
可固定左右侧,显示隐藏列,可拖拽移动列的位置
图示
DOM
<Popover
className="common-line-sty"
placement="bottomRight"
title={getHeader()}
content={getContent()}
trigger="click">
<Tooltip placement="top" title="列设置">
<i className="i-menu" />
</Tooltip>
</Popover>
渲染表头
const getHeader = () => {
return (
<Row type="flex" justify="space-between">
<Col>
<Checkbox
onChange={changeAll}
indeterminate={
chooseArr.length < sortColumns.length && sortColumns.length
}
checked={chooseArr.length == sortColumns.length}>
列展示
</Checkbox>
</Col>
<a onClick={reset}>重置</a>
</Row>
);
};
渲染下拉内容
const getContent = () => {
const sorttableOper = {
items: sortColumns.filter((item) => !['left', 'right'].includes(item.fixed)),//过滤固定列
axis: 'y',
sortEnd: (cols) => sortEnd(cols),
renderDragDom: renderDragDom,
};
return (
<div className="set_con">
<div className="top_con">
{getDom('left')}
</div>
<div className="center_con">
<div className="fix_title">不固定</div>
<Sorttable {...sorttableOper} />
</div>
<div className="top_con">
{getDom('right')}
</div>
</div>
);
};
拖拽内容渲染
const renderDragDom = (item) => {
// const { isMove = false } = this.props;
// const { topCol, bottomCol, chooseArr } = this.state;
// const isTop = topCol.includes(item.title);
// const isBot = bottomCol.includes(item.title);
return (
<Row
key={item.dataIndex}
type="flex"
justify="space-between"
className="set_block">
<Checkbox
disabled={item.title == '操作'}
checked={item.visible}
onChange={(e) => changeOne(e, item.title)}>
{' '}
{item.text || item.title}
</Checkbox>
<Col>
<Icon
type="vertical-align-top"
onClick={() => moveCol(item.title, 'left')} />
<Icon type="vertical-align-bottom" onClick={() => moveCol(item.title, 'right')} />
</Col>
</Row >
);
};
固定列渲染
// 渲染上下的DOM
const getDom = (type = 'left') => {
const lArr = sortColumns.filter((item) => item.fixed == type)
return lArr.length > 0 ? (<> <div className="fix_title">固定在{type == 'left' ? '左' : '右'}侧</div>{lArr.map((_item) => (<>
<Row
key={_item.title}
type="flex"
justify="space-between"
className="set_block">
<Checkbox
disabled={_item.title == '操作'}
checked={_item.visible}
onChange={(e) => changeOne(e, _item.title)}>
{_item.text || _item.title}
</Checkbox>
{_item.title != '操作' && <Col>
<Icon
type="vertical-align-middle"
onClick={() => moveCol(_item.title, '')} />
</Col>}
</Row>
</>
))}</>) : null;
};
移动列数据
const sortEnd = (cols) => {
let topArr = sortColumns.filter((item) => item.fixed == 'left')
let bottomArr = sortColumns.filter((item) => item.fixed == 'right')
let newArr = [...topArr, ...cols, ...bottomArr]
console.log(newArr)
setSortColumns(newArr)
}