Hi, guys!
I have a doubt regarding filtering a list of Observables which has a complex object. So, considering this example:
var searchString = Observable("");
fruits = Observable(
{ name: "Apple" , color: "red" },
{ name: "Lemon" , color: "yellow" });
var filteredList = searchString.flatMap(function(searchTerm) {
return fruits.where(function(fruit) {
return stringContainsValue(fruit.name, searchTerm);
});
});
function stringContainsValue(main, filter){
return main.toLowerCase().trim().indexOf(filter.toLowerCase().trim()) != -1;
}
This works ok in scenarios where I have a ux file like this:
<Each Items="{filteredList}">
<FruitsList />
</Each>
...
But, when considering this scenario:
fruits = Observable(
{ name: "Apple" , color: "red", categories: [{category: "category 1", type: "type 1"}, {category: "category 2", type: "type 2"}] },
{ name: "Lemon" , color: "yellow", categories: [{category: "category 3", type: "type 3"}, {category: "category 4", type: "type 4"}] });
And, in the ux file:
<Each Items="{filteredList}">
<FruitsList />
</Each>
...
<Panel ux:Class="FruitsList">
<Panel ux:Name="header">
<Text Value="{name}" />
</Panel>
<Each Items="{categories}">
<Text Value="{category}" />
<Text Value="{type}" />
</Each>
</Panel>
How can I adapt the filteredList function to work in this case, when I have a nested “Each”? I tried something like this with no success:
var filteredList = searchString.flatMap(function(searchTerm) {
return fruits.where(function(fruit) {
return fruit.where(function(cat) {
return stringContainsValue(cat.categories.name, searchTerm);
});
});
});
I need a list with a header and, for each headers, a list of categories which I’m allowed to filter based on a condition.
Thanks in advance!