0

I have added a search textbox which allows users to perform search on all rows and columns now i want to get all the filtered rows in json format so i can generate a report. Below is the code for Jqgrid

var grid = $("#jqGrid");
 grid.jqGrid({
    url: 'VehicleMaster.aspx/GetLoadData',
    mtype: 'POST',
    datatype: 'json',
    ajaxGridOptions: { contentType: 'application/json; charset=utf-8' },
    serializeGridData: function(postData) {
        return JSON.stringify(postData);
    },
    ajaxSelectOptions: { type: "GET" },
    jsonReader: {
        repeatitems: false,
        root: function(obj) {
            return JSON.parse(obj.d);
        }
    },
    loadonce: true,
    sortable: true,
    shrinkToFit: false,
    autowidth: true,
    rowNum: 25,
    rowList: [15, 25, 50, 75, 100],
    viewsortcols: [true, 'vertical', true],
    pager: '#pager',
    pgtext: "Page from {0} to {1}",
     styleUI: 'Bootstrap',
    height: $(window).height() - (343), //DYNAMIC HEIGHT
    loadComplete: function() {
        $(this).find(" > tbody > tr.jqgrow:odd").addClass("altRowClassEven");
        $(this).find(" > tbody > tr.jqgrow:even").addClass("altRowClassOdd");
    },
     colNames: gridColumnNames, //FROM BACKEND
     colModel: [
        { label: 'Id', name: 'Id', width: 130, editable: false, hidden: true },
        { label: 'VehicleName', name: 'VehicleName', width: 130, editable: false },
        { label: 'PlateCode', name: 'PlateCode', editable: true, width: 100 },
        {
            label: 'PlateNo',
            name: 'PlateNo',
            editable: true,
            width: 100,
            editoptions: {
                dataInit: function(domElem) {
                    $(domElem).keyup(function() {
                        this.value = this.value.replace(/[^0-9\.]/g, '');
                    });
                }
            }
        },
        { label: 'Owner', name: 'Owner', editable: true, width: 100 },
        { label: 'Brand', name: 'Brand', editable: true, width: 100 },
        { label: 'Color', name: 'Color', editable: true, width: 100 },
        { label: 'Make', name: 'Make', editable: true, width: 100 },
        { label: 'Model', name: 'Model', editable: true, width: 100 },
        { label: 'YearModel', name: 'YearModel', editable: true, width: 100 },
        {
            label: 'Seats',
            name: 'Seats',
            editable: true,
            width: 100,
            editoptions: {
                dataInit: function(domElem) {
                    $(domElem).keyup(function() {
                        this.value = this.value.replace(/[^0-9\.]/g, '');
                    });
                }
            }
        },
        {
            label: 'Doors',
            name: 'Doors',
            editable: true,
            width: 100,
            editoptions: {
                dataInit: function(domElem) {
                    $(domElem).keyup(function() {
                        this.value = this.value.replace(/[^0-9\.]/g, '');
                    });
                }
            }
        },
        {
            label: 'RegistrationDate',
            name: 'RegistrationDate',
            editable: true,
            width: 130,
            editoptions: {
                dataInit: function(domElem) {
                    $(domElem).datepicker({
                        format: 'dd/mm/yyyy',
                        autoclose: true,
                        clearBtn: true
                    });
                    $(domElem).prop('readonly', true);
                    //$(domElem).inputmask('99/99/9999');
                }
            }
        },
        {
            label: 'RegistrationExpiry',
            name: 'RegistrationExpiry',
            editable: true,
            width: 130,
            editoptions: {
                dataInit: function(domElem) {
                    $(domElem).datepicker({
                        format: 'dd/mm/yyyy',
                        autoclose: true,
                        clearBtn: true
                    });
                    $(domElem).prop('readonly', true);
                }
            }
        },
        { label: 'ChassisNo', name: 'ChassisNo', editable: true, width: 100 },
        { label: 'EngineNo', name: 'EngineNo', editable: true, width: 130 },
        { label: 'TrafficFileNo', name: 'TrafficFileNo', editable: true, width: 130 },
        { label: 'VehicleType', name: 'VehicleType', editable: true, width: 130 },
        { label: 'InsuranceCompany', name: 'InsuranceCompany', editable: true, width: 160 },
        {
            label: 'InsuranceDate',
            name: 'InsuranceDate',
            editable: true,
            width: 130,
            editoptions: {
                dataInit: function(domElem) {
                    $(domElem).datepicker({
                        format: 'dd/mm/yyyy',
                        autoclose: true,
                        clearBtn: true
                    });
                    $(domElem).prop('readonly', true);
                }
            }
        },
        {
            label: 'InsuranceExpiry',
            name: 'InsuranceExpiry',
            editable: true,
            width: 130,
            editoptions: {
                dataInit: function(domElem) {
                    $(domElem).datepicker({
                        format: 'dd/mm/yyyy',
                        autoclose: true,
                        clearBtn: true
                    });
                    $(domElem).prop('readonly', true);
                }
            }
        },
        {
            label: 'PlateIssueDate',
            name: 'PlateIssueDate',
            editable: true,
            width: 130,
            editoptions: {
                dataInit: function(domElem) {
                    $(domElem).datepicker({
                        format: 'dd/mm/yyyy',
                        autoclose: true,
                        clearBtn: true
                    });
                    $(domElem).prop('readonly', true);
                }
            }
        },
        { label: 'Department', name: 'Department', editable: true, width: 130 },
        { label: 'Description', name: 'Description', editable: true, width: 130 },
        {
            label: 'Column1',
            name: 'Column1',
            editable: true,
            width: 130,
            editoptions: {
                dataInit: function(domElem) {
                    $(domElem).datepicker({
                        format: 'dd/mm/yyyy',
                        autoclose: true,
                        clearBtn: true
                    });
                    $(domElem).prop('readonly', true);
                }
            }
        },
        {
            label: 'Column2',
            name: 'Column2',
            editable: true,
            width: 130,
            editoptions: {
                dataInit: function(domElem) {
                    $(domElem).datepicker({
                        format: 'dd/mm/yyyy',
                        autoclose: true,
                        clearBtn: true
                    });
                    $(domElem).prop('readonly', true);
                }
            }
        },
        {
            label: 'Column3',
            name: 'Column3',
            editable: true,
            width: 130,
            editoptions: {
                dataInit: function(domElem) {
                    $(domElem).datepicker({
                        format: 'dd/mm/yyyy',
                        autoclose: true,
                        clearBtn: true
                    });
                    $(domElem).prop('readonly', true);
                }
            }
        },
        {
            label: 'Column4',
            name: 'Column4',
            editable: true,
            width: 130,
            editoptions: {
                dataInit: function(domElem) {
                    $(domElem).datepicker({
                        format: 'dd/mm/yyyy',
                        autoclose: true,
                        clearBtn: true
                    });
                    $(domElem).prop('readonly', true);
                }
            }
        },
        {
            label: 'Column5',
            name: 'Column5',
            editable: true,
            width: 130,
            editoptions: {
                dataInit: function(domElem) {
                    $(domElem).datepicker({
                        format: 'dd/mm/yyyy',
                        autoclose: true,
                        clearBtn: true
                    });
                    $(domElem).prop('readonly', true);
                }
            }
        },
        {
            label: 'Column6',
            name: 'Column6',
            editable: true,
            width: 130,
            editoptions: {
                dataInit: function(domElem) {
                    $(domElem).datepicker({
                        format: 'dd/mm/yyyy',
                        autoclose: true,
                        clearBtn: true
                    });
                    $(domElem).prop('readonly', true);
                }
            }
        },
        {
            label: 'Column7',
            name: 'Column7',
            editable: true,
            width: 130,
            editoptions: {
                dataInit: function(domElem) {
                    $(domElem).datepicker({
                        format: 'dd/mm/yyyy',
                        autoclose: true,
                        clearBtn: true
                    });
                    $(domElem).prop('readonly', true);
                }
            }
        },
        {
            label: 'Column8',
            name: 'Column8',
            editable: true,
            width: 130,
            editoptions: {
                dataInit: function(domElem) {
                    $(domElem).datepicker({
                        format: 'dd/mm/yyyy',
                        autoclose: true,
                        clearBtn: true
                    });
                    $(domElem).prop('readonly', true);
                }
            }
        },
        {
            label: 'Column9',
            name: 'Column9',
            editable: true,
            width: 130,
            editoptions: {
                dataInit: function(domElem) {
                    $(domElem).datepicker({
                        format: 'dd/mm/yyyy',
                        autoclose: true,
                        clearBtn: true
                    });
                    $(domElem).prop('readonly', true);
                }
            }
        },
        {
            label: 'Column10',
            name: 'Column10',
            editable: true,
            width: 130,
            editoptions: {
                dataInit: function(domElem) {
                    $(domElem).datepicker({
                        format: 'dd/mm/yyyy',
                        autoclose: true,
                        clearBtn: true
                    });
                    $(domElem).prop('readonly', true);
                }
            }
        }
    ],
    multiselect: false,
    cellEdit: true,
    hiddengrid: false,
    loadtext: 'Loading Data...',
    sortascimg: 'sort_asc.gif',
    sortdescimg: 'sort_desc.gif',
    cellsubmit: 'clientArray',
    viewrecords: true,
    beforeSelectRow: function(rowid) {
        console.log(rowid);
         if (typeof selectedRows[0] !== "undefined") {
            updateSelectedRow(selectedRows[selectedRows.length - 1]);
            console.log(selectedRows[selectedRows.length - 1]);
        }
         selectedRows.push(rowid);
     }
});

Below is the code for live search.

$("#globalSearchText").keyup(function() {
        var rules = [],
            i,
            cm,
            postData = grid.jqGrid("getGridParam", "postData"),
            colModel = grid.jqGrid("getGridParam", "colModel"), 
            searchText = $("#globalSearchText").val(),
            l = colModel.length;

        for (i = 0; i < l; i++) {
            cm = colModel[i];
            if (cm.search !== false && (typeof cm.stype === "undefined" || cm.stype === "text")) {
                rules.push({
                    field: cm.name,
                    op: "cn",
                    data: searchText
                });
            }
        }

        $.extend(postData, {
            filters: {
                groupOp: "OR",
                rules: rules
            }
        });

Kindly guide me to resolve this. Thanks

Mohammad Yusuf
  • 16,554
  • 10
  • 50
  • 78
Alexander
  • 91
  • 1
  • 10
  • Please include always which version of jqGrid you use (or can use) and from which fork of jqGrid ([free jqGrid](https://github.com/free-jqgrid/jqGrid), commertial [Guriddo jqGrid JS](http://guriddo.net/?page_id=103334) or an old jqGrid in version <=4.7). You included `styleUI: 'Bootstrap'` option, which exist in **commertial** product Guriddo. Free jqGrid contains mostly the same features, but one more, which could help you: `lastSelectedData` parameter (see [the demo](http://www.ok-soft-gmbh.com/jqGrid/OK/GetFilteredData.htm)), which helps you. – Oleg Dec 31 '16 at 14:54
  • See [here](https://free-jqgrid.github.io/getting-started/index.html#bootstrap) about the usage of Bootstrap in free jqGrid. I'd recommend you to use column templates additionally to reduce and simplify your JavaScript code. See [the old answer](http://stackoverflow.com/a/6047856/315935) for more details. – Oleg Dec 31 '16 at 14:55
  • i am using jqGrid 5.1.1, also i have used lastSelectedData but its not returning anything. – Alexander Dec 31 '16 at 18:24
  • I tried to explain you that there are **two alternative forks** of jqGrid after th version 4.7: free jqGrid, which I develop and provide under old MIT/GPL licences completely free of charge, and commercial Guriddo jqGrid JS (see the prices [here](http://guriddo.net/?page_id=103334). Both forks will be developed independently and have different features. I implemented `lastSelectedData` feature in the fork, which I develop. – Oleg Dec 31 '16 at 18:37
  • I don't support Guriddo. The only way, which I can suggest you, if you can't upgrade to free jqGrid, is following of [my old answer](http://stackoverflow.com/a/9831125/315935) – Oleg Dec 31 '16 at 18:40
  • Hi, i am getting the filter data now with lastSelectedData but the row order is different.Can i also get the row order as well. – Alexander Jan 11 '17 at 12:39
  • Please append your question with *the current code*, which you use. It's unclear for me which oder of data you mean because the items of `lastSelectedData` and `data` are *objects* which properties corresponds to the `name` of `colModel`. Do you need to concert the items to array of string? Free jqGrid provide `iColByName`, which can be used to map the names to the index (iCol) in `colModel` – Oleg Jan 11 '17 at 13:18
  • Hi, i have created a JavaScript function which takes in objects generated from the lastSelectedData and generates a CSV file for download. The order of the objects generated from the lastSelectedData dosen't match with the grid. So can i get these objects in the same order as the grid which i see on the screen.I am using your code to get the lastSelectedData for the Guriddo jqgrid. Thanks – Alexander Jan 18 '17 at 08:19
  • Sorry I don't understand what you mean with the statement "I am using your code to get the lastSelectedData for the Guriddo jqgrid". Do you use now [free jqGrid](https://github.com/free-jqgrid/jqGrid) fork, which I develop, or commercial [Guriddo jqGrid JS](http://guriddo.net/?page_id=103334)? Which version of free jqGrid (or Guriddo jqGrid JS) you use now? It's new for me that Guriddo implemented `lastSelectedData`. By the way, the order of elements in `lastSelectedData` is exactly line displayed in the grid, but it has all elements (not one page only). – Oleg Jan 18 '17 at 08:30
  • Try to change the order of elements in the grid by sorting by some column in [the demo](http://www.ok-soft-gmbh.com/jqGrid/OK/GetFilteredData.htm) or in [this one](http://www.ok-soft-gmbh.com/jqGrid/OK/GetFilteredData1.htm) and click on the button above the grid. You will see that the order or elements is the same as displayed in the grid. – Oleg Jan 18 '17 at 08:32
  • I am using free version of Guriddo jqGrid – Alexander Jan 18 '17 at 13:33
  • Sorry, but "Guriddo jqGrid" is the name of the **commercial** product, which source code is opened, but which can't be used for free (see the prices [here](http://guriddo.net/?page_id=103334)). Even if you can use it without paying the money it's still **another product** as "free jqGrid", which I develop. Both "free jqGrid" and "Guriddo jqGrid" are later development of jqGrid 4.7. In any way, `lastSelectedData` exists **only in the product "free jqGrid"**. It's not exist in "Guriddo jqGrid". Please reread carefully my previous comments. – Oleg Jan 18 '17 at 14:07
  • Yeah, i know but its not easy for me to switch the free jqgrid. – Alexander Jan 18 '17 at 15:07
  • You should post to [Guriddo forum](http://guriddo.net/?forum=guriddo-js) if you want to use Guriddo. In any way, I can't help you with Guriddo. – Oleg Jan 18 '17 at 15:15
  • No worries, thanks for helping. – Alexander Jan 18 '17 at 15:21

0 Answers0