JSF:在h:dataTable中垂直显示h:selectManyCheckBox

3
我有一个简单的Facelet页面,以表格形式显示产品列表。在每行的最后一列中,有一个复选框用于标记要删除的产品。到目前为止,我必须在每行上放置一个selectBooleanCheckBox,并在Product实体中拥有一个“标记删除”的属性,但我认为这很丑陋,因为我的模型Bean中有一些演示内容。
是否有任何方法可以在每个dataTable行上分发f:selectItem的h:selectManyCheckBox?
谢谢
4个回答

13

t:selectManyCheckbox layout="spread"是一个很好的建议。

作为替代方案,您还可以将h:selectBooleanCheckbox组件绑定到Map<Long, Boolean>属性,其中Long表示实体ID(或您可以用来标识行的任何标识符),Boolean表示选中状态。

例如:

public class Bean {
    private List<Entity> entities;
    private Map<Long, Boolean> checked = new HashMap<Long, Boolean>();

    public void submit() {
        for (Entity entity : entities) {
            if (checked.get(entity.getId())) {
                // Entity is checked. Do your thing here.
            }
        }
    }

    // ...
}

使用

<h:dataTable value="#{bean.entities}" var="entity">
    <h:column>
        <h:selectBooleanCheckbox value="#{bean.checked[entity.id]}" />
    </h:column>
    ...
</h:dataTable>
<h:commandButton value="submit" action="#{bean.submit}" />
< p > Map<Long, Boolean> 将自动填充所有实体的ID作为映射键,并将复选框值设置为与实体ID关联的映射值作为键。

另请参阅:


3

1
在我的应用程序中,我使用了以下代码来获取多个复选框列表以垂直方式显示,并带有滚动条:
<style type="text/css">
#productCategoryId label {
  float: inherit;
  font-size: 10px!important;
  font-weight: normal;
}
#productCategoryId table.formTable th, table.formTable td {
  padding: 0px 0px 0 0;
}
</style>

<div style="width:200px;height: 280px;overflow-y:scroll;overflow-x:hidden;border:1px solid #999;" max-height=280px>
             <h:selectManyCheckbox  id="productCategoryId" layout="pageDirection" style="width:200px" styleClass="changeId">
                  <f:selectItem itemValue="-1000" itemLabel="ALL" />
                  <f:selectItems value="#{lookup.list['RSM_LOOKUP']['PRODUCT_CATEGORY']}"/>
                </h:selectManyCheckbox >
            </div>

0

使用selectManyCheckbox和dataTable的最佳方法是...

=== Page.xhtml ===

<ice:selectManyCheckbox id="idSelectManyCheckbox" layout="spread"
   value="#{MyBean.selectedsValuesCheckbox}" >
   <f:selectItems value="#{MyBean.selectItemsCheck}"/>
</ice:selectManyCheckbox>

<ice:dataTable varStatus="rowVar"
   value="#{MyBean.listOfMyObjects}" var="anyNameVar">

   <ice:column>
      <ice:checkbox for="idSelectManyCheckbox" index="#{rowVar.index}" />
   </ice:column>
   <ice:column>
      <ice:outputText value="#{anyNameVar.property1}" />
   </ice:column>

   <!-- ... more columns .. -->
</ice:dataTable>

=== MyBean.java ===

private List<MyObject> listOfMyObjects = new ArrayList<MyObject>(3);
private List<String> selectedsValuesCheckbox = new ArrayList<String>(2);
private SelectItem[] selectItemsCheck = new SelectItem[3];

private handleSelectItemsCheck(){
   int idx = 0;
   selectedsValuesCheckbox.add("1");
   selectedsValuesCheckbox.add("3");
   for (MyObject myObject : listOfMyObjects) {
      selectItemsCheck[idx++] = 
    new SelectItem(myObject.property1, myObject.property2); // value and label
   }
}

// 获取和设置

================================================================

*you must use layout="spread" in that situation.
*in the table the checkboxs 1 and 3 will be selected. because "selectedsValuesCheckbox"

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接