使用动态ID选择Multiple SelectManyCheckBox中的所有项目

我想在JSF顶部使用PrimeFaces组件选中一些复选框中的所有复选框。

我的代码是这样的:

 

我尝试使用此处发布的代码,但只有在页面上只有一组复选框时它才有效。

将其包裹在可重复使用的复合组件中,如下所示:

/resources/composites/selectManyCheckboxAll.xhtml

         

/resources/composites/selectManyCheckboxAll.css

 .checkboxes-all { white-space: nowrap; } .checkboxes-all .ui-selectmanycheckbox, .checkboxes-all .all { display: inline-block; vertical-align: middle; } .checkboxes-all .all .ui-chkbox { margin: 1px 4px 0 0; vertical-align: top; } .checkboxes-all .all label { display: inline-block; margin-top: 4px; } 

/resources/composites/selectManyCheckboxAll.js

 $(document).on("click", ".checkboxes-all .all .ui-chkbox-box, .checkboxes-all .all input", function() { var $composite = $(this).closest(".checkboxes-all"); var widgetAll = PrimeFaces.widgets[$composite.data("widget-all")]; var widgetCheckboxes = PrimeFaces.widgets[$composite.data("widget-checkboxes")]; widgetCheckboxes.inputs.prop("checked", !widgetAll.isChecked()).click(); }); $(document).on("click", ".checkboxes-all .ui-selectmanycheckbox input", function() { var $composite = $(this).closest(".checkboxes-all"); var widgetAll = PrimeFaces.widgets[$composite.data("widget-all")]; if (!$(this).is(":checked") && widgetAll.isChecked()) { widgetAll.uncheck(); } }); 

用法:

  ...