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

时间:2010-07-28 14:29:29

标签: java jsf jsf-2

我有一个简单的facelet,以表格格式显示产品列表。在每行的最后一列中,有一个用于标记产品以进行删除的复选框。到目前为止,我必须在每一行上放置一个selectBooleanCheckBox,并在Product实体中有一个“mark for deletion”属性,但我认为它很难看,因为我的模型bean中有一些演示文稿。

有没有一个h:selectManyCheckBox,它的f:selectItem在dataTable的每一行都有分发?

谢谢

4 个答案:

答案 0 :(得分:13)

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

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

E.g。

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}" />

Map<Long, Boolean>将自动填充所有实体的ID作为映射键,复选框值将设置为与实体ID关联的映射值作为键。

另见:

答案 1 :(得分:3)

您可以使用MyFaces Tomahawk的<t:selectManyCheckbox> layout="spread"

答案 2 :(得分: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>

答案 3 :(得分: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"