0

I have a jqgrid and am calling 'returnHyperLink' in my custom formatter. This code works fine, and goes to the link I am requesting:

function returnHyperLink(cellValue, options, rowdata, action) {
     return '<a id="myLink" href="#" onclick="switchPage(\''+cellValue+'\');">'+cellValue+'</a>'

}

function switchPage(cellValue) {
     $("#contents").load("jsp/newpage.jsp");
}

But I would like to add one more parameter when calling calling switchPage from returnHyperLink.

I am looking for the correct formatting to something like this:

function returnHyperLink(cellValue, options, rowdata, action) {
return '<a id="myLink" href="#" onclick="switchPage(\''+cellValue+'\', \''+rowdata+'\');">'+cellValue+'</a>'
}

So that I can access that particular rowdata object and extract information from it before calling $("#contents").load("jsp/newpage.jsp"); in switchPage

Obviously, my new switchPage function would look like this:

function switchPage(cellValue, rowdata)
Jules Mena
  • 67
  • 8
  • So what is not working ? – Rayon Jun 24 '16 at 15:47
  • @Rayon When I said "I am looking for the correct formatting to something like this," that's the code that doesn't seem to be right. When I try to console.log rowdata.name, for example, it prints 'undefined.' When I try the same thing from inside of returnHyperLink, it works correctly. I don't think my parameter is going through – Jules Mena Jun 24 '16 at 15:55
  • https://jsfiddle.net/rayon_1990/f4ro0noj/ – Rayon Jun 24 '16 at 16:43

2 Answers2

0

You can not pass rowdata directly because you can't pass objects inside inline onclick events. To do that you should put it to onlick defined inside a javascript as described in this Question but that is not going to work for you.

So what you should do ispass all the rowdata fields you need one by one lets say you need Field1 and Field2 from your rowdatathen you do this

 function returnHyperLink(cellValue, options, rowdata, action) 
 {
   return '<a id="myLink" href="#" onclick="switchPage(' + cellValue+ ', ' + rowdata.Field1+ ', ' + rowdata.Field2+');">' + cellValue+ '</a>';          
 }

function switchPage(cellValue,field1, field2) {
    // do whatever with field1,field2...
     $("#contents").load("jsp/newpage.jsp");
}
Community
  • 1
  • 1
Helen Araya
  • 1,886
  • 3
  • 28
  • 54
0

You could build your link string using an array, something like...

    function returnHyperLink(cellValue, options, rowdata, action) {
        var link = "<a id='myLink' href='#' onclick='switchPage(" + cellvalue;
        $.each(rowdata, function (index, value) {
            link += ", " + value
        });
        link + ');">' + cellValue + '</a>';
        return link
    }