2
0
mirror of https://github.com/inventree/inventree-app.git synced 2025-04-28 13:36:50 +00:00

Allow model class to be filtered

- Each subclass should provide a custom filtering function
This commit is contained in:
Oliver Walters 2020-04-03 14:27:39 +11:00
parent ee7ab5308e
commit 728597fcdc
4 changed files with 50 additions and 2 deletions

View File

@ -142,6 +142,25 @@ class InvenTreeModel {
// TODO - Define a 'save' / 'update' function
// Override this function for each sub-class
bool matchAgainstString(String filter) => false;
// Filter this item against a list of provided filters
// Each filter must be matched
// Used for (e.g.) filtering returned results
bool filter(String filterString) {
List<String> filters = filterString.trim().toLowerCase().split(" ");
for (var f in filters) {
if (!matchAgainstString(f)) {
return false;
}
}
return true;
}
}

View File

@ -44,6 +44,15 @@ class InvenTreeStockLocation extends InvenTreeModel {
var loc = InvenTreeStockLocation.fromJson(json);
return loc;
}
@override
bool matchAgainstString(String filter) {
if (name.toLowerCase().contains(filter)) return true;
if (description.toLowerCase().contains(filter)) return true;
return false;
}
}

View File

@ -28,6 +28,17 @@ class _LocationDisplayState extends State<LocationDisplayWidget> {
List<InvenTreeStockLocation> _sublocations = List<InvenTreeStockLocation>();
String _locationFilter = '';
List<InvenTreeStockLocation> get sublocations {
if (_locationFilter.isEmpty || _sublocations.isEmpty) {
return _sublocations;
} else {
return _sublocations.where((loc) => loc.filter(_locationFilter)).toList();
}
}
List<InvenTreeStockItem> _items = List<InvenTreeStockItem>();
String get _title {
@ -86,7 +97,17 @@ class _LocationDisplayState extends State<LocationDisplayWidget> {
textAlign: TextAlign.left,
style: TextStyle(fontWeight: FontWeight.bold),
),
Expanded(child: SublocationList(_sublocations)),
TextField(
decoration: InputDecoration(
hintText: "Filter locations",
),
onChanged: (text) {
setState(() {
_locationFilter = text.trim().toLowerCase();
});
},
),
Expanded(child: SublocationList(sublocations)),
Divider(),
Text(
"Stock Items - ${_items.length}",

View File

@ -27,7 +27,6 @@ class _StockItemDisplayState extends State<StockItemDisplayWidget> {
if (item == null) {
return "Stock Item";
} else {
print(item.jsondata);
return "Item: x ${item.partName}";
}
}