I try to implement the lazy option of the DataTable
. In the official example, only the onPage
event is being used. But what if I need to use column filters, paging and sorting together? Is this the workflow which I need to develop?
- client collects all sort, filter, paging in a
parameter list
- client send the
parameter list
to the backend - backend filters the data according to the
parameter list
- backend sends the new data to the client
- client updates the datatable by updating the state with the new updated data
If this is the workflow, then how do I do that since I don't see any global event which collects all the parameters from the DataTable
.
I've created a test case where the CarService
class supposed to filter the data based on the event parameters all together. Consider this CarService
as the backend.
App
export class App extends Component<{}, State> {
carService = new CarService();
constructor(props) {
super(props);
this.state = {
cars: this.carService.getCars()
};
}
onSort = (event: any) => {
this.setState({ cars: this.carService.getFilteredCars(event) });
};
onPage = (event: any) => {
this.setState({ cars: this.carService.getFilteredCars(event) });
};
onFilter = (event: any) => {
this.setState({ cars: this.carService.getFilteredCars(event) });
};
render() {
return (
<div className="App">
<DataTable
value={this.state.cars}
paginator={true}
lazy={true}
rows={2}
rowsPerPageOptions={[2, 4]}
onSort={this.onSort}
onPage={this.onPage}
onFilter={this.onFilter}
>
<Column field="vin" header="Vin" filter sortable />
<Column field="year" header="Year" filter sortable />
</DataTable>
</div>
);
}
}
CarService
export class CarService {
cars = [
{ vin: "aaa", year: 1980 },
{ vin: "bbb", year: 1981 },
{ vin: "ccc", year: 1982 },
{ vin: "ccc", year: 1983 },
{ vin: "csdd", year: 1984 },
{ vin: "cgg", year: 1982 },
{ vin: "cyy", year: 1982 }
];
getCars() {
return this.cars;
}
getFilteredCars(filter: string) {
// filter the cars based on the filter
// an example filter could be: ?name=TestName&page=1
return this.cars;
}
}