How can I filter a Primefaces datatable clicking on a commandLink button?
I'm trying to reach that using JQuery, but it is not working:
<p:commandLink id="filterLink" value="Click to Filter" onclick=" $('#MyFormID\\:MyDatatableID\\:MyColumnID').val('123').filter().trigger('keyup')" />
Relevant code:
<h:form id="MyFormID">
<p:panel id="MyPanelID" header="Some title">
<p:dataTable id="MyDatatableID"
value="#{myController.items}"
rowKey="#{item.id}"
var="item"
selection="#{myController.selected}"
filteredValue="#{myController.filteredDemandas}"
widgetVar="wv">
<p:column id="MyColumnID" sortBy="#{item.status.status}" filterBy="#{item.status.status}" filterMatchMode="in">
<f:facet name="header">
<h:outputText value="MyColumnName" />
</f:facet>
<f:facet name="filter">
<p:selectCheckboxMenu label="MyColumnTitleName" onchange="PF('wv').filter()" >
<f:selectItems value="#{myController.listValues}" />
</p:selectCheckboxMenu>
</f:facet>
<h:outputText value="#{item.status.status}"/>
</p:column>
<!-- some code ommited -->
</p:dataTable>
</p:panel>
</h:form>
Can someone help me, please ?
Thanks in advance.
EDITED:
In react to comments, I've received some help and found a solution to use in a column that has an input text field where we type some value to be filtered:
<p:commandLink id="filterLink" value="Click to Filter" onclick=" $('#MyFormID\\:MyDatatableID\\:myColumnID\\:filter').val('123'); PF('wv').filter();" />
But in my question, I'm using a SelectCheckBoxMenu, where I have a list of values and I'm confused about what element(s) do I need to call to set the value and how to code that.
SelectCheckboxMenu elements - Primefaces Guide 6.2, pg. 452:
.ui-selectcheckboxmenu-items -> Option list container
.ui-selectcheckboxmenu-item -> Each options in the collection
.ui-chkbox -> Container of a checkbox.
I've found solutions to SelectOneMenu, but I don't know how to adapt/achieve this to SelectCheckboxMenu.
- Change item of p:selectOneMenu using jQuery
- How can I set the value of a DropDownList using jQuery?
- How to get value using JQuery
- How to set a dropdown box value in jQuery
I've already tried the following, but no success:
<p:commandLink value="test1" onclick=" $('#MyFormID\\:MyDatatableID\\:MyColumnID_panel .ui-selectcheckboxmenu-list-item div div.ui-chkbox-box')[3].click()" />
<p:commandLink value="test2" onclick=" $('#MyFormID\\:MyDatatableID\\:selectcheckboxmenuItemsID').val('Vence hoje'); PF('wv').filter();" />
<p:commandLink value="test3" onclick=" PF('wvSelectcheckboxmenu').jq.find('.ui-selectcheckboxmenu-item:eq(3)').click(); PF('wv').filter();" />
<p:commandLink value="test4" onclick=" PF('wvSelectcheckboxmenu').jq.find('.ui-selectcheckboxmenu-items:eq(3)').click(); PF('wv').filter();" />
Below, the (relevant) html code that I got with Chrome browser developer tool:
<div class="ui-column-customfilter">
<div id="MyFormID:MyDatatableID:idBoxSituacao" class="ui-selectcheckboxmenu ui-widget ui-state-default ui-corner-all">
<div class="ui-helper-hidden-accessible">
<input id="MyFormID:MyDatatableID:idBoxSituacao_focus" name="MyFormID:MyDatatableID:idBoxSituacao_focus" type="text" readonly="readonly" aria-expanded="true" aria-labelledby="MyFormID:MyDatatableID:idBoxSituacao_label">
</div>
<div class="ui-helper-hidden">
<input id="MyFormID:MyDatatableID:idBoxSituacao:0" name="MyFormID:MyDatatableID:idBoxSituacao" type="checkbox" value="A Vencer" data-escaped="true" onchange="PF('demandasTable').filter()" aria-checked="true">
<label for="MyFormID:MyDatatableID:idBoxSituacao:0">
A Vencer
</label>
<input id="MyFormID:MyDatatableID:idBoxSituacao:1" name="MyFormID:MyDatatableID:idBoxSituacao" type="checkbox" value="A Vencer até 10 dias" data-escaped="true" onchange="PF('demandasTable').filter()" aria-checked="false">
<label for="MyFormID:MyDatatableID:idBoxSituacao:1">
A Vencer até 10 dias
</label>
<input id="MyFormID:MyDatatableID:idBoxSituacao:2" name="MyFormID:MyDatatableID:idBoxSituacao" type="checkbox" value="Indefinida" data-escaped="true" onchange="PF('demandasTable').filter()" aria-checked="true">
<label for="MyFormID:MyDatatableID:idBoxSituacao:2">
Indefinida
</label>
<input id="MyFormID:MyDatatableID:idBoxSituacao:3" name="MyFormID:MyDatatableID:idBoxSituacao" type="checkbox" value="Vence hoje" data-escaped="true" onchange="PF('demandasTable').filter()" aria-checked="false">
<label for="MyFormID:MyDatatableID:idBoxSituacao:3">
Vence hoje
</label>
<input id="MyFormID:MyDatatableID:idBoxSituacao:4" name="MyFormID:MyDatatableID:idBoxSituacao" type="checkbox" value="Vencida" data-escaped="true" onchange="PF('demandasTable').filter()">
<label for="MyFormID:MyDatatableID:idBoxSituacao:4">
Vencida
</label>
</div>
<span class="ui-selectcheckboxmenu-label-container">
<label class="ui-selectcheckboxmenu-label ui-corner-all" id="MyFormID:MyDatatableID:idBoxSituacao_label">
Situação
</label>
</span>
<div class="ui-selectcheckboxmenu-trigger ui-state-default ui-corner-right">
<span class="ui-icon ui-icon-triangle-1-s">
</span>
</div>
</div>
</div>
Can someone help me, please ?