1

I have been combing through the wiki for the jqgrid and i can't seem to figure out how to change the logic in my controller for the jqgrid to search.

I assume the search will use the same URL specified in the jqgrid. Here is the action logic called by my jqgrid. I am using spring 3.0 and its a java controller.

@RequestMapping(value = "studentjsondata", method = RequestMethod.GET)
public @ResponseBody String studentjsondata(HttpServletRequest httpServletRequest) {
    Format formatter = new SimpleDateFormat("MMMM dd, yyyy");
    String column = "id";
    if(httpServletRequest.getParameter("sidx") != null){
        column = httpServletRequest.getParameter("sidx");
    }
    String orderType = "DESC";
    if(httpServletRequest.getParameter("sord") != null){
        orderType = httpServletRequest.getParameter("sord").toUpperCase();
    }
    int page = 1;
    if(Integer.parseInt(httpServletRequest.getParameter("page")) >= 1){
        page = Integer.parseInt(httpServletRequest.getParameter("page"));
    }
    int limitAmount = 10;
    int limitStart = limitAmount*page - limitAmount;
    List<Person> students = Person.findStudentPeopleOrderByColumn(true, column, orderType, limitStart, limitAmount).getResultList();

    long countStudents = Student.countStudents();

    double tally = Math.ceil(countStudents/10.0d);
    int totalPages = (int)tally;
    long records = countStudents;


    StringBuilder sb = new StringBuilder();
    sb.append("{\"page\":\"").append(page).append("\", \"records\":\"").append(records).append("\", \"total\":\"").append(totalPages).append("\", \"rows\":[");
    boolean first = true;
    for (Person s: students) {
        sb.append(first ? "" : ",");
        if (first) {
            first = false;
        }
        sb.append(String.format("{\"id\":\"%s\", \"cell\":[\"%s\", \"%s\", \"%s\", \"%s\", \"%s\", \"%s\"]}",s.getId(), s.getId(), s.getFirstName(), s.getLastName(),  formatter.format(s.getDateOfBirth().getTime()), s.getGender(), s.getMaritalStatus()));
    }
    sb.append("]}");
    return sb.toString();
}

and here is my navGrid decleration

    $("#studentGrid").jqGrid('navGrid', "#pager", {edit:false,add:false,del:false,search:true},{ },{ },{ },
            { 
                sopt:['eq', 'ne', 'lt', 'gt', 'cn', 'bw', 'ew'],
                closeOnEscape: true, 
                multipleSearch: true, 
                closeAfterSearch: true 
            }
     );

Here is my colModel and colNames

            colNames:['id','First Name', 'Last Name', 'Date Of Birth', 'Gender', 'Marital Status'], 
            colModel:[ 
                {name:'id',index:'id', width:15}, 
                {name:'firstName',index:'firstName', width:30, formoptions:{elmprefix:'(*) '}, editable:true, edittype: 'text', editrules:{required:true}},
                {name:'lastName',index:'lastName', width:30, formoptions:{elmprefix:'(*) '}, editable:true, edittype: 'text',editrules:{required:true}},
                {name:'dateOfBirth',index:'dateOfBirth', width:30, formoptions:{elmprefix:'(*) '},editrules:{required:true}, editable:true, edittype: 'text',               
                    editoptions: {
                        dataInit: function(element) {
                            $(element).datepicker({dateFormat: 'MM dd, yy'})
                        }
                    } 
                },                    
                {name:'gender',index:'gender', width:30, formoptions:{elmprefix:'(*) '}, editable:true, editrules:{required:true}, edittype: 'select',
                    editoptions:{value:{}}
                },
                {name:'maritalStatus',index:'maritalStatus', width:30, formoptions:{elmprefix:'(*) '}, editable:true, editrules:{required:true}, edittype: 'select',
                    editoptions:{value:{}}
                }                    
            ]

As it is, by default the search uses trhe searchGrid method. In the post array the _search: true and filters: {"groupOp":"AND","rules":[{"field":"firstName","op":"eq","data":"Anil"}]} are present. The searchField, searchOper and searchString are all empty but present in th epost array.

What do I have to do to get the search working?

Do I have to parse the json into Java using the json parser and the filters array, then change my query by adding a where clause and use the values form the Json object?

Does the jqgrid query its own data object insted of going back to the server and launch a new query ?

I am not too sure what I have to do, please offer some form of guidance.

SliverNinja - MSFT
  • 31,051
  • 11
  • 110
  • 173
Faiyet
  • 5,341
  • 14
  • 51
  • 66
  • I don't see in your code something like `httpServletRequest.getParameter("filters")` and `httpServletRequest.getParameter("_search")`. – Oleg Mar 26 '12 at 14:41
  • I didn't make the changes yet, because I am not sure what to do with the values. Do I pass it as an arg to my finder and parse it form json to java there ? I am not sure what pattern is usually used for the action to enable the search feature. The values can be accessed just as you outlined above. – Faiyet Mar 26 '12 at 14:46
  • I have seen this page http://blog.brzezinka.eu/webmaster-tips/jquery/how-to-enable-the-search-functionality-in-jqgrid where it was done in php, do I do this in java ? Is this the way it is usually done ? – Faiyet Mar 26 '12 at 14:55
  • In [the post](http://stackoverflow.com/a/5501644/315935) I described how you can use `filters` in ASP.NET together with Entity Framework, but I am not sure that it's help you really. I think that [he post](http://krams915.blogspot.de/2012/01/spring-mvc-31-jqgrid-and-spring-data_8761.html) (see ma answer) could help you more. – Oleg Mar 26 '12 at 15:03

1 Answers1

1

I am not use Spring myself, but the post seems to me contain the information which you need.

In general if you use Advance Searching dialog (multipleSearch: true) or Toolbar Searching with stringResult: true the jqGrid send to the server additional parameter filters which format described here. The one parameter filters can contain information about multiple filters. So you have to covert JSON string to an object and analyse the object to construct some kine of WHERE part of the SELECT statement. The exact implementation is depend from the technology which you use to assess to the database.

Oleg
  • 220,925
  • 34
  • 403
  • 798