简体   繁体   English

如何在质数中有多于一列的复选框

[英]how to have more than one column of checkboxes in primefaces

I need to do a user permissions grid hopefully using a primefaces datatable. 我希望使用primefaces数据表来做一个用户权限网格。 For each row: Column one is a class name, Column two is a checkbox for permission for one of two subclasses. 对于每一行:第一列是类名,第二列是两个子类之一的许可复选框。 Column three is a checkbox for permission for the other subclass. 第三列是允许其他子类使用的复选框。 The headers of columns two and three must have the subclass title plus a checkbox, the toggles all the other checkboxes in that subclass. 第2列和第3列的标题必须具有子类标题以及一个复选框,该切换将切换该子类中的所有其他复选框。 I think I can use two or three datatables side by side inside a scrollpanel but I'm having issues lining up the column text to the left of the checkbox (it wants to go on top). 我想我可以在滚动面板内并排使用两个或三个数据表,但是在将复选框列的文本对齐复选框的左侧时出现了问题(它想放在顶部)。
Another thing I tried was to just have a selectbooleancheckbox for each row for each subclass and then have a listener for the column header checkbox that iterates through each item in the list and set the value accordingly. 我尝试过的另一件事是,每个子类的每一行都具有一个selectbooleancheckbox,然后为列标题复选框提供了一个侦听器,该复选框遍历列表中的每个项目并相应地设置了值。 The issue I have here is that when I update the datatable, then all the checkboxes get stuck in the 'checked' state. 我这里的问题是,当我更新数据表时,所有复选框都停留在“已检查”状态。

This is what my grid: ----------------------------------------| 这是我的网格:---------------------------------------- |

| | Class | 类| [] subclass 1 | []子类别1 | [] subclass 2 | []子类别2 |

| | Cls A | Cls A | [] | [] | [] | [] |

| | Cls B | Cls B | [] | [] | [] | [] |

| | Cls n | Cls n | [] | [] | [] | [] |

There are 'n' possible rows to this table. 该表有“ n”个可能的行。

Does anyone have examples of what I want to do that they can point me to? 有人能给我指出我想做的事的例子吗?

Thanks, 谢谢,

To start of, make sure you have some kind of model where you can bind the checkboxes to. 首先,请确保您具有可以将复选框绑定到其中的某种模型。 For example: 例如:

public class MyObject {

  private String name;
  private boolean bool1;
  private boolean bool2;
  // Add getters and setters

  public MyObject(String name, boolean bool1, boolean bool2) {
    this.name = name;
    this.bool1 = bool1;
    this.bool2 = bool2;
  }

}

Then, in your bean add a list of booleans to store the state of the checkboxes in the header: 然后,在您的bean中添加一个布尔值列表,以将复选框的状态存储在标头中:

private List<MyObject> myObjects = new ArrayList<>();
private List<Boolean> headerChecks = new ArrayList<>();
// Add getter and setter

@PostConstruct
private void init() {
  myObjects.add(new MyObject("Object 1", false, true));
  myObjects.add(new MyObject("Object 2", false, false));
  myObjects.add(new MyObject("Object 3", true, true));

  headerChecks.add(false);
  headerChecks.add(false);
}

Now, in the checkbox column header have a checkbox with a listener. 现在,在复选框列标题中有一个带有侦听器的复选框。 In the listener set the state of all checkboxes of that column. 在侦听器中,设置该列所有复选框的状态。 Then update only the checkboxes in the column. 然后仅更新该列中的复选框。 To do so I've used a selector selecting elements with a specific class: @(.bool1) . 为此,我使用了选择器来选择具有特定类的元素: @(.bool1)

<p:column>
  <f:facet name="header">
    <p:selectBooleanCheckbox id="bool1" value="#{myBean.headerChecks[0]}">
      <p:ajax listener="#{myBean.headerClickListener}"
              update="@(.bool1)"/>
    </p:selectBooleanCheckbox>
    <p:outputLabel for="bool1" value="Bool 1"/>
  </f:facet>
  <p:selectBooleanCheckbox value="#{item.bool1}" styleClass="bool1"/>
</p:column>

In listener the state of the corresponding row checkboxes is set: 在侦听器中,设置了相应行复选框的状态:

public void headerClickListener(AjaxBehaviorEvent event) {
  for (MyObject myObject : myObjects) {
    if (event.getComponent().getId().equals("bool1")) {
      myObject.setBool1(headerChecks.get(0));
    }
    if (event.getComponent().getId().equals("bool2")) {
      myObject.setBool2(headerChecks.get(1));
    }
  }
}

The full table: 完整表:

<p:dataTable id="dataTable" value="#{myBean.myObjects}" var="item">
  <p:column headerText="Name">
    <h:outputText value="#{item.name}"/>
  </p:column>
  <p:column>
    <f:facet name="header">
      <p:selectBooleanCheckbox id="bool1" value="#{myBean.headerChecks[0]}">
        <p:ajax listener="#{myBean.headerClickListener}"
                update="@(.bool1)"/>
      </p:selectBooleanCheckbox>
      <p:outputLabel for="bool1" value="Bool 1"/>
    </f:facet>
    <p:selectBooleanCheckbox value="#{item.bool1}" styleClass="bool1"/>
  </p:column>
  <p:column>
    <f:facet name="header">
      <p:selectBooleanCheckbox id="bool2" value="#{myBean.headerChecks[1]}">
        <p:ajax listener="#{myBean.headerClickListener}"
                update="@(.bool2)"/>
      </p:selectBooleanCheckbox>
      <p:outputLabel for="bool2" value="Bool 2"/>
    </f:facet>
    <p:selectBooleanCheckbox value="#{item.bool2}" styleClass="bool2"/>
  </p:column>
</p:dataTable>

Please note that this is all quite WET. 请注意,这一切都是相当湿的。 You might want to DRY stuff up a bit. 您可能要稍微塞满一些东西。

Result: 结果:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM