material-table icon indicating copy to clipboard operation
material-table copied to clipboard

Add Server side paging, filtering, sorting feature

Open mbrn opened this issue 8 years ago • 26 comments

Add Server side paging, filtering, sorting feature

mbrn avatar Oct 25 '18 17:10 mbrn

+1 any idea on when this will be completed?

jakeleventhal avatar Nov 14 '18 20:11 jakeleventhal

Hi @jakeleventhal,

Nowadays i am working very hard for another project. But i will do this as soon as possible. Please star project and wait for next releases.

Also why don't you fork this project and contribute it.

mbrn avatar Nov 15 '18 05:11 mbrn

I'm a very busy guy haha I can wait. Thanks for all the effort though.

jakeleventhal avatar Nov 15 '18 06:11 jakeleventhal

@mbrn I see what you mean in regard to #174

Want to delete or merge these issues?

plummer-flex avatar Jan 22 '19 23:01 plummer-flex

This feature would be super useful for a project im doing. Any heads up on when this will be implemented?

IllusiveBagel avatar Jan 25 '19 10:01 IllusiveBagel

Hi @IllusiveBagel ,

I am working on grouping feature now. I think i can start this feature after 1 months.

mbrn avatar Jan 25 '19 10:01 mbrn

Ok thanks @mbrn,

Is there any way of tricking it until you add that? e.g adding my own pagination on top?

(Sorry I know probably not the right place to chat about this)

IllusiveBagel avatar Jan 25 '19 10:01 IllusiveBagel

No problem. You may override the Components. But this would not be an effective solution.

https://mbrn.github.io/material-table/#/docz-examples-10-example-component-overriding

mbrn avatar Jan 25 '19 10:01 mbrn

ahh ok cool thanks this will be a good enough solution for now

IllusiveBagel avatar Jan 25 '19 10:01 IllusiveBagel

I have implemented server side implementation by adding custom handlers for pagesize, page change, searching, sorting, filtering. I can use that until the server side events are implemented as part of material table. Once available I can remove my custom code. For now I am able to manage without any issues

vijthang61 avatar Feb 07 '19 11:02 vijthang61

Is there a way to use customSort to trigger a function that would reload the data so that sorting can be done server side? I can't seem to get it to do anything. I'd like to click on one of the table headers and then have that call a function that makes a call with the table header as a param to sort server side.

roseak avatar Feb 11 '19 22:02 roseak

@roseak , unfortunately for now.

mbrn avatar Feb 12 '19 04:02 mbrn

I have implemented server side sort by overriding the onOrderChange prop, I had written by custom implementation onOrderChange={this.handleOrderChange}, here I make a server side in the function handleOrderChange, which will update the table row state variable which will automatically render the UI. In handleOrderChange function I update the a state variable for sort fields, once I set it it will immediately call ComponentDidUpdate method from where I make the ajax call and update the state variable for table row data. Once state variable is again updated it calls render again with new data. You have to check the old query and new query so that render, state variable update doesn't get called in infinite loop.

Sample code: /* Method which is invoked when the column sort order is changed */ handleOrderChange = (orderedColumnId, orderDirection) => {
var columnField = this.props.columns[orderedColumnId].field; this.setState({sorting:{columnName: columnField, direction: orderDirection },loading: true});
};

queryString() { let queryUrl = ${URL};

  const { pageSize, currentPage } = this.state;
  const { filters } = this.state;
  //const { searchValue, columns } = this.state;
  const { sorting } = this.state;

  // Todo: The URL formation will undergo change based on the contract between UI and backend url

  let filter = filters.reduce((acc, { columnName, columnValue }) => {
    acc.push(`["${columnName}", "contains", "${encodeURIComponent(columnValue)}"]`);
    return acc;
  }, []).join(",\"and\",");

  if (filters.length > 1) {
    filter = `[${filter}]`;
  }

  queryUrl += `?filter=${filter}`;

  queryUrl += `&take=${pageSize}&skip=${pageSize * currentPage}`;

  if (sorting.columnName && sorting.columnName.length>0) {
    const sortDirectionString = sorting.direction === "desc" ? " desc" : "";
    queryUrl += `&orderby=${sorting.columnName}${sortDirectionString}`;
  }

  return queryUrl;
}

loadData() {        
  const queryString = this.queryString();
  if (queryString === this.lastQuery) {
    if (this.state.loading !== false) {
      this.setState({ loading: false });
    }
    return;
  }

  fetch(queryString)
    .then(response => response.json())
    .then(data => this.setState({
      rowData: data.items,
      totalCount: data.totalCount,
      loading: false,
      filterApplied:false
    }))
    .catch(() => this.setState({ loading: false }));
  this.lastQuery = queryString;

}

vijthang61 avatar Feb 12 '19 05:02 vijthang61

This is done. You can download 1.21.0.

Try it:

https://mbrn.github.io/material-table/#/docz-examples-14-example-remote-data

mbrn avatar Mar 08 '19 12:03 mbrn

Is it possible to sort and filter server side? Or is it just pagination server side?

roseak avatar Mar 08 '19 14:03 roseak

Hi @roseak ,

It supports search, pagination and sort for now. I will add filtering this weekend.

mbrn avatar Mar 08 '19 15:03 mbrn

@mbrn Is there any update ? I need filtering server-side, too.

revskill10 avatar Mar 29 '19 16:03 revskill10

Sorry, i have forgotten add a comment here:).

query already has a filters field.

mbrn avatar Mar 30 '19 15:03 mbrn

Hi @mbrn , how to use the server-side filtering? I didn't find an example in: https://mbrn.github.io/material-table

hsluoyz avatar Apr 16 '19 13:04 hsluoyz

Any news about an example for server side filtering? Thanks!

jlubeck avatar Jul 13 '19 19:07 jlubeck