|
1 | | -# How-to-load-data-into-a-DataGrid-for-each-page-using-JSON |
2 | | -How to load data into a DataGrid for each page using JSON |
| 1 | +# How to load data into a DataGrid for each page using JSON in Flutter DataTable (SfDataGrid). |
| 2 | +In this article, we will show you how to load data into a DataGrid for each page using JSON in [Flutter DataTable](https://www.syncfusion.com/flutter-widgets/flutter-datagrid). |
| 3 | + |
| 4 | +## STEP 1: |
| 5 | + |
| 6 | +Refer to this KB [link](https://support.syncfusion.com/kb/article/10959/how-to-populate-flutter-datatable-datagrid-with-json-api) to populate Flutter DataTable (DataGrid) with JSON API. |
| 7 | + |
| 8 | +## STEP 2: |
| 9 | + |
| 10 | +Initialize the [SfDataGrid](https://pub.dev/documentation/syncfusion_flutter_datagrid/latest/datagrid/SfDataGrid-class.html) and [SfDataPager](https://pub.dev/documentation/syncfusion_flutter_datagrid/latest/datagrid/SfDataPager-class.html) widgets with all the required properties. Instead of supplying the entire data to the DataGrid, provide only the data corresponding to the current page to the [DataGridSource](https://pub.dev/documentation/syncfusion_flutter_datagrid/latest/datagrid/DataGridSource-class.html). |
| 11 | + |
| 12 | +```dart |
| 13 | +Future generateProductList() async { |
| 14 | + // Here we have get the entire data asynchronously. |
| 15 | + var response = await http.get(Uri.parse( |
| 16 | + 'https://ej2services.syncfusion.com/production/web-services/api/Orders?')); |
| 17 | + var list = json.decode(response.body).cast<Map<String, dynamic>>(); |
| 18 | + productlist = |
| 19 | + await list.map<Product>((json) => Product.fromJson(json)).toList(); |
| 20 | + jsonDataGridSource = |
| 21 | + JsonDataGridSource(productlist.getRange(0, rowsPerPage).toList()); |
| 22 | +
|
| 23 | + return productlist; |
| 24 | +} |
| 25 | +
|
| 26 | +@override |
| 27 | + Widget build(BuildContext context) { |
| 28 | + return Scaffold( |
| 29 | + appBar: AppBar( |
| 30 | + title: const Text('Flutter DataGrid Sample'), |
| 31 | + ), |
| 32 | + body: FutureBuilder( |
| 33 | + future: generateProductList(), |
| 34 | + builder: (BuildContext context, AsyncSnapshot<dynamic> snapshot) { |
| 35 | + return snapshot.hasData |
| 36 | + ? LayoutBuilder(builder: (context, constraints) { |
| 37 | + return Column(children: [ |
| 38 | + SizedBox( |
| 39 | + height: constraints.maxHeight - 60, |
| 40 | + width: constraints.maxWidth, |
| 41 | + child: buildDataGrid()), |
| 42 | + SizedBox( |
| 43 | + height: 60, |
| 44 | + width: constraints.maxWidth, |
| 45 | + child: SfDataPager( |
| 46 | + pageCount: (productlist.length / rowsPerPage) |
| 47 | + .ceil() |
| 48 | + .toDouble(), |
| 49 | + delegate: jsonDataGridSource, |
| 50 | + availableRowsPerPage: const [10, 20, 30], |
| 51 | + onRowsPerPageChanged: (int? rowsPerPages) { |
| 52 | + setState(() { |
| 53 | + rowsPerPage = rowsPerPages!; |
| 54 | + jsonDataGridSource.updateDataGriDataSource(); |
| 55 | + }); |
| 56 | + }, |
| 57 | + ), |
| 58 | + ) |
| 59 | + ]); |
| 60 | + }) |
| 61 | + : const Center( |
| 62 | + child: CircularProgressIndicator(strokeWidth: 3)); |
| 63 | + })); |
| 64 | + } |
| 65 | +
|
| 66 | + Widget buildDataGrid() { |
| 67 | + return SfDataGrid( |
| 68 | + columns: getColumns(), |
| 69 | + source: jsonDataGridSource, |
| 70 | + columnWidthMode: ColumnWidthMode.fill); |
| 71 | + } |
| 72 | +``` |
| 73 | + |
| 74 | +## STEP 3: |
| 75 | + |
| 76 | +Then, load the relevant data for the specific page within the [handlePageChange](https://pub.dev/documentation/syncfusion_flutter_datagrid/latest/datagrid/DataGridSource/handlePageChange.html) method. This method is invoked whenever the page is changed through the data pager. |
| 77 | + |
| 78 | +```dart |
| 79 | +class JsonDataGridSource extends DataGridSource { |
| 80 | + ... |
| 81 | +
|
| 82 | +@override |
| 83 | + Future<bool> handlePageChange(int oldPageIndex, int newPageIndex) async { |
| 84 | + int startIndex = newPageIndex * rowsPerPage; |
| 85 | + int endIndex = startIndex + rowsPerPage; |
| 86 | + if (endIndex > productlist.length) { |
| 87 | + endIndex = productlist.length; |
| 88 | + } |
| 89 | + if (startIndex < productlist.length && endIndex <= productlist.length) { |
| 90 | + buildDataGridRow(productlist.getRange(startIndex, endIndex).toList()); |
| 91 | + notifyListeners(); |
| 92 | + } else { |
| 93 | + dataGridRows = []; |
| 94 | + } |
| 95 | +
|
| 96 | + return true; |
| 97 | + } |
| 98 | +} |
| 99 | +``` |
| 100 | + |
| 101 | +You can download this example on [GitHub](https://github.com/SyncfusionExamples/How-to-load-data-into-a-DataGrid-for-each-page-using-JSON). |
0 commit comments