根据另一列中自动完成的结果更改jqGrid文本列的可编辑属性 [英] Changing a jqGrid text column's editable property based on results of an autocomplete in another column

查看:263
本文介绍了根据另一列中自动完成的结果更改jqGrid文本列的可编辑属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用jqGrid 4.4.0进行内联编辑.为了这个问题,我的网格有四列:ID列(SomeGridRowId),带有jQuery自动完成功能的文本列(Autocomplete),单个字符文本列(SingleChar)和隐藏的boolean列(CanEditSingleChar) .我需要基于CanEditSingleChar列的值启用或禁用单个字符列的编辑.我已经使用onSelectRowsetColProp在现有行上进行了此工作,但是由于某些原因,我无法使其在新插入的行上正确运行.如果我添加新行并从自动完成功能中选择一个值,则SingleChar列总是 不可编辑.我已经使用Chrome和IE开发人员工具逐步浏览了Javascript;列的值和属性已正确设置,但是SingleChar列的editable属性不能反映这一点.

I am using jqGrid 4.4.0 with inline editing. For the sake of this question, my grid has four columns: an ID column (SomeGridRowId), a text column with a jQuery autocomplete (Autocomplete), a single character text column (SingleChar), and a hidden boolean column (CanEditSingleChar). I need to enable or disable editing of the single character column based on the value of the CanEditSingleChar column. I've got this working on existing rows using onSelectRow and setColProp, but for some reason I cannot get it to behave correctly on newly inserted rows. If I add a new row and select a value from the autocomplete, the SingleChar column is always not editable. I've stepped through the Javascript using the Chrome and IE developer tools; the column values and properties are getting set properly, but the SingleChar column's editable property does not reflect this.

我为巨大的代码片段表示歉意,但我不想遗漏任何内容.

I apologize for the gigantic code snippet, but I don't want to leave anything out.

$("#coolGrid").jqGrid({
    url: '@Url.Action("GetCoolGridData")',
    postData: {
        someId: function () { return $("#someId").val(); },
        otherStaticArg: function () { return 1; }
    },
    datatype: 'json',
    mtype: 'POST',
    loadonce: true,
    cellsubmit: 'clientArray',
    editurl: 'clientArray',
    scroll: true,
    pager: $("#coolGridPager"),
    rowNum: 200,
    sortname: 'SomeGridRowId',
    sortorder: 'asc',
    viewrecords: true,
    height: '100%',
    colNames: ['SomeGridRowId', 'CanEditSingleChar', 'Autocomplete', 'SingleChar'],
    colModel: [
        { name: 'SomeGridRowId', index: 'SomeGridRowId', hidden: true },
        { name: 'CanEditSingleChar', index: 'CanEditSingleChar', hidden: true }, 
        {
            name: 'Autocomplete',
            index: 'Autocomplete',
            width: 220,
            editable: true,
            edittype: 'text',
            editoptions: {
                dataInit: function (elem) {
                    $(elem).autocomplete({
                        source: '@Url.Action("GetSomeGridAutocomplete")',
                        minLength: 2,
                        select: function (event, ui) {
                            var rowId = getRowId($(this));
                            if (ui.item) {
                                $("#coolGrid").jqGrid('setCell', rowId, 'CanEditSingleChar', ui.item.CanEditSingleChar, '', '');
                                $("#coolGrid").jqGrid('setCell', rowId, 'Autocomplete', ui.item.label, '', '');
                                setSingleCharEditMode(rowId);
                            }
                        }
                    });
                }
            }
        },
        {
            name: 'SingleChar',
            index: 'SingleChar',
            width: 10,
            editable: true,     //default to true, most are editable
            edittype: 'text'
        }
    ],
    onSelectRow: function (clickedRow) {
        if (clickedRow && clickedRow != $.coolGridLastSelectedRow) {
            $("#coolGrid").jqGrid('saveRow', $.coolGridSelectedRow, false, 'clientArray');
            $.coolGridLastSelectedRow = clickedRow;
        }
        setSingleCharEditMode($.coolGridLastSelectedRow);
        $("#coolGrid").jqGrid('editRow', $.coolGridLastSelectedRow, true);
    },
    afterInsertRow: function (rowid, rowdata, rowelem) {
        if (rowid == 'new_row') {       //shown solely for completeness, pretty sure this is not the problem
            var newRowIndex = $("#coolGridNewRowIndex").val();
            if (!newRowIndex)
                newRowIndex = 1;
            var newRowId = rowid + "_" + newRowIndex;
            $("#new_row").attr('id', newRowId);
            newRowIndex++;
            $("#coolGrid").val(newRowIndex);
            $("#coolGrid").jqGrid('setSelection', newRowId, true);
        }
    }
});
$("#coolGrid").jqGrid('navGrid', '#coolGridPager',
{
    add: false,
    del: false,
    edit: false,
    search: false,
    beforeRefresh: function () {
        $("#coolGrid").jqGrid('setGridParam', { datatype: 'json' });
    }
});
$("#coolGrid").jqGrid('inlineNav', '#coolGridPager',
{
    edit: false,
    add: true,
    addtext: "Add",
    save: false,
    cancel: false,
    restoreAfterSelect: false,
    addParams: {
        position: 'last',
        addRowParams: {
            keys: true
        }
    }
});

setSingleCharEditMode函数:

function setSingleCharEditMode(rowid) {
    var rowData = $("#coolGrid").jqGrid('getRowData', rowid);
    if (rowData.CanEditSingleChar === "false") {
        $("#coolGrid").jqGrid('setColProp', 'SingleChar', { editable: false });
    } else {
        $("#coolGrid").jqGrid('setColProp', 'SingleChar', { editable: true });
    }
}

我已经尝试了一大堆东西,冲过一堆其他的SO问题,像疯了似的在谷歌上搜索..全部无济于事.我已经把头发拔了.在另一列上自动完成select之后,如何在新行上全部控制一列的editable属性?

I've tried a whole slew of stuff, rifled through a pile of other SO questions, googled like mad....all to no avail. I've resorted to pulling my hair out. How can I control the editable property of one column after an autocomplete select on another column, all on a new row?

编辑
最后,我在这里有了一个工作示例.如果您添加一行,然后在Autocomplete列中选择非典型*"之一,则可以重现此行为.

EDIT
I've got a working example up here, finally. If you add a row and then select either of the "Atypical *" in the Autocomplete column, you can reproduce this behavior.

推荐答案

没有实际示例,很难说清为什么不能编辑新添加的行.另外,我自己很难整理出一个简单的工作示例(例如,在 jsfiddle

Without an actual example it is hard to tell why newly added rows can not be edited. Also, it is quite hard to put together a simple working example myself (e.g. on jsfiddle or jsbin) due to jqGrid and other dependencies.

浏览完代码后可能会有所帮助的一些问题(或者可能对您已经尝试的内容没有什么新意):

A few questions after looking through your code that might help (or might be nothing new to what you already tried):

正确/唯一的行ID?-您是否已检查rowid对于新行是唯一的,以及您的 代码是否以正确的代码(即新行的ID)执行?您是否将console.log语句放入setSingleCharEditMode中以查看发生了什么(只是可以肯定的是,您说您已经完成了代码)?

Correct / unique row id? - Have you checked that the rowid is unique for new rows and that your code is executed with the correct one (i.e. the id from the new row)? Did you put console.log statements into setSingleCharEditMode to see what's going on (just to be sure, you said that you already stepped through the code)?

您可能正在设置单元格的editable属性 .另一行(考虑一下:不能这样,因为默认情况下该单元格应该是可编辑的,并且显式设置为不可被您的代码编辑.).与editable属性一起更改行的颜色,以轻松查看正在处理的单元格/行.

You might be setting the editable property of a cell in another row (Thinking about it: this can't be it, as the cell should be editable by default and the explicitely set to not being editable by your code.). Change the color of the row together with the editable property to easily see which cell/row is being worked on.

默认工作吗?-由于editable的默认值为true:您是否尝试过 禁用setSingleCharEditMode并看到该单元格是可编辑的 默认情况下?也许问题不在于您的评估,而在于 行本身?

Working by default? - As the default value of editable is true: have you tried disabling setSingleCharEditMode and see that the cell is editable by default? Maybe the problem isn't your evaluation but the adding of the row itself?

是正确的类型吗??-在setSingleCharEditMode中,您测试getRowData)? 我想您已经在该函数中广泛使用console.log来查看传递的id所发生的情况以及设置editable属性的比较

Correct type? - In setSingleCharEditMode you test the column value for strict equality with the string "false". Are you sure that the values have the same type for existing and new lines (should be, as they are strings and get parsed through the same jqGrid code: getRowData)? I guess you already have console.logged extensively in that function to see what is going on with the passed id and the comparison that sets the editable property‽

希望这对查找问题有帮助.就像我说的那样,如果没有可用的示例,调试起来真的很困难.您可能要在某个地方放一个.

Hope this helps a little to find the problem. As I said, it's really hard to debug without a working example. You might want to put one up somewhere.

[示例后编辑]

我首先看到的是,如果在现有的可编辑行中选择非典型躁狂症",则同样的行为也适用.另外:添加可编辑的行(双极性的东西)也可以.这似乎不是新行"与现有行"的问题.更像是将可编辑行更改为不可编辑的问题.

First thing I see is, that if you select "Atypical Manic Disorder" in an existing editable row, the same behaviour applies. Also: adding editable rows (Bipolar something) works. This does not seem to be a problem of "new row" vs. "existing row". More like a problem with editable rows that get changed to not editable.

我的猜测是这正在发生:

My guess is that this is happening:

  1. 您要添加新行,默认情况下列SingleChar是可编辑的,因此显示<input>.
  2. 您评估服务器响应并将自动完成select处理程序中的列设置为editable : false.
  3. 您取消选择该行,并且jqGrid还原了所有然后可编辑的行,因此它不会触及SingleChar,因为它认为 SingleChar不需要重置为初始状态.
  1. You are adding a new row, column SingleChar is editable by default, so the <input> is shown.
  2. You evaluate the server response and set the column to editable : false in the autocomplete select handler.
  3. You deselect the row and jqGrid reverts all then editable rows, so it doesn't touch SingleChar, because it thinks that SingleChar does not need to be reset to its initial state..

我说得通吗?

在jqGrid重置行或自己删除<input>之后,尝试将editable设置为false.

Try setting editable to false after jqGrid reset the row or removing the <input> yourself.

这篇关于根据另一列中自动完成的结果更改jqGrid文本列的可编辑属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆