开发者

How to enable click in edit action button if new row is saved jqgrid

开发者 https://www.devze.com 2023-04-01 08:55 出处:网络
Edit formatter action button is placed to jqgrid column: colModel: [{\"fixed\":true,\"label\":\" change \",\"name\":\"_actions\",\"width\":($.browser.webkit == true? 37+15: 32+15)

Edit formatter action button is placed to jqgrid column:

colModel: [{"fixed":true,"label":" change ","name":"_actions","width":($.browser.webkit == true? 37+15: 32+15)
    ,"align":"center","sortable":false,"formatter":"actions",
"formatoptions":{"keys":true,"delbutton":false,"onSuccess":function (jqXHR) {actionresponse = jqXHR;return true;}
    ,"afterSave":function (rowID) {
    cancelEditing($('#grid'));afterRowSave(rowID,actionresponse);actionresponse=null; }
    ,"onEdit":function (rowID) {
      if (typeof (lastSelectedRow) !== 'undefined' && rowID !== lastSelectedRow)
        cancelEditing($('#grid'));
        lastSelectedRow = rowID;
        }
    }}

New row is added to jqgrid in loadcomplete event

var newRowData = {};
var newRowId = '_empty' + $.jgrid.randId();
$('#grid').jqGrid('addRowData', newRowId, newRowData);

and its id is updated if save action button is clicked:

function aftersavefunc(rowID, response) {
    restoreActionsIcons();
    $('#grid').jqGrid('resetSelection');
    var json = $.parseJSON(response.responseText);
    $("#" + rowID).attr("id", json.Id);
    lastSelectedRow = json.Id; 
    $("#grid").jqGrid('setSelection', lastSelectedRow);
}
开发者_JAVA技巧

After clicking save action button edit action button clicks are ignored. It is not possible to re-enter to edit mode after first editing.

How to fix this so that row can edited by edit button click again after saving ?

Update

I added $(this).focus() as suggested in Oleg answer and also wrapped id change into setTimeout as Oleg recommends in other great answer:

function aftersavefunc(rowID, response) {
    restoreActionsIcons();
    $(this).focus();
    $('#grid').jqGrid('resetSelection'); 
    var json = $.parseJSON(response.responseText);
    setTimeout(function () {
        $("#" + rowID).attr("id", json.Id);
        lastSelectedRow = json.Id;
        $("#grid").jqGrid('setSelection', lastSelectedRow);
    }, 50);
}

Problem persists. The problem may related to row id change since:

  1. It occurs only in last row (where id is changed after save). It does not occur for saved rows where responseText returns same id and row id is actually not changed.
  2. It does not occur if cancel action button is pressed.

Maybe row id needs additional reset id addition to resetSelection or needs updated in somewhere other place also.

Update2

I added code form updated answer to errorfunc and used only english characters and numbers id ids. This allows to click multiple times but introduces additional issue:

extraparam is no more passed. If rowactions() calls are commented out, extraparam is passed with with rowactions calls extraparam is not passed.

I changed jqGrid source code and added alert to rowactions method:

alert( cm.formatoptions);
if (!$.fmatter.isUndefined(cm.formatoptions)) {
  op = $.extend(op, cm.formatoptions);
  }

In first clicks alert outputs 'Object'. In succeeding clicks to Save button it outputs undefined. So for unknown reason formatoptions is cleared.

Remarks to comment:

  1. Absolute url in testcase is not used. Datasource is set to localarray. I verified that testcase works in IE and FF without external url access. For extraparam issue I can create new testcase.

  2. Without image directory buttons are shown in cursor is moved over them. Missing image directory still allows to reproduce the issue.

  3. FormData function is defined in js file.

Since new issue occurs after adding rowactions() calls and does not occur if those calls are removed, this seems to be related to the code proposed in answer.


I suppose that the problem exist because one hide a button which has currently focus. Look at the code from the answer. If one remove the line $(this).focus(); // set focus somewhere one has the same problem as you describes. So I suggest that you just try to set somewhere, for example in restoreActionsIcons the focus to any the table element of the grid after hiding the button having currently the focus. I can't test this, but I hope it will help.

UPDATED: I examined your problem one more time and I hope I can suggest you a solution.

You problem can be divided on two sub-problems. The main your problem is the the changing of the id of the row. So it is not common problem which everybody has.

The problem is that "actions" formatter create onclick functions directly in the HTML code (see for example here):

ocl = "onclick=$.fn.fmatter.rowactions('"+rowid+"','"+opts.gid+"','edit',"+opts.pos+");..."

So the functions will contains the original rowid. To fix the problem you can modify the code fragment of your aftersavefunc inside of setTimeout from

$("#" + rowID).attr("id", json.Id);
lastSelectedRow = json.Id;
$("#grid").jqGrid('setSelection', lastSelectedRow);

to something like the following:

var $tr = $("#" + rowID),
    $divEdit = $tr.find("div.ui-inline-edit"),
    $divDel = $tr.find("div.ui-inline-del"),
    $divSave = $tr.find("div.ui-inline-save"),
    $divCancel = $tr.find("div.ui-inline-cancel");

$tr.attr("id", json.Id);
if ($divEdit.length > 0) {
    $divEdit[0].onclick = function () {
        $.fn.fmatter.rowactions(newId,'grid','edit',0);
    };
}
if ($divDel.length > 0) {
    $divDel[0].onclick = function () {
        $.fn.fmatter.rowactions(newId,'grid','del',0);
    };
}
if ($divSave.length > 0) {
    $divSave[0].onclick = function () {
        $.fn.fmatter.rowactions(newId,'grid','save',0);
    };
}
if ($divCancel.length > 0) {
    $divCancel[0].onclick = function () {
        $.fn.fmatter.rowactions(newId,'grid','cancel',0);
    };
}
lastSelectedRow = json.Id;
$("#grid").jqGrid('setSelection', lastSelectedRow);

The second problem is that you use special characters inside of ids. I found a bug in the $.fn.fmatter.rowactions which need be fixed to support special characters in ids. The problem is that in the line 407 of jquery.fmatter.js the original rowid parameter rid will be changed:

rid = $.jgrid.jqID( rid )

and later everywhere will be used modified id. For example in the id is my.id the encoded version will be my\\.id. It's correct for the most places of the $.fn.fmatter.rowactions code (see here), but it' s incorrect as the rowid parameter of the editRow, saveRow, restoreRow, delGridRow, setSelection and editGridRow (see the lines 433-453). So the code must be fixed to use the original not escaped (not encoded) rid value with which the $.fn.fmatter.rowactions was called.

I think I will post tomorrow the corresponding bug report with the suggestions in the trirand forum.

UPDATED 2: The code $.fn.fmatter.rowactions(newId,'grid','edit',0); which I wrote above is just an example. I took it from the test demo which you send me. You should of course modify the code for your purpose. How you can see for example from the line the second parameter of the $.fn.fmatter.rowactions in the id of the grid which you use: 'grid', 'list' of something like myGrid[0].id. The last parameter should be the index of the column having formatter:'actions' in the colModel. You can use getColumnIndexByName function from the answer on your old question to get the index by column name.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号