Articles in this section
Category / Section

How to update the data pager with proper page count when applying filtering in Flutter?

2 mins read

 

The Syncfusion Flutter DataGrid supports programmatic and excel-like UI filtering of its rows. You can find more information about the filtering in this UG documentation.

 

The DataGrid interactively supports the manipulation of data using the SfDataPager control. This supports load data in segments when dealing with large volumes of data. You can find more information about the paging in this UG documentation.

 

In this article, we will show you how to update the data pager with the proper page count when applying filtering in Flutter DataGrid.

 

To initialize the SfDataGrid and SfDataPager widgets with all necessary properties, declare them and specify their respective properties. The onFilterChanged callback will be triggered each time a filter is applied to the DataGrid. To update the DataPager's page count while filtering the rows, update the pageCount value based on the length of the DataGridSource.effectiveRows within the onFilterChanged callback function.

 

class MyHomePageState extends State<MyHomePage> {
  late EmployeeDataSource _employeeDataSource;
  List<Employee> _employees = [];
  int rowsPerPage = 5;
  double pageCount = 0;
 
  @override
  void initState() {
    super.initState();
    _employees = populateData();
    _employeeDataSource = EmployeeDataSource(_employees);
    _updatePageCount();
  }
 
  void _updatePageCount() {
    final rowsCount = _employeeDataSource.filterConditions.isNotEmpty
        ? _employeeDataSource.effectiveRows.length
        : _employeeDataSource._employeeData.length;
    pageCount = (rowsCount / rowsPerPage).ceil().toDouble();
  }
 
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(title: const Text('PageNavigation Demo')),
        body: LayoutBuilder(builder: (context, constraints) {
          return Column(children: [
            SizedBox(
              height: constraints.maxHeight - 60,
              child: SfDataGrid(
                source: _employeeDataSource,
                columns: getColumns,
                allowFiltering: true,
                rowsPerPage: rowsPerPage,
                onFilterChanged: (details) {
                  setState(() {
                    _updatePageCount();
                  });
                },
              ),
            ),
            SizedBox(
              height: 60,
              child: SfDataPager(
                  pageCount: pageCount, delegate: _employeeDataSource),
            ),
          ]);
        }));
  }
}

View the GitHub sample here.

 

Conclusion

I hope you enjoyed learning about how to update the data pager with proper page count when applying filtering in Flutter.

You can refer to our  Flutter DataGrid feature tour page to know about its other groundbreaking feature representations. You can also explore our Flutter DataGrid documentation to understand how to create and manipulate data.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion®, you can try our 30-day free trial to check out our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forumsDirect-Trac, or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied