I have a prime faces datatable with few columns and have filteryBy attribute for all the columns. How do I add placeholder or watermark to provide hint for users. Any suggestion
if someone needs to use a placeholder in a datatable with dynamic columns, maybe this could help you...
<p:dataTable id="generalReportTable" value="#{generalReportController.reports}" var="report"
filteredValue="#{generalReportController.filteredReports}"
rowKey="#{report.rowKey}" >
<p:columns id="dynamicColumns" value="#{generalReportController.columns}" var="column" columnIndexVar="colIndex"
sortBy="#{report[column.property]}" filterBy="#{report[column.property]}" filterMatchMode="contains"
styleClass="dynamic-cols-width-#{colIndex}">
<f:facet name="header">
<p:watermark for="@(.dynamic-cols-width-#{colIndex})" value="#{column.header}" />
<h:outputText value="#{column.header}" />
</f:facet>
<h:outputText value="#{report[column.property]}" />
</p:columns>
You can change this without overriding the placeholder attribute by going to jquery.dataTables.js line 2701 and replacing
.attr( 'placeholder', language.sSearchPlaceholder )
with
.attr( 'placeholder', 'Search table...' )
You could also find the sSearchPlaceholder variable which is probably located in a language file and replace that.
First give and ID for your column and add a p:watermark
component:
<h:form id="tableForm">
...
<p:dataTable var="dt" widgetVar="widgetUserRecords"
value="#{userBean.result}"
id="userRecordTable" paginator="true"
paginatorAlwaysVisible="false" rows="10"
height="300" >
<p:column id="column1" sortBy="#{dt.course.name}" filterStyle="width:50px;"
filterBy="#{dt.course.name}" headerText="Course Name" style="text-align:bottom">
<h:outputText value="#{dt.course.name}"/>
<p:watermark forElement="tableForm:userRecordTable:column1" value="hint..."/>
</p:column>
</p:dataTable>
...
</h:form>
Don't forget to replace the tableForm
id with your actual form around your p:dataTable
.
I realise this question was asked back in 2012, but I'm hoping this answer may help people wanting to add a watermark to the filter by field in a datatable. I tried using the solutions suggested in the other answers with forElement
but could not get the watermark to be displayed. Instead I found two solutions that use the for
attribute, the first making use of the styleClass
attribute on the p:column
element, and the second making use of the jQuery selector within the for
attribute. I also found that the p:watermark
element needed to be located within the f:facet
element used for the header.
The code I used in the for these two solutions is as follows:
<h:form id="myForm">
<p:dataTable id="myTable">
<p:column id="column1" filterBy="column1" styleClass="watermark1">
<f:facet name="header">
<p:watermark for="@(.watermark1)" value="Watermark 1" />
<h:outputText value="Column1" />
</f:facet>
</p:column>
<p:column id="column2" filterBy="column2">
<f:facet name="header">
<p:watermark for="@(#myForm\\:myTable\\:column2\\:filter)"
value="Watermark 2" />
<h:outputText value="Column2" />
</f:facet>
</p:column>
</p:dataTable>
</h:form>
According to this thread on the PrimeFaces forum it should be possible using something like this:
<h:form id="form">
<p:dataTable id="dataTable">
<p:row>
<p:column id="column" filterBy="....">
<p:watermark forElement=":form:dataTable:column" value="Filter..."/>
...
</p:column>
</p:row>
</p:dataTable>
</h:form>