Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The ability to reset columnOrder in DataTable #899

Closed
Bublade opened this issue May 22, 2019 · 1 comment
Closed

The ability to reset columnOrder in DataTable #899

Bublade opened this issue May 22, 2019 · 1 comment
Assignees
Labels
Type: Enhancement Issue contains an enhancement related to a specific component. Additional functionality has been add
Milestone

Comments

@Bublade
Copy link

Bublade commented May 22, 2019

I'm submitting a ...

[ ] bug report
[X] feature request
[ ] support request => Please do not submit support request here, instead see https://forum.primefaces.org/viewforum.php?f=57
  • PrimeReact version:
    3.1.3
  • Language:
    TypeScript 3.4.5

Currently in the datatable there is no ability to reset/restore a default layout (columnWith, columnSort order, columnSortKey, visible columns, etc...). So I fixed that myself, there is one thing though that cannot be reset / restored without refreshing or by remounting the datatable and that is de columnOrder.

When I add the columns as children to DataTable I already sorted them myself by the default layout but these lines DataTable.js#L1179-L1194 use the states columnOrder which cannot be modified from outside.

A few suggestions
instead of:

if(this.props.reorderableColumns && this.state.columnOrder) {
    let orderedColumns = [];
    for(let columnKey of this.state.columnOrder) {
        let column = this.findColumnByKey(columns, columnKey);
        if (column) {
            orderedColumns.push(column);
        }
    }
                
    return [...orderedColumns, ...columns.filter((item) => {
        return orderedColumns.indexOf(item) < 0;
    })];
}
else {
    return columns;
}

Add a property to render children as is used like this

if (this.props.keepChildrenOrder) { // or a better name for it.
    return columns;
}
else  if(this.props.reorderableColumns && this.state.columnOrder) {
    let orderedColumns = [];
    for(let columnKey of this.state.columnOrder) {
        let column = this.findColumnByKey(columns, columnKey);
        if (column) {
            orderedColumns.push(column);
        }
    }
                
    return [...orderedColumns, ...columns.filter((item) => {
        return orderedColumns.indexOf(item) < 0;
    })];
}
else {
    return columns;
}

Or by exposing the order as a property

if(columns && columns.length) {
    const columnOrder = (this.props.columnOrder || this.state.columnOrder); // giving the property higher priority
    if(this.props.reorderableColumns && columnOrder) { // <-- using the defined list.
        let orderedColumns = [];
        for(let columnKey of columnOrder ) { // <-- using the defined list.
            let column = this.findColumnByKey(columns, columnKey);
            if (column) {
                orderedColumns.push(column);
            }
        }
                    
        return [...orderedColumns, ...columns.filter((item) => {
            return orderedColumns.indexOf(item) < 0;
        })];
    }
    else {
        return columns;
    }
}
@mertsincan
Copy link
Member

I think of adding a resetColumnOrder method for these issues. Users can call this method wherever they want.

resetColumnOrder() {
        let columns = React.Children.toArray(this.props.children);
        this.setState({columnOrder: columns});
}
// in your project
<DataTable ref={(el) => this.dt = el} .. />

// to reset the column orders;
this.dt.resetColumnOrder();

@mertsincan mertsincan added the Type: Enhancement Issue contains an enhancement related to a specific component. Additional functionality has been add label May 22, 2019
@mertsincan mertsincan self-assigned this May 22, 2019
@mertsincan mertsincan added this to the 3.1.4 milestone May 22, 2019
mertsincan added a commit that referenced this issue May 22, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Type: Enhancement Issue contains an enhancement related to a specific component. Additional functionality has been add
Projects
None yet
Development

No branches or pull requests

2 participants