2

My data is being correctly loaded on my page. However I have 2 textboxes and one submit button based on which I want to filter the records from the server.

Note: I am not using existing filters functionality that is available with jqgrid by default.

I am little confused how can I achieve this. Is there any built in capability of jqgrid to achieve this? The way I currently handle this is I handle the click event in my javascript and supply post data to the action method:

  $('#submit').click(function () {

            $("#customers").jqGrid('setGridParam', { postData: { 'ContactName': $('#contactName').val(),
                CompanyName: $('#companyName').val()
            }
            });

            $("#customers").trigger("reloadGrid");

        });

This post data is then being captured on action method and it works fine. Is there any better way of doing this? or am I on right track? Sometimes I feel I write less code on the server and have become more of a client side programmer since I started using Asp.Net MVC 3.0 ;)

TCM
  • 16,780
  • 43
  • 156
  • 254
  • 1
    I'd love to see an answer to this. I ended up doing a fairly similar action in my implementation. The only thing I did that might be useful is to wrap the grid build and filter update in a custom jquery plugin. Which allows me to just call jqGrid_custom("updateFilter","newfilter"); A little extra work but it allows me to use the same solution on all my grids. – Jonathan Park Sep 12 '11 at 17:31

1 Answers1

3

You don't have to use setGridParam to change postData as you can declare a function :

jqGrid({
    url : ...,
    datatype : ...,
    mtype : "POST",
    postData : { 
            "ContactName" : (function() {
                            return $("#contactName").val();
                        })
    }
}

so your submit function will only call to reloadGrid

 $('#submit').click(function () {
    $("#customers").trigger("reloadGrid");
 }

If you want to reduce the amount of code, and you'd better create a simple API in JS to select entities like Customer, Person, Contact etc. Currently I have an app which forms consist of dozens such entities - so I had to create an JS API for selecting(it also gives universal look and feel). From the client side the customer specifies the name of list to get, while the list of possible names is defined in the server's configuration file, which also defines the query to the ORM and how to display fields(I am using an expression language to map from entity fields to strings).

Boris Treukhov
  • 17,493
  • 9
  • 70
  • 91
  • 2
    Thanks, you save my time in writing the answer with close information. I recommend @Anthony to read [the answer](http://stackoverflow.com/questions/2928371/how-to-filter-the-jqgrid-data-not-using-the-built-in-search-filter-box/2928819#2928819), which describes exactly situation which he has. – Oleg Sep 12 '11 at 19:08
  • @Oleg: Are you the developer of jqgrid? How do you know so much about jqgrid? You go much more beyond what OP normally asks for and cover every different possible scenario. – TCM Sep 13 '11 at 02:11
  • @Oleg: I am just curious (not quiet related to the question) but, why am I seeing that Boris Treukhov posted answer 6 hours ago and you posted comment on that answer 7 hours ago i.e you posted comment to the Boris which he was about to write after 1 hour? – TCM Sep 13 '11 at 02:24
  • @Anthony: I am not developer of jqGrid, but since last 2 years as I use it I posted many bug fixes and suggested some new features which are now in the code. JavaScript, jQuery, jqGrid was new for me 2 years ago. I like to study new things and to help other. I change from time to time my subject of interest to study new things. Writing answers in system development for windows, cryptography and simple C/C++/C# bring more reputation, but many other people know the same things already. I think that my answers about jqGrid on the other side more help to real people and I improve my knowledge. – Oleg Sep 13 '11 at 08:39
  • @Oleg: Ok! great! how can I contact you? or is it that you only answer on SO? – TCM Sep 13 '11 at 14:59
  • @Anthony: You can write email to oleg.kiriljuk@ok-soft-gmbh.com – Oleg Sep 13 '11 at 15:06