0

I'm wondering how to have jqGrid custom formatter to call a seperate function, "test1"? I get an undefined error on the "test1" function.

Script #1...

//colModel json objects...
{ name: 'Vin', index: 'Vin' },
{ name: 'Links', index: 'Links', formatter: jqgridCellFormatterLink }

//jqGrid formatter function...
function jqgridCellFormatterLink(cellValue, options, rowObject) {
    return "<span onclick='test1(\"" + rowObject[0] + "\");'>Test</span>";
}

//non-jqGrid function
function test1(parmVin) {
    alert(parmVin);
}

Thanks...

//Script #2...

//colModel json objects...
{ name: 'Vin', index: 'Vin' },
{ name: 'Links', index: 'Links', formatter: function(cellValue,options,rowObject) { return "<span>Test</span>";} }

beforeSelectedRow: function(rowid, e) {
   if (this.p.colModel[$.jgrid.getCellIndex($(e.target).closest("td")[0])].name === 'Links') 
   {
       alert($('#blah').getCell(rowid, 0));  //Can be 0 or 'Vin'...
   } 
}
fletchsod
  • 3,560
  • 7
  • 39
  • 65

1 Answers1

1

I recommend you to use approach described in the answer and in this one. You don't need to bind onclick to some global method. Instead of that it's more effective to use beforeSelectRow or onCellSelect callback which will be called inside of one existing click event handle.

By the way, the formatter which you posted could don't work because the format of rowObject depend on many things: how you fill the grid, which datatype you use ("local", "json" or "xml" can produce different format of rowObject), whether you use repeatitems: true or some other settings of jsonReader, whether you use loadonce or not and so on.

Community
  • 1
  • 1
Oleg
  • 220,925
  • 34
  • 403
  • 798
  • Nice!! Now I got the "test1" function working using the jqGrid beforeSelectRow object. Now, how do I pass on a cell value to "test1" function in the jqGrid beforeSelectRow object? Thanks. – fletchsod May 16 '13 at 17:07
  • @fletchsod: You are welcome! Which way you use now? Where is the code? If you do all correctly you will get `e` (event) as parameter. You can get **all needed information** from `e.target`. – Oleg May 16 '13 at 17:09
  • I just updated the script above. I wrote them down on paper then type it here on special computer due to Intranet restriction. I'm using datatype: json. Not using repeatitems. I'm using loadonce: true . Hope my description is good enough. – fletchsod May 16 '13 at 17:33
  • @fletchsod: The code which you posted seems not good enough. What value you need to get? The `$(e.target).closest("tr").attr("id")` get you rowid. You can use standard jqGrid method `getCell` to get value from column if you know rowid and the column name. – Oleg May 16 '13 at 17:44
  • That's it. getCell function does the trick. Just edit the script above. Thanks for your time and help. :-) It works now. Wish things are a bit easier. :-D – fletchsod May 16 '13 at 18:45
  • @fletchsod: You are welcome! You should better use `$(this)` instead of constructs like `$('#blah')` inside of callbacks (inside of `beforeSelectedRow` code). `$('#blah')` search DOM element with id="blah" and then returns jQuery wrapper on the element. `this` inside of callback is already the DOM element of the table. So `$(this)` is always quicker as `$('#blah')`. Moreover one copy frequently code examples from one project to another one. Constructs like `$('#blah')` have to be modified, but `$(this)` stay unchanged. – Oleg May 16 '13 at 18:51
  • @fletchsod: Another common remark: I see you don't used till now your voting right (UP arrows near questions and answers). It's the basis of success of stackoverflow. Searching engine use voting count as the main criteria. Only in the way *helpful* information will be suggested more as non-helpful. You have right to vote 30 answers or questions **per day** (see [here](http://meta.stackexchange.com/a/5213/147495)). So **if you want help other people to find helpful information of stackoverflow you should use your voting right** everything when you see *some helpful information* on stackoverflow – Oleg May 16 '13 at 18:58
  • Hmm.. How do I use $(this) if I have 2 seperate jqGrids on the same webpage? Hmm... – fletchsod May 19 '13 at 23:56
  • Thanks for the info on the UP arrow. Just did that now and I didn't know of this points feature. I know nothing about Stackoverflow and it doesn't feels like a forum, you know. :-) – fletchsod May 20 '13 at 00:00
  • @fletchsod: Every callback get `this` like an additional parameter. So if you have two grids then you can *share* the code of callbacks like `beforeSelectedRow` in both grids. – Oleg May 20 '13 at 08:16