Search code examples
javascriptreactjsmui-datatable

Search is not working in mui-datatable, reactjs when serverSide is true?


Hey I have implemented mui-datatable in my project. Everything is working good but the search bar is not working when I am enabling serverSide: true in options.

const options = {
    filter: true,
    count: state.count,
    rowsPerPage: state.rowsPerPage,
    serverSide: true,
}

If any clarification regarding this problem you want to know please feel free to ask.

Please Help me.

Regards


Solution

  • Here I provide some code so that it will be conceptually clear.

    Here I declare some states so that we can use these for paging or data loading

    const [data, setData] = useState(carStocks);
    const [page, setPage] = useState(0);
    const [total, setTotal] = useState(0);
    

    Define your method that will call api and set data using setData

    function changePage(newTableState) {
        let state = {
            ...tableState, ...newTableState,
            pageNo: newTableState.page + 1,
            pageSize: newTableState.rowsPerPage,
            searchText: newTableState.searchText
        };
        setTableState(state);
        // CALL API with state parameter
        // setData(data);
        // setPage(page);
        // setTotal(total);
    }
    

    Here you declare your columns

    const columns = [
       {
            name: 'name',
            label: 'Name',
            options: {
                filter: false,
                sort: false,
                viewColumns: true
            }
        },
        {
            name: 'title',
            label: 'TITLE',
            options: {
                filter: false,
                sort: false,
                viewColumns: true
            }
        },
    ];
    

    Here you declare your options. In options you must use serverSide: true and if you use serverSide: true then you must bind count, page, rowsPerPage with the value based on data size found from api.

    const options = {
            serverSide: true,
            count: total,
            page: page,
            rowsPerPage: tableState.pageSize,
            selectableRows: 'multiple',
            searchText: tableState.searchText,
            searchPlaceholder: 'Type Car Title to Search',
            textLabels: {
                body: {
                    noMatch: loading ?
                        <CircularProgress color='secondary' size={40} /> :
                        'SORRY_THERE_IS_NO_MATCHING_DATA_TO_DISPLAY'
                }
            },
            onTableChange: (action, newTableState) => {
                switch (action) {
                    case 'changePage':
                    case 'changeRowsPerPage':
                        changePage(newTableState);
                        break;
                    case 'search':
                        changePage(newTableState);
                        break;
                    case 'filterChange':
                        handleFilterSubmit(newTableState.filterList);
                        break;
                }
            }
        };
    

    Finally you can use data, columns and options

    <MUIDataTable data={data} columns={columns} options={options} />
    

    More Details