aguilaair / lazy_data_table_plus

BSD 2-Clause "Simplified" License
2 stars 1 forks source link

How to add rows from custom list #1

Closed hamzamon closed 3 years ago

hamzamon commented 3 years ago

// Example List

class SalesHistory {
  int orderId, productId;
  String date, item;
  double price, cost, profit;

  SalesHistory({this.orderId, this.productId, this.date, this.item, this.price, this.cost, this.profit});

  factory SalesHistory.fromJson(Map<String, dynamic> json) {
    return SalesHistoryModel(
      orderId : int.parse(json['id']),
      productId : int.parse(json['product_id']),
      date : json['date'],
      item : json['item'],
      price : double.parse(json['price']),
      cost : double.parse(json['cost']),
      profit : double.parse(json['profit']))
    );
  }
}

List<SalesHistory> salesHistory = new List <SalesHistory>();

      columnHeaderBuilder: (i) => Center(child: _columns[i]),
      dataCellBuilder: (i, j) => Text( _How to add data here from_ **salesHistory** ),

  final List<Widget> _columns =[
    Text('DATE'),
    Text('ORDER#'),
    Text('ITEM'),
    Text('PRICE'),
    Text('COST'),
    Text('PROFIT'),
  ];

Thank you.

Update by aguilaair (Code formatting)

aguilaair commented 3 years ago

Hey!

Thanks for filing issue #1! As soon as I get some time I'll send over an example.

aguilaair commented 3 years ago

In the meantime, there's multiple ways to do it. What you're looking to iterate in for each You could:

a) Maybe the easiest, use ifs, so if i is 1 then you'd render the date, likewise for the rest b) Another option would be to have a map that links int i to a certain piece of data in SalesHistory, for example

Map<int, SalesHistory> _map = {
    0: salesHistory[j].date,
    1: salesHistory[j].orderId,
    2: salesHistory[j].item,
    3: salesHistory[j].price,
    4: salesHistory[j].cost,
    5: salesHistory[j].profit,
  };

This might be placed inside the class, and may not use j but grab the data directly from the JSON.

If you need any other help or further guidance please let me know! This should give you some ideas on how to tackle it.

hamzamon commented 3 years ago

In the meantime, there's multiple ways to do it. What you're looking to iterate in for each You could:

a) Maybe the easiest, use ifs, so if i is 1 then you'd render the date, likewise for the rest b) Another option would be to have a map that links int i to a certain piece of data in SalesHistory, for example

Map<int, SalesHistory> _map = {
    0: salesHistory[j].date,
    1: salesHistory[j].orderId,
    2: salesHistory[j].item,
    3: salesHistory[j].price,
    4: salesHistory[j].cost,
    5: salesHistory[j].profit,
  };

This might be placed inside the class, and may not use j but grab the data directly from the JSON.

If you need any other help or further guidance please let me know! This should give you some ideas on how to tackle it.

Thank you for your prompt reply, I have tried with given hint as

    dataCellBuilder: (i, j) => Text(_map[i][j]),

Map<int, SalesHistory> _map = { 0: salesHistory[j].date, 1: salesHistory[j].orderId, 2: salesHistory[j].item, 3: salesHistory[j].price, 4: salesHistory[j].cost, 5: salesHistory[j].profit, };

But actully The element type 'String' can't be assigned to the map value type 'SalesHistory'. Your help highly appreciated.

aguilaair commented 3 years ago

Yep, that was my bad, forgot to change it. Try putting dynamic or String instead of SalesHistory in the map type.

hamzamon commented 3 years ago

In the meantime, there's multiple ways to do it. What you're looking to iterate in for each You could: a) Maybe the easiest, use ifs, so if i is 1 then you'd render the date, likewise for the rest b) Another option would be to have a map that links int i to a certain piece of data in SalesHistory, for example

Map<int, SalesHistory> _map = {
    0: salesHistory[j].date,
    1: salesHistory[j].orderId,
    2: salesHistory[j].item,
    3: salesHistory[j].price,
    4: salesHistory[j].cost,
    5: salesHistory[j].profit,
  };

This might be placed inside the class, and may not use j but grab the data directly from the JSON. If you need any other help or further guidance please let me know! This should give you some ideas on how to tackle it.

Thank you for your prompt reply, I have tried with given hint as

    dataCellBuilder: (i, j) => Text(_map[i][j]),

Map<int, SalesHistory> _map = { 0: salesHistory[j].date, 1: salesHistory[j].orderId, 2: salesHistory[j].item, 3: salesHistory[j].price, 4: salesHistory[j].cost, 5: salesHistory[j].profit, };

But actully The element type 'String' can't be assigned to the map value type 'SalesHistory'. Your help highly appreciated.

Can you please give me an example to define 'j'

aguilaair commented 3 years ago

Maybe a function along these lines can do the job:

Text getDataCell(i, j, List<int> salesHistory){
  Map<int, dynamic> _map = {
    0: salesHistory[j].date,
    1: salesHistory[j].orderId,
    2: salesHistory[j].item,
    3: salesHistory[j].price,
    4: salesHistory[j].cost,
    5: salesHistory[j].profit,
  };
  return Text(_map[i]);
}

Basically, getDataCell will accept i, j and the list. Then, as j is the column or the item number we pick that. Lastly, we return the piece of data needed.

hamzamon commented 3 years ago

Done, Thank you very much.

aguilaair commented 3 years ago

My pleasure!