0

I wish to iterate through jqGrid, and for a given column (ie: the second) I wish to insert a value. How do you find the first data row? The documentation warns to not use getRowData if updating row or cell data.

This is what I'm using, but it seems clumsy:

function loadCompleted() {
    var $grid = jQuery("#jqGrid"), rows = $grid[0].rows;

    for (var i = 1; i < rows.length; i++) {
        var row = rows[i];
        var id = row.cells[0].innerHTML;

        $(row.cells[1]).html("<a href='#' onclick='deleteApp(" + id + "); return false;'>Delete</a>");
    }
}

... this works, but it makes the assumption that the first data row is the second row in table #jqGrid. It also relies on index values for columns 1 and 2.

Is there any way to use setRowData when the documentation warns to not use getRowData when editing the row or cells?

Developer Webs
  • 983
  • 9
  • 29
  • How you fill the data in jqGrid? Do you use `datatype: "local"` together with `data` parameter which contains all data needed be placed in the grid? – Oleg Dec 21 '18 at 17:03
  • @Oleg We post to a server (.NET Core 2.X) with data type JSON. – Developer Webs Jan 02 '19 at 20:59
  • It would be better if you post more detailed description of your problem. What is your real problem: 1) performance, 2) understanding why you need to skip the first row? ... Which options of jqGrid you use? Do you use `loadonce: true` option for example? Do you use data grouping, subgrids, TreeGrid and so on? From performance point of view one should never use the code which you posted (at least if you need to display many rows or the grid on complex HTML page). Instead of that you should use custom formatter, which will build the content of the column. – Oleg Jan 02 '19 at 21:06
  • @Oleg The real problem is that the server-side doesn't (shouldn't) return a value for a given column in the model. That column will contain a link to edit/delete the record. So when the data comes back from the server I want to insert HTML into the empty column. We skip the first row because the first row (if you look in the TRs produced by jqGrid) is empty. But who knows, maybe in the future the jqGrid people will make the first 2 or 3 rows empty. I'd like the code to be robust. We do not use loadonce:true. There is no grouping or subgroups, no treegrid. – Developer Webs Jan 02 '19 at 21:13
  • jqGrid uses CSS style `table-layout: fixed;` for the main grid. It improve the performance of rendering because the width of columns defines **the first row** of the ``. Because of that jqGrid uses one empty row (`tr.jqgfirstrow`), which cells specifies explicitly CSS inline style with `width` value. If you need to enumerate rows the you should take in considerations only rows having CSS class `"jqgrow"`. See [the answer](https://stackoverflow.com/a/7084920/315935) and [the picture](https://free-jqgrid.github.io/getting-started/index.html#grid-internal-div).
    – Oleg Jan 02 '19 at 21:28
  • On the other side one should **reduce modifications of HTML page**. If you change content of *one cell* then the web browser have to recalculate properties of almost all other HTML elements existing on the page (for example position etc). It's "web browser reflow" (see [here](https://developers.google.com/speed/articles/reflow)). Using custom formatters or existing link formatter allows to **create grid body** with correct content instead of creating empty cells and modifying the cells in the loop, which is ineffective. Which **version** of jqGrid you use and from which **fork** of jqGrid? – Oleg Jan 02 '19 at 21:32
  • @Oleg We're using 4.6, which I believe was the last version before they went commercial? So we're using the original, not one of the newer forks. – Developer Webs Jan 04 '19 at 22:05
  • It's your choice to use a jqGrid version or fork, but you should understand that the version 4.6 is **5 years old** and it's dead. Even if you don't modify your web application Chrome and Firefox publish every 1.5-2 months new version of web browser and your web application based on jqGrid 4.6 can stop working or render incorrectly (see clase problem [here](https://stackoverflow.com/a/10621951/315935)). Thus I'd recommend you to upgrade either to the current version 4.15.5 of "free jqGrid" fork, which I develop or to Guriddo jqGrid JS 5.3.2, which develop Tony Tomov. – Oleg Jan 04 '19 at 22:14
  • @Oleg pfft, we still have a LOT of code in VB6 and ASP classic :( LOL – Developer Webs Jan 04 '19 at 22:39

1 Answers1

0

The first row in grid is hidden and is used in the jqGrid for internal purposes.

I think that using a custom formatter will do the job.

Example of custom formatter can be found here. If you use Guriddo jqGid you may look into the docs for the parameters passed.

Tony Tomov
  • 3,122
  • 1
  • 11
  • 18