DataTable:从选定的行中获取第一个单元格的值 [英] DataTable: Get the value of the first cell from a selected row
本文介绍了DataTable:从选定的行中获取第一个单元格的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试通过检索第一个单元格(id)的值并将其传递给deleteRow函数从DataTable中删除选定的行.如何获得该值?
I'm trying to delete a selected row from a DataTable by retrieving the value of the first cell (id) and passing it to the deleteRow function. How do I get this value?
child: DataTable(
columns: data.first.keys
.map((dynamic keys) => DataColumn(label: Text(keys.toString())))
.toList(),
rows: data.map((map) {
return DataRow(
onSelectChanged: (bool selected) {
if (selected) {
try {
//determine the id of the selected row
int selectedId = /*get index of selected row and return value of the first cell*/
//delete row associated with id in db
deleteRow(selectedId);
//update ui to remove that row
setState(() {});
} catch (e) {
return SnackBar(
content: Text('Cant delete this row. EXCEPTION: $e'),
);
}
}
},
cells: map.values.map((dynamic val) => DataCell(Text(val.toString()))).toList());
}).toList()),
推荐答案
List<Map> data = [], selected = [];
@override
void initState() {
super.initState();
data.add({'id': 'Emp1', 'name': 'Naveen', 'salary': 50000});
data.add({'id': 'Emp3', 'name': 'Satish', 'salary': 35000});
data.add({'id': 'Emp2', 'name': 'Ram', 'salary': 40000});
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Container(
padding: EdgeInsets.all(15),
alignment: Alignment.center,
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Text(
selected.length < 1
? 'Nothing Selected !'
: 'First cell : ${selected[0].values.toList()[0]}',
textAlign: TextAlign.center),
DataTable(
columns: data.first.keys
.map((dynamic keys) =>
DataColumn(label: Text(keys.toString())))
.toList(),
rows: data.map((map) {
return DataRow(
selected: selected.contains(map),
onSelectChanged: (boo) {
if (boo) {
setState(() {
selected.add(map);
});
} else {
setState(() {
selected.remove(map);
});
}
},
cells: map.values.map((value) {
return DataCell(Text(value.toString()));
}).toList());
}).toList()),
],
),
));
}
这篇关于DataTable:从选定的行中获取第一个单元格的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文