Wednesday, June 20, 2012

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>





20 comments:

  1. hard to implement it

    ReplyDelete
  2. Yes quite a bit, but once implemented it's useful.

    ReplyDelete
  3. I tried as per the steps you have given. BUt I found the following error:


    tag "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"

    ReplyDelete
    Replies
    1. 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.

      Delete
    2. Thanks a lot Mr. Satyam,

      I 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:




















      Delete
    3. display:table name="<%=resultBean%>" export="true" id="model" uid="models" class="dataTable" pagesize="30" cellpadding="0" defaultsort="1" sort="list">
      display: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

      Delete
    4. Moreover, I rename jquery.dataTables.min.js as jquery.dataTables.min.displaytag.js

      Delete
  4. hi satyam,
    i am not able to create this and hv tried it alot.., please help here asap.

    ReplyDelete
    Replies
    1. Have you downloaded the jar from the link given? Keep only one jar in your classpath.
      What errors are you getting ?
      Please share your code.

      Delete
  5. 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.....

    ReplyDelete
    Replies
    1. you have to make a back end call every time to get the data from DB.

      Delete
  6. Hi, 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 ?

    ReplyDelete
    Replies
    1. open the jquery.dataTables.min.js file
      search 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.

      Delete
  7. this code searches for data in current page only. how to configure the code so that it searches in all the pages?

    ReplyDelete
    Replies
    1. I have similar concern. Any solution?

      Delete
    2. Did you find any solution? I have same problem

      Delete
  8. Hi 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.

    ReplyDelete
  9. Also, 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.

    ReplyDelete
  10. In my jsp, the Search box in table header is not working. What am I missing.Also i removed decorator="subtotaler" because
    I am getting ClassNotFoundException. Any ideas?

    ReplyDelete
    Replies
    1. Ihave tried same steps as you said to follow not getting any errors but its not searching also.Please help me.

      Delete