Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/310.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 导叶下拉选择中的分离器_Java_Drop Down Menu_Wicket - Fatal编程技术网

Java 导叶下拉选择中的分离器

Java 导叶下拉选择中的分离器,java,drop-down-menu,wicket,Java,Drop Down Menu,Wicket,在Wicket DropDownChoice的选项列表中添加分隔符有什么明显的方法吗?在我的例子中,我使用数据源中的两种域对象填充选择。我想我可以去手动添加一些虚拟域对象到选择列表中,但它感觉很难看 例如: +---------+-+ | Apple |▼| | Orange +-+ | ------- | | Carrot | | Cucumber| +---------+ 当前代码(不带任何分隔符)如下所示: EntityModel model = getModel(); List

在Wicket DropDownChoice的选项列表中添加分隔符有什么明显的方法吗?在我的例子中,我使用数据源中的两种域对象填充选择。我想我可以去手动添加一些虚拟域对象到选择列表中,但它感觉很难看

例如:

+---------+-+
| Apple   |▼|
| Orange  +-+
| ------- |
| Carrot  |
| Cucumber|
+---------+
当前代码(不带任何分隔符)如下所示:

EntityModel model = getModel();
List<? extends Produce> foods = foodService.getAllProduce(); 
// getAllProduce() returns first all fruits, then all vegetables
add(new DropDownChoice<Produce>(
    "produceSelect", new PropertyModel<Produce>(model, "favProduce"), foods)
);
EntityModel model=getModel();
列表参见
有“源代码”链接。

add(新的下拉选择(“选择”),Arrays.asList(“苹果”、“橙色”、“胡萝卜”、“黄瓜”)){
@凌驾
受保护的无效appendOptionHtml(AppendingStringBuffer缓冲区、字符串选择、int索引、选定字符串){
super.appendOptionHtml(缓冲区、选项、索引、选定项);
如果(“橙色”。等于(选择)){
缓冲区。追加(“”);
}
}
});

我最终使用martin-g提到的
Select
SelectOptions
组件解决了这个问题

SelectOptions<Produce> fruitOptions = new SelectOptions<Produce>(
                                      "fruits",
                                      fruitCollection, 
                                      new FruitRenderer());

SelectOptions<Produce> vegetableOptions = new SelectOptions<Produce>(
                                          "vegetables",
                                          vegetableCollection, 
                                          new VegetableRenderer());

Select select = new Select("produceSelect", 
                           new PropertyModel<Produce>(model, "favProduce"));
select.add(fruitOptions);
select.add(vegetableOptions);

事实是,使用PropertyModel有一个很好的理由(UI更改会自动传播到持久层)。这种方法(给DropDownChoice一个简单的选项列表)会使域对象(和数据库)的更新变得复杂。呃,什么?该示例介绍如何添加分隔符(覆盖
appendOptionHtml()
和追加
)。直接传递一个列表只是使示例编译和运行所能做的最简单的事情。当然,您可以使用
PropertyModel
和自定义类……啊,对不起,我不知怎么地错过了您的要点(覆盖appendOptionHtml())。这确实也适用于PropertyModel。您只需找出第一种类型的最后一个域对象(或者第二种类型的第一个,我想这会更容易)。因此,+1因为这是一个可行的解决方案,至少比从wicket extensions切换到Select&SelectOptions要快。这难道没有问题吗?它不会将
标记包含在
中吗?
标记应该是
的子项。更“语义正确”的方法是使用Select/SelectOption组件,正如@martin-g所指出的。
appendOptionHtml()。我所说的“hack”并不是说它会生成无效的HTML(本身)或给Wicket带来任何麻烦,而是说它不是一个纯粹的、语义正确的HTML(因为选项元素不是按语义分组的)。但可能需要更少的行:)我用这个作为解决方案,请参阅下面我的答案中的详细描述。如果有更新的链接,我希望看到。否则,我不确定这个答案是否仍然有效,可能应该删除。@spaaarky21可能吗?这个解决方案假设您知道这些组将是“水果”和“蔬菜”。如果您不知道预期的组,该怎么办?在这种情况下,您将无法以这种方式使用SelectOption,因为在引擎盖下,它只是一个需要ID的repeatingListView。您可能希望直接在select下创建一个repeating ListView,而该选项又包含一个固定ID的SelectOption?
SelectOptions<Produce> fruitOptions = new SelectOptions<Produce>(
                                      "fruits",
                                      fruitCollection, 
                                      new FruitRenderer());

SelectOptions<Produce> vegetableOptions = new SelectOptions<Produce>(
                                          "vegetables",
                                          vegetableCollection, 
                                          new VegetableRenderer());

Select select = new Select("produceSelect", 
                           new PropertyModel<Produce>(model, "favProduce"));
select.add(fruitOptions);
select.add(vegetableOptions);
<select wicket:id="produceSelect" id="produceSelect">
    <optgroup label="Fruits">
        <wicket:container wicket:id="fruits">
            <option wicket:id="option">Apple</option>
        </wicket:container>
    </optgroup>
    <optgroup label="Vegetables">
        <wicket:container wicket:id="vegetables">
            <option wicket:id="option">Carrot</option>
        </wicket:container>
    </optgroup>
</select>
+----------------+-+
| **Fruits**     |▼|
| Apple          +-+  
| Orange         | 
| **Vegetables** |  
| Carrot         |
| Cucumber       |
+----------------+