Click here to Skip to main content
15,891,905 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,

I need help to implement the search operation in C# Jqgrid. I have searched in google I have written a Script for jQGrid . I have bind a data from sql server database it is working fine and I have implemented a edit,insert and delete operations Using jQGrid.
For calling of methods I have written in Web service. Now my problem is how can I Implement the search operation in "filter Toolbar" and "Direct search" in the jQGrid.

Hear is my script

JavaScript
<script type="text/javascript">
        jQuery(document).ready(function () {
            jQuery.extend(jQuery.jgrid.edit, {
                closeAfterEdit: true,
                closeAfterAdd: true,

                ajaxEditOptions: { contentType: "application/json" },
                serializeEditData: function (postData) {
                    var postdata = { 'data': postData };
                    return JSON.stringify(postdata); ;
                }
            });
            jQuery.extend(jQuery.jgrid.del, {
                ajaxDelOptions: { contentType: "application/json" },

                onclickSubmit: function (eparams) {
                    var retarr = {};
                    var sr = jQuery("#contactsList").getGridParam('selrow');
                    rowdata = jQuery("#contactsList").getRowData(sr);
                    retarr = { PID: rowdata.PID };

                    return retarr;
                },
                serializeDelData: function (data) {
                    var postData = { 'data': data };
                    return JSON.stringify(postData);
                }
            });
            $.extend($.jgrid.defaults,
                  { datatype: 'json' }
                  );

            jQuery.extend(jQuery.jgrid.search, {
            
            });


            $("#contactsList").jqGrid({

                url: '/WebService.asmx/GetListOfPersons1',

                datatype: 'json',

                mtype: 'POST',

                ajaxGridOptions: { contentType: 'application/json; charset=utf-8' },
                serializeGridData: function (postData) {

                    return JSON.stringify(postData);
                },

                jsonReader: { repeatitems: false, root: "d.rows", page: "d.page", total: "d.total", records: "d.records" },
                colNames: ['PID', 'First Name', 'Last Name', 'Gender'],
                colModel: [
                    { name: 'PID', width: 60, align: "center", hidden: true, searchtype: "integer", editable: true },
                    { name: 'FirstName', width: 180, sortable: true, hidden: false, editable: true },
                    { name: 'LastName', width: 180, sortable: false, hidden: false, editable: true },
                    { name: 'Gender', width: 180, sortable: false, hidden: false, editable: true, cellEdit: true, edittype: "select", formater: 'select', editrules: { required: true },
                        editoptions: { value: getAllSelectOptions() }
                    }],
                rowNum: 10,
                rowList: [10, 20, 30],
                sortname: 'PID',
                sortorder: "asc",
                pager: jQuery('#gridpager'),
                viewrecords: true,
                gridview: true,
                height: '100%',
                editurl: '/WebService.asmx/EditRow',
                searchurl: '/WebService.asmx/Searchrow',
                caption: 'Person details'
            }).jqGrid('navGrid', '#gridpager', { edit: true, add: true, del: true, search: true });
            jQuery("#contactsList").jqGrid('filterToolbar', { stringResult: true, searchOnEnter: false, searchOperators: true });
           // jQuery("#contactsList").setGridParam({ data: results.rows, localReader: reader }).trigger('reloadGrid');
        });
    function getAllSelectOptions() {
        var states = { 'male': 'M', 'female': 'F' };

        return states;

    }

    </script>



please help me to perform the search operation in client side or server side.



thanks
purna
Posted

1 solution

Hello Puran,

Did you get a chance to look at JQGrid demo site[^]. They have a example for doing the search.
Basically you will have to create a service which will take the filter parameters and return the JSON. The client side script will invoke this service on click of the search button and will then trigger the reloadGrid.
JavaScript
function doSearch(){
    var strFName = document.getElementById('txtFName');
    var strLName = document.getElementById('txtLName');
    jQuery("#mygrid").jqGrid('setGridParam',{url:"YOUR_SERVICE.asmx?firstName=" + strFName + "&lastName=" + strLName, page:1}).trigger("reloadGrid");
}


Regards,
 
Share this answer
 
Comments
Purna.N 23-May-14 8:18am    
Here I am not declaring any text boxs then How can I get the particular text box vale i.e search popup text box value.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900