0

I use two jqgrid. I want when click on row in grid1 then grid2 show data by Ajax on base parameters of grid1. How to change data of grid2 by Ajax? this code for grid2:

$("#gridrelatedLette").jqGrid(
{
    url: "GetLetterInformationHandler.ashx?CurrentUser=" + 1157 + "&IndCode=" + rowData["PctIndCode"] + "&IndSerial=" + rowData["PctIndNum"] + "&TabName=relatedLetters",
    datatype: 'json',
    width: $("#relatedLetterTab").width()-20,
    height: a1,
    direction: "rtl",
    colNames: ['1', '2', '2', '3', '4', '5','6',],
    colModel: [
        { name: 'IRltType', width: 40, sortable: false, hidden: false, template: CenterTemplate },
        { name: 'IRltLettDate', width: 60, sortable: false, hidden: false, template: CenterTemplate },
        { name: 'IRltLettNum', width: 60, sortable: false, hidden: false, template: CenterTemplate },
        { name: 'IRltLettType', width:30, sortable: false, hidden: false, template: CenterTemplate },
        { name: 'IndTopic', width: 100, sortable: false, hidden: false, template: CenterTemplate },

    ],
    rowNum: 20,
    loadonce: true,
    rowList: [5, 10, 20],
    recordpos: "left",
    ignoreCase: true,
    toppager: true,
    viewrecords: true,
    sortorder: "desc",
    scrollOffset: 1,
    editurl: 'clientArray',
    shrinkToFit: true ,
    jsonReader:
    {
        repeatitems: false,
    },
    rowattr : function(rd) {
        return { "class": "myRowClass" };
    },
    gridview: true,
});
Oleg
  • 220,925
  • 34
  • 403
  • 798
ZSH
  • 631
  • 5
  • 16
  • 36
  • You posted the code of *one grid* only. The `url` contains `rowData`, and `relatedLetters`. What is the variables where the variables are defined and when the variables could be changed? – Oleg May 25 '13 at 13:19
  • Fistly, you should set 'loadonce' to false i think. And you seem lucky, Oleg is there :) – A. Wolff May 25 '13 at 13:19
  • This code is a function that when click on one of the row of grid1 is called and rowData means the information of row(this function call in selectedrow of grid1 and parameter change by click on row."relatedLetters" is name of tab that jqgri2 inside this tab. – ZSH May 25 '13 at 15:52

1 Answers1

2

The code of master/details scenario looks like following:

  1. First of all one use datatype: "local" initially for the second (details) grid. As the result the details grid stay empty till no rows in the first (master) grid is selected. No request to the url of the second (details) grid will be made initially.
  2. If the first (master) grid will be reloaded (after the click on "Reload Grid" button of navGrid) the data of the second grid must be removed. One can reset optionally datatype to "local" value too.
  3. One can use postData with properties defined as function to append parameters to the URL if one use HTTP GET operation. See the answer for details. It allows to implement dynamical values of the parameters in mostly natural way. Additionally the values of parameters will be automatically encoded using encodeURIComponent and you will have correct URL.
  4. It's frequently a problem, that details grid (the second grid) can use the same rowids as the master grid (the first grid). To prevent the problem with id duplicates I recommend to use idPrefix at least in one from the grids. For example you can use idPrefix: "d_" for the details grid.
  5. Because you set explicitly datatype to "json" before reloading of data from the server you can safe use loadonce: true for the second (details) grid. After the loading the data from the server datatype will be automatically set to "local", but you will have at the moment the data which corresponds selected row of the master grid.

The main schema of the code could be the following

var $master = $("#grid1"), $details = ("#grid2");

$master.jqGrid({
    colModel: [
        ...
        { name: "PctIndCode", ... },
        { name: "PctIndNum", ... }
    ],
    onSelectRow: function (rowid) {
        // set datatype to "json" and reload the grid.
        $details.jqGrid("setGridParam", { datatype: "json" })
            .triggerHandler("reloadGrid", [{current: true, page: 1}]);
    },
    loadComplete: function () {
        var selRowId = $(this).jqGrid("getGridParam", "selrow");
        if (selrow === null) {
            // no row selected after reloading of the master grid
            $details.jqGrid("setGridParam", { datatype: "local" })
                .triggerHandler("reloadGrid", [{page: 1}]);
        }
    },
    toppager: true,
    gridview: true
});
$master.jqGrid("navGrid", "#grid1_toppager",
    { add: false, edit: false, del: false, search: false,
        refreshstate: "current"});

$details.jqGrid({
    datatype: "local",
    idPrefix: "d_", // to be sure that no id duplicates with master grid exist
    url: "GetLetterInformationHandler.ashx",
    postData: {
        CurrentUser: 1157,
        TabName: "relatedLetters",
        IndCode: function () {
            var selRowId = $master.jqGrid("getGridParam", "selrow");

            return selRowId === null ? "" :
                $master.jqGrid("getCell", selRowId, "PctIndCode");
        },
        IndSerial: function () {
            var selRowId = $master.jqGrid("getGridParam", "selrow");

            return selRowId === null ? "" :
                $master.jqGrid("getCell", selRowId, "PctIndNum");
        }
    }
    ...
);
Community
  • 1
  • 1
Oleg
  • 220,925
  • 34
  • 403
  • 798
  • @ZSH: You are welcome! By the way, I see that you don't used till now your voting right (clicking on arrow up new the answer or question). It's *very important right*. *Searching on stackoverflow are based on voting*, because the users decide whether the information more or less helpful. You have right to vote 30 questions and answers **per day** (see [here](http://meta.stackexchange.com/a/5213/147495)). So **if you want to help other users to find helpful information of stackoverflow your should vote up all answers or questions which you read on the stackoverflow and which you find helpful**. – Oleg May 27 '13 at 13:15
  • I will be sure act to your recommendations to other users use of your useful information ^_^ – ZSH May 27 '13 at 16:04
  • @ZSH: It was just common recommendation, so that you understand what is the main goal of voting. If I search on stackoverflow or just on Google and find some answer on stackoverflow which helps me to go forward with my problem I vote the answer or both answer and the question. I want just that you understand your voting right. – Oleg May 27 '13 at 16:25
  • I did not know that the vote is important. Thank you for information – ZSH May 28 '13 at 04:52