0

My server provides the following json string:

{
    "total":3,
    "page":1,
    "records":52,
    "rows":[
        {"cell":[
             "6789",
             "Veridien Dynamics",
             "ver01",
             "Description of Site: ver01",
             "1986a594-bb12-4a4a-a70b-4b85251fd268",
             "UKSODMBHANU01",
             6440],
         "id":120}
         ......
    ]
}

In my grid, I would like to have a link for each row. The link is built using custom formatter. The url for the link needs to include the id (e.g. 120). I cannot obtain the value of id from cellvalue, options, nor the rowObject in my custom formatter. Since id is not part of the 'cell', I don't know how to obtain this value to construct the url.

Following is part of my grid definition :

$("#list").jqGrid({
    url:'http://mydomain:8080/myserver/api/v1/data-grid.json',
    loadBeforeSend: function(xhr) {
        xhr.setRequestHeader("Authorization",'Basic xxxxxxxxxxxxxxxxxxxxxxxxx');
        return xhr;
    },
    datatype: "json",
    jsonReader : {
        root: "rows",
        page: "page",
        total: "total",
        records: "records",
        repeatitems: true,
        cell: "cell",
        id: "id"
    },
    colNames:['Customer ID','Customer','Site ID','Site', 'Server ID','Server Name',
              'XML Size','id'],
    colModel :[ 
        {name:'customerID',editable:false},
        {name:'customerName',editable:false},
        {name:'siteID',editable:false,width:60, align:'center'},
        {name:'siteDescription',editable:false,align:'center'},
        {name:'serverID',editable:false,width:150,align:'right'},       
        {name:'serverName',editable:false, width:150,align:'right'},
        {name:'xmlSize',editable:false, align:'right'},
        {name:'id', index:'id', editable:false,       
             align:'center',formatter:that.xmlLinkFormatter},

   ],
   ......
}

Following is my xmlLinkFormatter, everything works fine if I hard code the id value here to be part of the targetURL.

xmlLinkFormatter:function(cellvalue, options, rowObject){
    var targetURL = "http://mydomain:8080/myServer/data/showXml/"+
                    IwantIDValue here;
    var link = "<a href='javascript: void(0)' onclick=\"window.open('" +
               targetURL + "');\">View XML</a>";

    return link;
}
Oleg
  • 220,925
  • 34
  • 403
  • 798
xueru
  • 767
  • 2
  • 11
  • 21

1 Answers1

0

I suggest you to use unobtrusive JavaScript to build the link. So the custom formatter can be very easy:

xmlLinkFormatter:function(cellvalue, options, rowObject){
    return "<a href='#'>View XML</a>";
}

now the contain of the 'id' column will be the same as in your example. To make 'onclick' binding I suggest to use jQuery.click.

The way how one can implement unobtrusive JavaScript in jqGrid I described here and here. So I will use almost the same way and will use the same simple helper function getColumnIndexByName:

loadComplete: function() {
    var i=getColumnIndexByName(myGrid,'id');
    // nth-child need 1-based index so we use (i+1) below
    $("tbody > tr.jqgrow > td:nth-child("+(i+1)+") > a",myGrid[0]).click(function(e){
        var tr=$(e.target,myGrid[0].rows).closest("tr.jqgrow");
        //alert("clicked the row with the id='"+tr[0].id+"'");
        e.preventDefault();
        window.open("http://mydomain:8080/myServer/data/showXml/"+
                    encodeURIComponent(tr[0].id));
    });
}

(the variable myGrid here is defined as var myGrid=$("#list");) How you can verify in the live demo here, the approach works.

Community
  • 1
  • 1
Oleg
  • 220,925
  • 34
  • 403
  • 798