GWT ListBox - how to have a listbox item disabled?

别等时光非礼了梦想. 提交于 2019-12-12 08:33:55

问题


How can I have the first item in a listbox disabled? Following is my code:

ListBox list  = new ListBox();
list.addItem("Select an item");
list.addItem("a");
list.addItem("b");
list.addItem("c");

How do I disable the first item in list? Thanks so much


回答1:


You could select the first child element and set the disabled atribute:

list.getElement().getFirstChildElement().setAttribute("disabled", "disabled");



回答2:


For anyone who finds this page and is looking to disable a GWT ListBox option which is NOT the first option in the ListBox, this worked for me:

list.getElement().getElementsByTagName("option").getItem(itemNumber).setAttribute("disabled", "disabled");

Where itemNumber is the option you would like to disable.




回答3:


list.getElement().<SelectElement>cast().getOptions().getItem(0).setDisabled(true‌​);



回答4:


On six lines:

 private void disableOption(ListBox listBox, int optionNumber, boolean disable){
      if(optionNumber<=listBox.getElement().getChildCount()){
            Element element = listBox.getElement().getFirstChildElement();
            while (--optionNumber > 0){
                element = element.getNextSiblingElement();
            }
            element.setAttribute("disabled", disable ? "true" : "false");
      }
 }


来源:https://stackoverflow.com/questions/6269878/gwt-listbox-how-to-have-a-listbox-item-disabled

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!