【问题标题】:jqgrid: how to set toolbar options based on column value in row selectedjqgrid:如何根据所选行中的列值设置工具栏选项
【发布时间】:2011-07-19 10:10:42
【问题描述】:

我有一个具有值的列字段类型(可编辑,只读)。所有行都将填充这些值之一。

我只想在选定行的列值可编辑时启用/禁用工具栏选项编辑。

如何在 jqgrid 中实现这一点。

【问题讨论】:

    标签: jqgrid jqgrid-asp.net


    【解决方案1】:

    如果我理解您是正确的,您希望根据所选行启用/禁用导航器的“编辑”或“删除”按钮。这样你就会有

    如果没有选择行或所选行不可编辑或标准导航工具栏

    如果该行是可编辑的。

    “可编辑”或“只读”列的条件似乎我错了,因为它是列上的条件列而不是行上的条件列,但您可以轻松实现自己的自定义条件。

    实现可以是

    var myGrid = jQuery("#list");
    myGrid.jqGrid({
        /* definition of jqGrid */
        beforeSelectRow: function(rowid) {
            var selRowId = $(this).getGridParam('selrow'),
                tr = $("#"+rowid);
            // you can use getCell or getRowData to examine the contain of
            // the selected row to decide whether the row is editable or not
            if (selRowId !== rowid && !tr.hasClass('not-editable-row')) {
                // eneble the "Edit" button in the navigator
                $("#edit_" + this.id).removeClass('ui-state-disabled');
                $("#del_" + this.id).removeClass('ui-state-disabled');
            } else {
                // unselect previous selected row
                // disable the "Edit" and "Del" button in the navigator
                $("#edit_" + this.id).addClass('ui-state-disabled');
                $("#del_" + this.id).addClass('ui-state-disabled');
            }
            return true; // allow selection or unselection
        },
        loadComplete: function() {
            // just one example how to mark some rows as non-editable is to add
            // some class like 'not-editable-row' which we test in beforeSelectRow
            $("tr.jqgrow:even",this).addClass('not-editable-row');
        }
    }).jqGrid('navGrid','#pager');
    // disable "Edit" and "Delete" button at the beginning
    $("#edit_" + myGrid[0].id).addClass('ui-state-disabled');
    $("#del_" + myGrid[0].id).addClass('ui-state-disabled');
    

    要启用/禁用“编辑”和“删除”按钮,我们添加/删除导航工具栏按钮上的“ui-state-disabled”类。在上面的代码中,我将所有带有偶数的行标记为“不可编辑”。在您的情况下,您可以使用任何其他更有意义的标准。

    您可以在here现场观看演示。

    【讨论】:

    • 感谢您的完美回答。禁用/启用编辑/删除选定行的列值的条件。场景是:系统中有创建者/查看者/批准者。我有存储条目状态的列:新/已提交/已批准。一旦 column:status 提交了值,它就不可编辑。因此对于创建者 column:status new 表示可编辑行,但 column:status approved 表示不可编辑行。因此启用或禁用删除/编辑的标准基于所选行的列中的值,而不是基于行。
    • @user668829:好的,现在很清楚了。所以使用可以修改我的代码并使用jqGrid('getCell',rowid,'status') 来获取所选行的“状态”列的值。然后您可以测试该值是否“已提交”并调用addClass('ui-state-disabled')removeClass('ui-state-disabled'),就像我在回答中那样。如果问题解决了你可以“接受”我的回答(详见here
    • @Oleg:你为什么if (selRowId !== rowid?如果我选择可编辑行,然后再次单击它,则编辑/删除按钮将被禁用。有意为之?
    • @Stahlkocher:我使用了beforeSelectRow 内部的代码。它将被调用之前该行将被选中。如果selRowId === rowid 那么我们选择previous selected row。换句话说,用户在选定的行上再单击一次。您可以自行决定在这种情况下哪种行为最合理。
    猜你喜欢
    • 2013-03-21
    • 2020-03-02
    • 2011-11-24
    • 2012-08-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-13
    • 1970-01-01
    相关资源
    最近更新 更多