基于antd table做一个可拖拽的列设置组件react

主要功能

可固定左右侧,显示隐藏列,可拖拽移动列的位置

图示

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)
  }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值