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

Sorting and filter is not working using react data grid #3479

Open
Randhir01 opened this issue Mar 27, 2024 · 1 comment
Open

Sorting and filter is not working using react data grid #3479

Randhir01 opened this issue Mar 27, 2024 · 1 comment
Labels

Comments

@Randhir01
Copy link

i'm facing the issue when integrate the sorting and filter using react data grid and react version 18..Below code is use. Any suggestion what's wrong in the code.

import React, { createContext, createElement, useContext, useMemo, useEffect, useLayoutEffect as useLayoutEffect$1, useRef, useState, useCallback, memo, useId, forwardRef, useImperativeHandle } from 'react';
import { flushSync } from 'react-dom';
import clsx from 'clsx';
import { jsx, jsxs, Fragment } from 'react/jsx-runtime';
import ReactDataGrid from "react-data-grid";
import 'react-data-grid/lib/styles.css';

class EmptyToolbar extends React.Component {
componentDidMount() {
this.props.onToggleFilter()
}

render() {
    return (<div></div>)
}

}

class Title extends React.Component {
constructor(props) {
super(props);
this.state = {
rows: [
{ id: 1, name: 'John', age: 25 },
{ id: 2, name: 'Jane', age: 30 },
{ id: 3, name: 'Doe', age: 40 },
],
columns: [
{
key: 'id',
name: 'ID',
width: 100,
sortable: true,
filterRenderer: () => ,
},
{
key: 'name',
name: 'Name',
width: 200,
sortable: true,
filterRenderer: () => ,
},
{
key: 'age',
name: 'Age',
width: 100,
sortable: true,
filterRenderer: () => ,
},
],
sortColumn: null,
sortDirection: 'NONE',
filters: {},
};
}

    handleSort = (columnKey, direction) => {
        this.setState({ sortColumn: columnKey, sortDirection: direction });
    };

    handleFilterChange = (columnKey, value) => {
        this.setState((prevState) => ({
            filters: { ...prevState.filters, [columnKey]: value },
        }));
    };
    rowGetter = rowIdx => {
        const rows = this.state.rows;
        return rows[rowIdx];
    };
render() {
    //return 
    const { rows, sortColumn, sortDirection, filters } = this.state;

    const FilterInput = ({ columnKey }) => (
        <input
            type="text"
            onChange={(e) => this.handleFilterChange(columnKey, e.target.value)}
            placeholder={`Filter ${columnKey}`}
        />
    );

    const filteredRows = rows.filter((row) =>
        Object.entries(filters).every(([key, value]) =>
            row[key].toString().toLowerCase().includes(value.toLowerCase())
        )
    );
    return (
        <div>
           
            <ReactDataGrid
                enableCellSelect={true}
                columns={this.state.columns}
                
                onGridSort={this.handleSort}
                
                rowsCount={this.state.rows.length}
                toolbar={<EmptyToolbar />} //here's the EmptyToolbar component placed
                onAddFilter={this.handleFilterChange}
                 />
        </div>
    );

}

}

export default Title;

@Randhir01 Randhir01 added the Bug label Mar 27, 2024
@truk
Copy link

truk commented Apr 30, 2024

You might be using a really old version. ReactDataGrid isn't an exported component at this point and onGridSort isn't a current prop. Try using a current version and use and you'll get a lot of sorting out of the box

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

No branches or pull requests

2 participants