使用行和列中的动态数据对PaginatedDataTable进行排序

时间:2019-11-02 10:26:50

标签: android sorting flutter dynamic dart

我正在使用PaginatedDataTable来显示我的数据。数据来自json。我可以显示数据并使用分页。但是排序在我的代码中不起作用。

我正在使用this code作为参考。在参考中排序工作正常。但在此代码中只有行是动态的。

我动态接收列和行,但我不知道如何使用动态数据进行排序。

这是我的DataGrid,其中定义了PaginatedDataTable:

import 'dart:convert';

import 'package:boshloo/Dashboard/ChartInfo.dart';
import 'package:flutter/material.dart';

import 'BarChart.dart';
import 'DataGrid.dart';
import 'GaugeChart.dart';
import 'LineChart.dart';
import 'PieChart.dart';
import 'package:http/http.dart' as http;

class Dashboard extends StatefulWidget {
  @override
  _DashboardState createState() => _DashboardState();
}

class _DashboardState extends State<Dashboard> {
  List<ChartInfo> charts = List();
  bool isLoaded = false;
  @override
  void initState() {
    super.initState();
    _getCharts().then((ch) {
      setState(() {
        charts = ch;
        isLoaded = true;
      });
    });
  }

  Future<List<ChartInfo>> _getCharts() async {
    List<ChartInfo> charts = List();
    String url =
        'http://ls.arian.co.ir:8081/api/1.0/arian/mobiledashboard/dashlet-list/71';
    //print('body = ' + body);

    http.Response response = http.Response('{}', 200);
    response = await http.get(url);
    //print('respons body = ' + response.body);
    try {} catch (e) {
      print('failed' + e.toString());
      return null;
    }

    List<dynamic> json = jsonDecode(response.body);
    for (var i = 0; i < json.length; i++) {
      //print('json[i]["domain-title"] = ' + json[i]['domain-title'][0]);
      ChartInfo chart;
      if (json[i]['title'] == 'تست گرید ') {
        chart = ChartInfo(
          title: json[i]['title'],
          type: 'grid',
          domainTitle: json[i]['domain-title'],
          measureTitle: json[i]['measure-title'],
          measures: json[i]['measure'],
          gridFields: json[i]['fields'],
          gridData: json[i]['data'],
        );
      } else {
        chart = ChartInfo(
          title: json[i]['title'],
          type: json[i]['type'],
          domainTitle: json[i]['domain-title'],
          measureTitle: json[i]['measure-title'],
          measures: json[i]['measure'],
        );
      }
      //print('title' + chart.title);
      //print('chart measure = ' + chart.measures.values.toList()[0].toString());

      charts.add(chart);

      //print('measures ' + chart.measures[0][1].toString());
    }
    return charts;
  }

  @override
  Widget build(BuildContext context) {
    List<Widget> _getChartsWidget() {
      List<Widget> chartList = List();
      for (var i = 0; i < charts.length; i++) {
        if (charts[i].type == 'bar' || charts[i].type == 'column') {
          //chartList.add(BarChart(chartsInfo: charts[i]));
        } else if (charts[i].type == 'pie') {
          //chartList.add(PieChart(chartsInfo: charts[i],));
        } else if (charts[i].type == 'line') {
          //chartList.add(LineChart(chartsInfo: charts[i]));
        } else if (charts[i].type == 'grid'){
          chartList.add(DataGrid(chartsInfo: charts[i]));
        }
      }
      chartList.add(GaugeChart());
      return chartList;
    }

    return Scaffold(
      body: isLoaded
          ? Container(
              child: ListView(
                children: _getChartsWidget(),
              ),
            )
          : Container(),
    );
  }
}

这是仪表板,我在其中接收json并显示DataGrid:

import math


def sum_digits3(n):
   r = 0
   while n:
    if (n%10)==0:
        return 0
        break
    r, n = r + (n % 10)*(n%10), n // 10
   return r

def isPerfectSquare(x): 

    # Find floating point value of  
    # square root of x. 
    sr = math.sqrt(x) 

    # If square root is an integer 
    return ((sr - math.floor(sr)) == 0) 
T =int(input())
for t in range(T):
    N=int(input())
    rstart=10**(N-1)
    rend=10**(N)-1
    flag=0
    for j in range(rstart,rend+1):
        sum1=sum_digits3(j)
        if sum1!=0 and isPerfectSquare(sum1):
            print(j)
            flag=1
            break
    if flag==0:
        print(-1)

The Json Link,(数组中的第三个索引号2)是需要在表格中显示的数据。

“字段”(仅键,值无用)是我的列,“数据”是我的行

(我认为您不需要包含tho的json文件)。

任何帮助将不胜感激。

0 个答案:

没有答案