Skip to content

Synctest-hub/How-to-update-the-data-pager-with-proper-page-count-when-applying-filtering-in-Flutter-DataTable

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

The Syncfusion Flutter DataGrid supports both 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 SfDataPager control. This provides support to 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 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),
            ),
          ]);
        }));
  }
}

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 3

  •  
  •  
  •