mirror of
				https://github.com/inventree/inventree-app.git
				synced 2025-10-29 20:40:35 +00:00 
			
		
		
		
	Allow model class to be filtered
- Each subclass should provide a custom filtering function
This commit is contained in:
		| @@ -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; | ||||
|   } | ||||
|  | ||||
|  | ||||
| } | ||||
|  | ||||
|   | ||||
| @@ -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; | ||||
|   } | ||||
| } | ||||
| @@ -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}", | ||||
|   | ||||
| @@ -27,7 +27,6 @@ class _StockItemDisplayState extends State<StockItemDisplayWidget> { | ||||
|     if (item == null) { | ||||
|       return "Stock Item"; | ||||
|     } else { | ||||
|       print(item.jsondata); | ||||
|       return "Item: x ${item.partName}"; | ||||
|     } | ||||
|   } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user