【问题标题】:Need to make individual jqgrid cells editable based on a certain value需要根据某个值使单个 jqgrid 单元格可编辑
【发布时间】:2019-05-24 15:27:29
【问题描述】:

我有一个 jqGrid,我希望某些单元格可以根据单独隐藏单元格中的值进行编辑。所以网格中的每一行都没有相同的配置。换句话说,我不希望整列都是可编辑的。

我将下面的代码放在网格的 loadComplete 事件中。我遍历每一行并根据隐藏值 ProductCatIndex 将 EstimatedCost 和 AverageSalePrice 上的可编辑属性设置为 true。

var ids = $('#' + jqgrid_id).jqGrid('getDataIDs');
var count = $('#' + jqgrid_id).getGridParam('reccount');

for (var x = 0; x < count; x++) {
    var rowId = ids[x];

    if (row.ProductCatIndex == 2) {
        $('#' + jqgrid_id).jqGrid('setCell', rowId, 'EstimatedCost', '', '', { 'editable': true });
    }
    else if (row.ProductCatIndex == 3) {
        $('#' + jqgrid_id).jqGrid('setCell', rowId, 'AverageSalePrice', '', '', { 'editable': true });
    }
}

我单步执行代码并看到它正常运行,但是单元格不可编辑。我在网格级别有 cellEdit: true 但我没有在列上设置可编辑属性,因为我试图在上面的代码中动态设置它。任何帮助将不胜感激!

【问题讨论】:

    标签: jqgrid


    【解决方案1】:

    你这样做的方式没有任何影响

    我可以推荐其他方式来做到这一点。

    如果单元格具有“不可编辑单元格”类,则单元格不可编辑,而不是在 colModel 中将其设置为可编辑。见docs here

    这样可以使用 cellattr 函数,因为它会在创建过程中添加属性。见docs here too

    将可编辑属性设置为 true 并尝试使用以下代码:

    colModel : [ 
        ...
        { name : 'EstimatedCost', editable : true,..., 
             cellattr : function( id, val, rawdata, cm, rdata)  { 
                 ret = ""
                 if( parseInt(rdata['ProductCatIndex'],10) !== 2 ) {
                     ret = " class='not-editable-cell'";
                 }
                 return ret;
             }
         }
    

    对其他字段执行相同操作。

    【讨论】:

    • 非常感谢!那太棒了!你能再回答一个关于这个的问题吗?当我单击列标题进行排序时,使用 cellattr 函数分配的属性似乎消失了。排序后如何保留或重置值?
    • 使用哪个版本的 jqGrid - Guriddo jqGid、free-jqGrid 或 jqGrid
    • 我们使用的是jqGrid 4.4.4,数据类型是'local'。
    • 这是一个非常旧的版本,它可能包含一些缺失的附加内容。它不再受支持。如果可能,请切换到支持的最后一个:由我们开发的商业Guriddo jqGrid,或似乎已停止的free-jqGrid
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-21
    相关资源
    最近更新 更多