Displaytag table search using datatables jquery plugin
Add client
side search (filter) functionality to displaytag generated table.
Displaytag doesn't allow
us to have a table header. So, we used Rhett Sutphin’s patch which adds the
support for <display:header>.
Used datatable
jquery plugin to filter the data on the client side
The display
tag jar that contains the latest patch can be downloaded from:
The jquery can be downloaded from:
Include the above downloaded scripts in jsp page:
<script type="text/javascript" src="js/jquery.dataTables.min.js"></script>
<script type="text/javascript" src=" js/jquery-1.7.1.min.js"></script>
The Display Table will be drawn like this:
<display:table name="${modelsList}"
id="model" uid="models"
pagesize="" style="background-color:white"
decorator="subtotaler" requestURI="" cellspacing="1"
cellpadding="5">
<display:setProperty name="basic.empty.showtable"
value="true" />
<display:header>
<thead id="sModel">
<tr>
<th><input type="text"
name="modelName" class="search_init"/></th>
<th><input type="text"
name="modelDescription" class="search_init"/></th>
</tr>
</thead>
</display:header>
<display:column property="modelName"
title="Quote"/>
<display:column property="modelDescription"
title="Service Model"/>
</display:table>
For the Search on <display:header> add the following script:
<script type="text/javascript">
var confirmed = false;
$(function () {
/* Search functionality */
var oTable = $('#models').dataTable({
"bPaginate":false,
"bLengthChange":false,
"bFilter":true,
"bSort":false,
"bInfo":false,
"bAutoWidth":false,
"bStateSave":false
});
$("thead input").keyup(function () {
/* Filter on the column (the index) of
this element */
oTable.fnFilter(this.value, $("thead
input").index(this));
});
$("thead input").focus(function () {
if (this.className == "search_init") {
this.className = "";
this.value = "";
}
});
$("thead input").blur(function (i) {
if (this.value == "") {
this.className = "search_init";
this.value =
asInitVals[$("thead input").index(this)];
}
});
});
</script>

hard to implement it
ReplyDeleteYes quite a bit, but once implemented it's useful.
ReplyDeleteI tried as per the steps you have given. BUt I found the following error:
ReplyDeletetag "header" defined in tag library imported with prefix "display"
org.apache.jasper.JasperException: /jsp/users/displayResult.jsp(143,4) No tag "header" defined in tag library imported with prefix "display"
That's may be because you haven't included the display tag jar file which is given above, it's the modified jar file not the usual display tag jar. So try that, Also check that there should not be any conflict in jar's. Even then if you face the error, please provide the code i will try to fix it.
DeleteThanks a lot Mr. Satyam,
DeleteI found that I have 2 diplaytag jar 1.1 & 1.2. After removing the 1.1, I got the search text box. However, I further got the following error. I request you for your help.
ReferenceError: jQuery is not defined
...h.indexOf(">")!=-1)return"html";return null}]);i.fn.DataTable=m;i.fn.dataTable=m... jquery...ytag.js (line 154)
TypeError: $("#models").dataTable is not a function
"bStateSave":false displa...308-s=1 (line 192
My Code is:
display:table name="<%=resultBean%>" export="true" id="model" uid="models" class="dataTable" pagesize="30" cellpadding="0" defaultsort="1" sort="list">
Deletedisplay:setProperty name="basic.empty.showtable" value="true" />
display:header>
thead id="sModel">
tr>
th>
input type="text" name="name" class="search_init"/>
/th>
/tr>
/thead>
/display:header>
display:column property="name" title="Name" sortable="true" headerClass="name" class="name" />
** due to restriction of html tag, I have renamed them
Moreover, I rename jquery.dataTables.min.js as jquery.dataTables.min.displaytag.js
Deletehi satyam,
ReplyDeletei am not able to create this and hv tried it alot.., please help here asap.
Have you downloaded the jar from the link given? Keep only one jar in your classpath.
DeleteWhat errors are you getting ?
Please share your code.
Hi, Is it possible to get limited number of records from DB not all at once using display tag or data tables jquery. Since using display tag or data tables jquery we can get all records from DB and we to limit in JSP. But I want only limited nu of records each time not all at once.....
ReplyDeleteyou have to make a back end call every time to get the data from DB.
DeleteHi, thank you so much.. its very useful link. i followed the same steps. i am getting one extra search box above header also.? How to avoid that? can i have one searchbox for all the fields ?
ReplyDeleteopen the jquery.dataTables.min.js file
Deletesearch for - sSearch:"Search:"
replace it with - sSearch:""
..................................
Now Search for - :c===""?'
after this string there is an input tag,
Remove it.
and leave single quote blank.. as - c===""?''
--------------------------------
By this you will be able to remove top search box.
this code searches for data in current page only. how to configure the code so that it searches in all the pages?
ReplyDeleteI have similar concern. Any solution?
DeleteDid you find any solution? I have same problem
DeleteHi Arun..what is the importance of decorator="subtotaler". Is it mandatory to use that..When I add in my code, I am getting ClassNotFoundException. Please help.
ReplyDeleteAlso, in my jsp, the Search box above the table is working. But the one in table header is not working. What am I missing. I have followed the code given exactly.
ReplyDeleteIn my jsp, the Search box in table header is not working. What am I missing.Also i removed decorator="subtotaler" because
ReplyDeleteI am getting ClassNotFoundException. Any ideas?
Ihave tried same steps as you said to follow not getting any errors but its not searching also.Please help me.
Delete