-
FEATURED COMPONENTS
First time here? Check out the FAQ!
Hello,
I have a site in MVVM. There is a Grid, with checboxmenu. The case is to use that menu for filtering. Whatever is checked, should be visible in a grid. I generate menu items dynamically in VeiwModel class:
public void populateMenuItems(){ for (String cat:cats) {
Menuitem item = new Menuitem(cat.toString());
item.setAutocheck(true);
item.setCheckmark(true);
item.setChecked(true);
System.out.print(item);
item.setCheckmark(true);
mp_cat.appendChild(item);
}
}
Additionally I call it in doAfterCompose method and have onOpen event:
@Override
public void doAfterCompose(Component comp) throws Exception {
super.doAfterCompose(comp);
populateMenuItems();
}
@Listen("onOpen=#mp_cat")
public void onOpenListen(OpenEvent event){
System.out.print(event.isOpen());
if(event.isOpen() && mp_cat.getChildren().size()==0){
populateMenuItems();
}
}
Now the problem is, to update existing filtering method (from data filter example): public static List<food> getFilterFoods(FoodFilter foodFilter) { List<food> somefoods = new ArrayList<food>(); String cat = foodFilter.getCategory().toLowerCase(); String nm = foodFilter.getName().toLowerCase(); String nut = foodFilter.getNutrients().toLowerCase();
for (Iterator<Food> i = foods.iterator(); i.hasNext();) {
Food tmp = i.next();
if (tmp.getCategory().toLowerCase().contains(cat) &&
tmp.getName().toLowerCase().contains(nm) &&
tmp.getTopNutrients().toLowerCase().contains(nut)) {
somefoods.add(tmp);
}
}
return somefoods;
}
The question is, how to update this method so it will be able to filter whatever was checked (meaning more than one argument) and what to write in zul file..
Oke I think I know what you want.
Mine first comment is :Please, do not mix MVVM and MVC.
@Listen
is MVC.
Now, I think you have to work with a map.
private Map<String,Menuitem> menuItems = new HashMap<String,Menuitem>();
public void populateMenuItems(){
for (String cat:cats) {
Menuitem item = new Menuitem(cat.toString());
item.setAutocheck(true);
item.setCheckmark(true);
item.setChecked(true);
System.out.print(item);
item.setCheckmark(true);
mp_cat.appendChild(item);
menuItems.put(cat,item);
}
}
So now you can always find the reference between a category and the menuitem.
Filter:
public static List<food> getFilterFoods(FoodFilter foodFilter) {
List<food> somefoods = new ArrayList<food>();
String cat = foodFilter.getCategory().toLowerCase();
String nm = foodFilter.getName().toLowerCase();
String nut = foodFilter.getNutrients().toLowerCase();
for (Iterator<Food> i = foods.iterator(); i.hasNext();) {
Food tmp = i.next();
if (checkCategory(i) &&
tmp.getName().toLowerCase().contains(nm) &&
tmp.getTopNutrients().toLowerCase().contains(nut)) {
somefoods.add(tmp);
}
}
return somefoods;
}
private boolean checkCategory (Food food) {
for (String cat : menuItems.keySet()) {
if (o.get(cat).isChecked() && food.getCategory().toLowerCase().contains(cat)) {
return true;
}
}
return false;
}
Pretty easy implementation, just need good thinking about it.
Hope this helped you.
Greetz chill.
Asked: 2015-02-05 09:23:59 +0800
Seen: 17 times
Last updated: Feb 06 '15
is this really the use case to have it checked? otherwise don't use filter if textbox is empty, this is the most common situation.
chillworld ( 2015-02-06 06:19:58 +0800 )editUnfortunately this is what have been requested, and this is how I need to design it. I have changed the method already, but it never gest called as I don't know how to call it inside menupop in the zul.
anetkia102 ( 2015-02-06 09:10:45 +0800 )editOk - that sounds very good, and how do I connect it to zull file? as I shouldn't be using @listen and @wire in MVVM
anetkia102 ( 2015-02-06 10:52:35 +0800 )editdid you extend selectorcomposer in your class? => MVC. MVVM works with @Command,.. (I think you are just doing MVC so no problem then cause your code is MVC and you only reffer MVVM in your text)
chillworld ( 2015-02-06 11:06:17 +0800 )editI did extend selectcomposer class. Unfortunatelly I cannot post an answer for two days, as I am a new user. Is there any place I can contact you to discuss further?
anetkia102 ( 2015-02-06 11:37:13 +0800 )edit