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

Passing row to getColumnValue, so column can have a sortBy function #10

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

MikeWillis
Copy link

I'm using multi-column-sort to sort a table of data, which has columns and rows. I'd like the columns to be able to use a custom sortBy function, which can perhaps return a calculated value based on other data in the row. Therefore, the getColumnValue function needs to receive the entire row.

It can then be left up to the user to set up their getColumnValue function to make use of the row and possible sortBy function.

In my case I'm working on a React app, and the columns are an array stored in state. multi-column-sort does not receive the columns array, so in my getColumnValue function I filter the columns from state and choose the one that matches.

For example here's some of the code from my React app:

let columns = [
    { name: 'Name', selector: 'name', sortable: true },
    { name: 'Gender', selector: 'gender', sortable: true },
    { name: 'Age', selector: 'age', sortable: true, sortBy: (column, value, row)=>{
        // here I'm just returning the name instead of age, but you could make any calculation you want
        return row.name
    } },
];
...
const [state_columns,setState_columns] = useState( (columns || []) );
...
let getColumnValue = (columnName, value, row) => {
    let sortingColumn = state_columns.filter(col=>col.selector === columnName);
    if ( sortingColumn[0].sortBy ) {
        return sortingColumn[0].sortBy(columnName, value, row);
    } else {
        return value;
    }
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant