2

如何在内联编辑jqgrid中将焦点设置在onSelectRow中的特定单元格上?

我尝试了以下答案:
1.需要 jqGrid 内联编辑才能将焦点转到单击的单元格
2.如何将焦点设置到单击以在 jqgrid 中开始内联编辑的
单元格 3.如何在 jqGrid 上编辑选定的单元格
4.设置jqgrid中选择行上可编辑输入字段的焦点

但这些都不起作用!

所以现在我不需要找到用户单击的单元格,但我需要光标准确地位于每一行的“lg_description”列中。我想问题出在 e 参数上,我认为当我将其设置为“lg_description”列时,我只需要在某处附加“lg_description”而不是 e 参数。

编辑:
这是我的代码:

onSelectRow: function(id, status, e){
    var grid = $(this); 
    if(id && id!==lastSel){               
        grid.restoreRow(lastSel);   
        lastSel=id;

        //cursor focus

        //first attemp: $("#lg_description").focus();
        //second attemp: $("input, select",e.target).focus();
        //third attemp: document.getElementById("lg_description").focus();
        //fourth attemp:
        /*var setFocusToCell = function(e) {
            if(!e || !e.target) return;
            var $td = $(e.target).closest("td"), $tr = $td.closest("tr.jqgrow"), ci, ri, rows = this.rows;
            if ($td.length === 0 || $tr.length === 0 || !rows) return;
            ci = $.jgrid.getCellIndex($td[0]);
            ri = $tr[0].rowIndex;
            $(rows[ri].cells[ci]).find("input,select").focus();
        }
        setFocusToCell(e);*/
    }
    grid.editRow(id, true,"","","","",aftersavefunc);  
    //fifth attemp: grid.editRow(id, true,function() {$("#lg_description").focus();},"","","",aftersavefunc);
 },

我尝试了 5 种不同的尝试,其中第 5 种替代了上面的代码

4

1 回答 1

3

如果内联编辑,您会在调用创建输入字段之前多次尝试设置焦点。editRow此外,您尝试使用的原因很奇怪

$("#lg_description").focus();

设置id="lg_description"焦点。是编辑行的rowid"lg"吗?列名是"description"?您引用的答案中的演示将焦点设置在单击的单元格中的或中(的子级)。如果您想始终将焦点设置在特定列上,那么您应该使用<input><select>e.target

$("#" + $.jgrid.jqID(id) + "_lg_description").focus();
// where "lg_description" is the column name

使用$.jgrid.jqID(id)而不是id更安全,因为 id 在常见情况下可能具有必须在选择器中转义的元字符。有关更多详细信息,请参阅jQuery 文档

于 2013-09-05T09:54:11.880 回答