1

Bind to menu

asked 2015-02-05 09:23:59 +0800

anetkia102 gravatar image anetkia102
21 1

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..

delete flag offensive retag edit

Comments

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 )edit

Unfortunately 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 )edit

Ok - 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 )edit

did 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 )edit

I 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

1 Answer

Sort by ยป oldest newest most voted
1

answered 2015-02-06 10:16:07 +0800

chillworld gravatar image chillworld flag of Belgium
5322 4 9
https://github.com/chillw...

updated 2015-02-06 10:16:23 +0800

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.

link publish delete flag offensive edit
Your answer
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!

[hide preview]

Question tools

Follow
1 follower

RSS

Stats

Asked: 2015-02-05 09:23:59 +0800

Seen: 17 times

Last updated: Feb 06 '15

Support Options
  • Email Support
  • Training
  • Consulting
  • Outsourcing
Learn More