【问题标题】:Making actions appears in JQGrid only when a boolean is true仅当布尔值为 true 时,才会在 JQGrid 中执行操作
【发布时间】:2014-11-27 14:12:18
【问题描述】:

我在 JQGrid 中显示了数据,并获得了一个 Actions 列,其中有用于删除和编辑操作的图标。在我检索以显示在网格中的数据中,我有一个布尔值,我想仅在布尔值为真时显示图标。怎么可能?

这是我的 JQGrid 显示的一段代码:

jQuery("#datagrid").jqGrid({
        stateOptions: getStateOptions("creation-site"),
        url: listUrl,
        datatype: "json",
        loadError: viewError,
        colNames: ['', 'Nom', 'N de dépôt Geopost', 'IATA', 'Groupe ID', 'Site de rattachement', 'Poste comptable', 'Centre cout', 'Description'],
        colModel: [
            {name: 'myac', width: 80, fixed: true, sortable: false, resize: false, formatter: 'actions', formatoptions: {keys: true, editbutton: true, }},
            {name: 'nom', index: 'nom', editable: true, edittype: "text", sortable: true},
            {name: 'geopostDepotNumber', index: 'geopostDepotNumber', editable: true, edittype: "text", sortable: true},
            {name: 'iata', index: 'iata', editable: true, edittype: "text", sortable: true},
            {name: 'groupeId', index: 'groupeId', editable: true, edittype: "text", sortable: true},
            {name: 'siteRattachement', index: 'siteRattachement', editable: true, edittype: "text", sortable: true},
            {name: 'posteComptable', index: 'posteComptable', editable: true, edittype: "text", sortable: true},
            {name: 'centreCout', index: 'centreCout', editable: true, edittype: "text", sortable: true},
            {name: 'description', index: 'description', editable: true, edittype: "text", sortable: true}
        ],
        rowList: [10, 20, 50, 100, 500, 1000, 5000],
        pager: '#navGrid',
        sortname: 'title',
        sortorder: "asc",
        viewrecords: true,
        loadonce: true,
        gridview: true,
        ignoreCase: true,
        height: 'auto',
        editurl: 'clientArray',
        caption: '<spring:message code="creationsite.title"/>'
    });

编辑:这里是一个示例 JSON,2 行:

[{"id":1,"centreCout":"211177","geopostDepotNumber":"0401","iata":"MLV","posteComptable":"77999","referentielId":5,"siteRattachement":" ","nom":"Ceci est un nom","networkRefId":1,"networkRefName":"FR-CHR","description":"MARNE-LA-VALLEE","groupeId":"CHRF","manual":false},
{"id":2,"centreCout":"211174","geopostDepotNumber":"0402","iata":"FTV","posteComptable":"75998","referentielId":5,"siteRattachement":" ","nom":null,"networkRefId":1,"networkRefName":"FR-CHR","description":"ALFORTVILLE","groupeId":"CHRF","manual":false}]

应根据 JSON 中的“手动”字段禁用操作。

编辑:按照我从 Oleg 的回答中理解的内容,我将其添加到我的网格中:

rowattr: function (rd) {
            if (rd.manual === false) { // verify that the testing is correct in your case
                return {"class": "not-editable-row"};
            }
        },

但它仍然无法正常工作。

【问题讨论】:

    标签: jquery jquery-ui jqgrid


    【解决方案1】:

    如果我理解正确,那么在创建网格后或在设置值 isReadOnly 之后直接有条件地隐藏列 myac 就足够了:

    if (isReadOnly) {
        jQuery("#datagrid").jqGrid("hideCol", "myac");
    }
    

    另一个常见的评论。我建议您检查文档的the page 上的Default 列中的值。您将看到edittypesortable 的默认值是“文本”和true 值,您将它们用于colModel 的所有列。此外,您可以考虑针对cmTemplate 更改colModel 项目的默认值(请参阅the old answer)。再说明:index 的值必须与name 的值相同,以防使用loadonce: true。如果不指定index,则将自动使用name 属性的值。因此,您可以将代码示例中的 colModel 减少为

    colModel: [
        { name: 'myac', width: 80, fixed: true, sortable: false, resize: false,
            formatter: 'actions', formatoptions: { keys: true, editbutton: true },
            editable: false },
        { name: 'nom' },
        { name: 'geopostDepotNumber' },
        { name: 'iata' },
        { name: 'groupeId' },
        { name: 'siteRattachement' },
        { name: 'posteComptable' },
        { name: 'centreCout' },
        { name: 'description' }
    ],
    cmTemplate: { editable: true }
    

    这样的代码更容易阅读和维护。

    顺便说一下,如果你知道isReadOnly的值网格创建之前你可以另外使用

    cmTemplate: { editable: !isReadOnly }
    

    最后一句话:你使用sortorder: "asc" 选项,它有默认值。您可以删除该选项。您应该验证sortname: 'title' 是否正确,因为您没有'title' 列。我想这只是一个“剪切和粘贴”错误。

    更新:如果您需要禁用某些行的编辑,您可以执行以下操作

    rowattr: function (rowData) {
        if (rowData.manual === false) {
            return { "class": "not-editable-row" };
        }
    },
    loadComplete: function () {
        $(this).find("tr.not-editable-row")
            .find(".ui-inline-edit,.ui-inline-del")
            .hide();
    }
    

    The corresponding demo 使用来自Closed 列的值来禁用编辑。生成的网格如下图所示

    【讨论】:

    • 其实,不,这不是我想做的。对不起,如果我不够清楚。我希望我的图标出现在某些行上(当为每行定义的布尔值为真时)而不是其他行上(当布尔值为假时)。
    • @AlexisDufrenoy:对不起,我还是不明白你想要什么。您在问题的文本中写道:“我只想在布尔值为真时显示图标。”你真正想要什么?如果您执行jQuery("#datagrid").jqGrid("hideCol", "myac");,那么包含编辑和删除按钮的列就会消失。这不正是你刚刚在评论中写的吗?我写信告诉你,知道什么时候你设置“布尔值”的值非常重要? 在创建网格之前是否已知?它是 jqGrid 数据的服务器响应的一部分吗? ...
    • @AlexisDufrenoy:如果您只需要使某些特定行可编辑,您可以使用rowattr"not-editable-row" 类添加到行中。因此无法编辑行。查看the answer,您只需将"myAltRowClass" 替换为"not-editable-row"。描述了另一种选择here
    • @AlexisDufrenoy:请参阅我的回答的更新部分。
    • @AlexisDufrenoy:如果您想使用表单编辑而不是内联编辑,您应该将editformbutton: true 选项添加到formatoptions。如果您需要指定和其他表单编辑选项,您应该使用formatoptionseditOptions 属性。例如formatoptions: { editformbutton: true, editOptions: { closeAfterEdit: true }}
    猜你喜欢
    • 2023-03-27
    • 2020-10-06
    • 1970-01-01
    • 2017-12-22
    • 2020-06-02
    • 2018-03-24
    • 1970-01-01
    • 2018-12-04
    • 1970-01-01
    相关资源
    最近更新 更多