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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3353880/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 01:26:52  来源:igfitidea点击:

JSF: display h:selectManyCheckBox vertically in h:dataTable

javajsfjsf-2

提问by robinmag

I have a simply facelet which display a list of products in tabular format. In the last column of each row, there is a checkbox used to mark the product for deletion. Until now i have to put a selectBooleanCheckBox on each row and have a "mark for deletion" property in the Product entity but i think it's ugly because i have some presentation stuff in my model bean.

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

Is there anyway to have a h:selectManyCheckBox which has its f:selectItem distribute on each row of the dataTable ?

反正有 ah:selectManyCheckBox ,它的 f:selectItem 分布在 dataTable 的每一行上吗?

Thank you

谢谢

回答by BalusC

The t:selectManyCheckbox layout="spread"is an excellent suggestion.

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

As an alternative, you can also just bind the h:selectBooleanCheckboxcomponent to a Map<Long, Boolean>property where Longrepresents the entity ID (or whatever identifier which you can use to identify the row) and Booleanrepresents the checked state.

作为替代方案,您也可以将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.
            }
        }
    }

    // ...
}

with

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

The Map<Long, Boolean>will be automagically filled with the ID of all entities as map keys and the checkbox value is set as map value associated with the entity ID as key.

Map<Long, Boolean>将被自动地填充所有实体的ID作为地图键和复选框值被设定为与该实体ID作为键相关联的地图的值。

See also:

也可以看看:

回答by Bozho

You can, using MyFaces Tomahawk's <t:selectManyCheckbox>with layout="spread"

你可以,使用 MyFaces Tomahawk's<t:selectManyCheckbox>layout="spread"

回答by Sanchi Girotra

In my application, i have used below set of code to get multiple checkbox list to be displayed vertically with scrollbar:

在我的应用程序中,我使用下面的一组代码来获取多个复选框列表,以使用滚动条垂直显示:

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

回答by Daniel Costa

The best way to use selectManyCheckbox and dataTable is...

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

=== Page.xhtml ===

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

=== 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
   }
}

// Gets and sets

// 获取和设置

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

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

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