【问题标题】:How to check another checkbox when click first checkbox? My checkbox list retrieve from datafile单击第一个复选框时如何检查另一个复选框?我的复选框列表从数据文件中检索
【发布时间】:2019-04-12 01:50:02
【问题描述】:

我是 jsf 的新手。我的复选框列表从数据表中检索。如果选中了 documentId 101 的复选框,系统应该自动选择另一个 documentId 102 的复选框。如何编码这个问题?

<p:dataTable id="popup1" var="comp1" rows="10" 
   value="#{ExaBackingBean.managedBean.popupcomp1List}" 
   editable="true" 
   selection="#{ExaBackingBean.managedBean.popupcomp1Select}" 
   rowKey="#{comp1.documentId}" rowIndexVar="index"> 
  <ac:paginator for="popup1"></ac:paginator> 

<p:column style="width:3px;text-align:center;" > 
<p:selectBooleanCheckbox value="#{comp1.selected}"> 
   <p:ajax listener="#{ExaBackingBean.ckechboxSelectPairingAction(comp1.documentId)}" partialSubmit="true" process="@this" update="@([id$=CompChecklist])" /> 
</p:selectBooleanCheckbox> 
</p:column> 

// ExaBackingBean
public void ckechboxSelectPairingAction(int documentId) throws Exception { 

if (documentId == 101) { 
    System.out.println("documentId test"+documentId); 
    --- checkbox101 & checkbox102 will check
}

【问题讨论】:

  • 显示您的 BackedBean 代码。
  • 下面的 来自 backedbean 代码。 // ExaBackingBean public void ckechboxSelectPairingAction(int documentId) throws Exception {
  • 目前 System.out.println("documentId test"+documentId);成功显示.. 10:38:02,090 INFO [stdout] (http-localhost-127.0.0.1-8080-1) documentId 101 -------- 但我不知道为复选框输入什么编码。 . 我尝试了很多方法,但都失败了。请帮帮我.. tq.

标签: jsf


【解决方案1】:

首先,您要显示多个复选框,那么您应该使用selectManyCheckbox 而不是selectBooleanCheckbox

让我们创建一个伪示例,如何根据其他值选择一些值:

HTML 代码

<p:selectManyCheckbox id="basic" value="#{bean.selectedItems}">
    <f:selectItems value="#{bean.availableItems}" />
    <p:ajax listener="#{bean.someLogic}" update="someComponent"/>
</p:selectManyCheckbox>

BackedBean

private Map<String, String> availableItems; // +getter (no setter necessary)
private List<String> selectedItems; // +getter +setter

@PostConstruct
public void init() {
    availableItems = new LinkedHashMap<String, String>();
    availableItems.put("Document1 label", "document1");
    availableItems.put("Document2 label", "document2");
    availableItems.put("Document3 label", "document3");
}

public void someLogic() {
    boolean contains = selectedItems.contains("document1");
    if (contains) {
        selectedItems.add("document2");
    }
}

【讨论】:

  • tq。如果数据表中的可用项目怎么样?从我的编码中,选择列表来自 value="#{ExaBackingBean.managedBean.popupcomp1List}"
  • @RuzaimahSamat,这是示例,您可以自己采用。您的数据来自 DB,而不是 availableItems
  • 我会尝试... tqvm.
  • @RuzaimahSamat,如果答案有帮助,请将其作为正确答案。谢谢。
  • Tqvm @BSeitkazin 为您提供有用的答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-11-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-04
  • 1970-01-01
相关资源
最近更新 更多