Table 相关问题

时旭东
2023-12-01

Table scroll不生效的问题

// Table scroll不生效的问题
<Table scroll={{y: 'calc(100% - 36px)'}} />

:global {
    .ant-table-wrapper,
    .ant-spin-nested-loading,
    .ant-spin-container,
    .ant-table,
    .ant-table-container {
        height: 100%;
    }
}

// 表格columns排序写法 (升序)
columns = [{
    title: '',
    dataIndex: '',
    sorter: {
        compare: (a, b) => {
            return a.calling - b.calling;
        }
    }
}]

在Drawer内的Table组件可拖拽写法

// HTML
import React, {Component} from 'react';
import arrayMove from 'array-move';
import {observer} from 'mobx-react';
import commonScreenCancel from '@img/icon/common_screen_cancel.png';
import {sortableContainer, sortableElement, sortableHandle} from 'react-sortable-hoc';
import drop from '@img/icon/drop.png';
import {Table, Button, Drawer} from 'antd';
import noticeStore from '@stores/Notice';
import {toJS} from 'mobx';
import style from './style.less';

const SortableContainer = sortableContainer(props => <tbody {...props} />);
const DragHandle = sortableHandle(() => (
    <div style={{width: 64, textAlign: 'center', height: 40, lineHeight: '40px', cursor: 'pointer'}}>
        <img src={drop} style={{margin: '-2px'}} />
    </div>
));
const SortableItem = sortableElement(props => <tr className={style.dragContent1} {...props} style={{position: 'relative', zIndex: 9999}} />); // 在抽屉拖拽可能不生效就是因为层级过低导致

@observer
export default class Index extends Component {
    constructor(props) {
        super(props);
        this.state = {};
    }

    columns = [
        {
            title: '排序',
            dataIndex: 'triggerId',
            width: 64,
            className: 'drag-visible',
            render: () => <DragHandle />
        },
        {
            title: '常用筛选名称',
            dataIndex: 'triggerName',
            width: 218,
            render: (text, record) => {
                return <div title={text || '-'}>{text || '-'}</div>;
            }
        },
        {
            title: '操作',
            width: 128,
            render: (text, record) => (
                <div className="table_action_text_blue">
                    <span
                        onClick={() => {
                            this.handleClickAdd('editor', record);
                        }}>
                        重命名
                    </span>
                    <span
                        onClick={() => {
                            this.handleClickDel(record);
                        }}>
                        删除
                    </span>
                </div>
            )
        }
    ];

    async componentDidMount() {}

    DraggableBodyRow = ({className, style, ...restProps}) => {
        // const dataSource = triggerStore.triggerData.resultlist || [];
        const dataSource = [];
        const index = dataSource.findIndex(x => x.triggerId === restProps['data-row-key']);
        return <SortableItem index={index} {...restProps} />;
    };

    onSortEnd = ({oldIndex, newIndex}) => {
        const {dataSource} = this.state;
        if (oldIndex !== newIndex) {
            const newData = arrayMove([].concat(dataSource), oldIndex, newIndex).filter(el => !!el);
            this.setState({dataSource: newData})
        }
    }

    render() {
        const DraggableContainer = props => <SortableContainer useDragHandle helperClass="row-dragging" onSortEnd={this.onSortEnd} {...props} />;

        return (
            <Drawer zIndex={11} className={style.common_config} width={460} placement="right" keyboard={false} maskClosable={false} closable={false} visible={true} getContainer={false} mask={false}>
                <div className={style.content}>
                    <div className={style.content_header}>
                        <div className={style.header_close_btn} onClick={this.onClose}>
                            <img src={commonScreenCancel} />
                        </div>
                        <div className={style.header_title}>常用筛选管理</div>
                    </div>
                    <div className={style.content_body}>
                        <Table
                            rowkey="triggerId"
                            pagination={false}
                            dataSource={[{a: 1}, {a: 2}]}
                            columns={this.columns}
                            rowKey="triggerId"
                            components={{
                                body: {
                                    wrapper: DraggableContainer,
                                    row: this.DraggableBodyRow
                                }
                            }}
                        />
                    </div>

                    <div className={style.content_footer}>
                        <Button className="btn-sure" htmlType="submit" onClick={() => this.debounceOk()}>
                            保存
                        </Button>
                        <Button onClick={this.onClose} className="btn-cancel">
                            取消
                        </Button>
                    </div>
                </div>
            </Drawer>
        );
    }
}

// less
.common_config {
    .content {
        width: 100%;
        height: 100%;
        display: flex;
        flex-direction: column;
        overflow: hidden;
   
        .content_header {
            height: 50px;
            line-height: 50px;
            padding: 0 20px;
            display: flex;
            align-items: center;
            // justify-content: space-between;
            border-bottom: 1px solid #ececec;
            color: #252525;
   
            .header_title {
                font-size: 14px;
                margin-left: 8px;
                color: #333333;
                font-weight: 500;
            }
   
            .header_close_btn {
                font-size: 12px;
                cursor: pointer;
            }
        }
   
        .content_body {
            padding: 24px;
            flex: 1;
            overflow: auto;

            .dragContent1 {
                :global {
                    .drag-visible {
                        padding: 0 !important;
                    }
                }
            }

            :global {
                .ant-table-thead {
                    .drag-visible {
                        padding: 0 !important;
                        text-align: center;
                    }
                }
            }
        }
   
        .content_footer {
            height: 64px;
            line-height: 64px;
            padding-left: 24px;
            border-top: 1px solid #ececec;
   
            button + button {
                margin-left: 10px;
            }

            > button {
                padding: 0 14px !important;
            }
        }
    }
}


 类似资料: