8

I have a jqGrid where a row is editable on click (i.e. 'editRow' inside 'OnSelectRow' works fine). But my requirement is to "load the grid with ALL ROWS IN EDIT MODE by default (Inline edit)", so there should not be any need for me click individual rows. Can someone throw some lights on?

I tried the below code but didnt work

var data_val = myGrid.getRowData();
for (var i=0;i<data_val.length;i++)
{
myGrid.editRow(data_val[i], true);
}
CodeMad
  • 950
  • 2
  • 12
  • 30

1 Answers1

15

You have to enumerate all rows of grid and call editRow for every row. The code can be like the following

loadComplete: function () {
    var $this = $(this), ids = $this.jqGrid('getDataIDs'), i, l = ids.length;
    for (i = 0; i < l; i++) {
        $this.jqGrid('editRow', ids[i], true);
    }
}

or the following

loadComplete: function () {
    var $this = $(this), rows = this.rows, l = rows.length, i, row;
    for (i = 0; i < l; i++) {
        row = rows[i];
        if ($.inArray('jqgrow', row.className.split(' ')) >= 0) {
            $this.jqGrid('editRow', row.id, true);
        }
    }
}
Oleg
  • 220,925
  • 34
  • 403
  • 798
  • 1
    @Oleg I would suggest using jQuery's `hasClass` function: `$(row).hasClass('jqgrow');` instead of the verbose and unreliable `inArray` function: `$.inArray('jqgrow', row.className.split(' ')) >= 0`. You can never know the size of the white-space (`' '`). – Yair Nevet Oct 25 '12 at 06:50
  • @YairNevet: Of cause one can use `$.hasClass` and enumerate `this.rows` with respect of `$.each`. You will get smaller code, but the performance of the code which I posted is better. By the way you can see that I save `rows.length` in the `l` variable and compare `i` with `l` in the loop instead of usage `i < rows.length`. All the steps are only because of better performance. If the performance aspect is not so important for you you can use `$.hasClass` and `$.each` instead. One more remark: if you have grid without subgrids and grouping you can remove `$.inArray` test and start with `i=1`. – Oleg Oct 25 '12 at 06:59
  • 1
    Thank you @Oleg for all of your useful answers to jqGrid questions throughout the site. I do have an improvement to this answer, however. I assume that the `editRow` method assumes that the user will be editing the referenced row, so it dutifully scrolls the row into view, but in this case it causes an undesired effect. When run as-is, any fixed-height grid which contains more rows than the height supports will scroll to the bottom of the list. Simply reversing the order of the for loop helps to keep the initial view grid more like the default behavior: `for (i = l; i > -1; i--) {`. – Starson Hochschild Jun 17 '13 at 13:50
  • Continuing on my previous post. @Oleg, perhaps you know of a way to temporarily turn off the "Scroll into view" behavior, which may not only negate the need to walk through the rows backwards, but also speed things up due to less DOM manipulation and reflow/rendering. Either way, I'll leave it to you to update your answer as you see fit. – Starson Hochschild Jun 17 '13 at 13:51
  • @StarsonHochschild: You are welcome! In general I agree with you with changing of order of editing items (`for (i = l; i > -1; i--) {`), but I don't recommend to set many rows in editing mode at the same time. It create many disadvantaged in jqGrid and can follow that user thing that row is changed, but it is not so. If one have scrolling then one probably use the feature in wrong case. – Oleg Jun 17 '13 at 14:03
  • @Oleg I have a little issue. I have a multi-select grid and it is on inline edit mode. I only want the selected rows to be in edit mode. That is, if a user selects a row, the row becomes editable. If the user unselects a row the row is reinitialized and editable is removed from it. If a row is unselected, it looses it's edited values. I also need a way of getting the values of the editable cells on a click of a button. – Uchenna Nwanyanwu Jun 17 '13 at 14:09
  • 1
    @UchennaNwanyanwu: It sounds like the standard way to call `editRow` inside of `onSelectRow`. The callback `onSelectRow` have the second parameter which represent the `status` of the selection. So you can distinguish select from unselect in case of usage `multiselect: true`. If you use *local* editing then you can need to get modified rows. You should first save the data by calling `saveRow` for all editing rows and then use `$("#grid").jqGrid("getGridParam", "data")` to get all local data. You can send the data to the server per manual `$.ajax` request. – Oleg Jun 17 '13 at 14:20
  • @Oleg That was a nice one. Thanks, `saveRow` solved the issue. But in `multiselect` mode, only the last clicked row gets into the edit mode. This is called in the event that is called when a row is selected in `multiselect` mode. `for(var i = 0; i < rowIds.length; i++) { var rowId = rowIds[i]; grid.jqGrid('editRow', rowId, true); } `. But this doesn't work exactly as i want it to work. I want all the selected rows to be in edit mode i.e. the editable text boxes should be in edit mode for the selected rows. – Uchenna Nwanyanwu Jun 18 '13 at 08:25
  • @Oleg I did end up writing a faster way that fit my needs for the particular grid I was working on. In my case I have a matrix of attributes and users with a single checkbox in each cell, then a custom button in the nav that saves all data. I didn't need all of what editRow does, I only need the user to be able to make all of their changes then save everything in one click. This one-liner does the trick for me: `$(this).find("input").removeAttr("disabled");` The one-liner is almost 2 orders of magnitude faster on my machine. – Starson Hochschild Jun 21 '13 at 17:15
  • @StarsonHochschild: Probably you use `formatter: "checkbox"` without specifying of `formatoptions: {disabled: false}`? – Oleg Jun 21 '13 at 19:21
  • @Oleg No, that's all set correctly. I just needed the entire grid of checkboxes to be editable at the same time. Since I'm using a custom function to post the data all at once which is triggered by a button in the bottom nav of the grid, all I needed to do was enable the actual checkboxes. Thanks again. You are a true asset to the jqGrid community as a whole and I think Tony is lucky to have you around :D – Starson Hochschild Jun 21 '13 at 20:07