简体   繁体   English

jqGrid-在“编辑”而不是“添加”上禁用内联下拉列表

[英]jqGrid - disabling inline dropdown on “edit” but not on “add”

It looks as if this question has been asked at least once before but was not answered. 看来此问题至少已被问过一次,但没有得到回答。 I have also seen this question answered regarding standard "form" based editing, but not inline. 我还看到有关基于标准“表单”的编辑的此问题的回答,但不是内联的。

Code

$(function() {
var lastSel;
var MSVendors = {'9990':'XXXXXX - LEXI','9991':'XXXXXX - RICH','9992':'XXXXXX - BIRM','9993':'XXXXXX - PEMB' };
$('#special_dialog').dialog({
    width:'auto',
    height:'auto',
    resizable:true
});
$.extend($.jgrid.defaults,{
    rowNum:250,
    rowList:[1000,2500,5000],
    viewrecords:true,
    sortorder:'asc',
    height:800,
    autowidth:true,
    deepempty:true,
    altRows: true,
    grouping: true,
    groupingView: {
        groupField: ["vendor"],
        groupColumnShow: [true],
        groupText: ["<b>WAREHOUSE : {0}</b>"],
        groupDataSorted: true,
        groupSummary: [false]
    }
});
var surplusGrid = $('#surplusGrid'),
    editingRowId,
    sEditParam = {
        keys: true,
        oneditfunc: function(id) {
            editingRowId = id;
            $('#surplusGrid_ilsave').removeClass('ui-state-disabled');
            $('#surplusGrid_ilcancel').removeClass('ui-state-disabled');
        },
        afterrestorefunc: function() {
            editingRowId = undefined;
        }
    },
    sAutoCompOpts = {
        source: function(request, response) {
            $.getJSON('/json/json.searchmultiMaterials.php',{term:request.term,type:'m'},function(data) {
                response(data);
            });
        },
        minLength: 3,
        focus: function(e,ui) {
            $('input:text[name="description"]').val(ui.item.description);
            $('input:text[name="vendor"]').val(ui.item.vendor);
            $('input:text[name="mfgr_partno"]').val(ui.item.mfgr_partno);
        },
        select: function(e,ui) {
            $('input:text[name="description"]').val(ui.item.description);
            $('input:text[name="vendor"]').val(ui.item.vendor);
            $('input:text[name="mfgr_partno"]').val(ui.item.mfgr_partno);
        }
    },
    sAddParam = {
        rowID: 'new',
        position:'last'
    };
surplusGrid.jqGrid({
    url: '/json/json.getSurplusStock.php',
    datatype:'json',
    emptyrecords: 'Surplus Stock is currently depleted!',
    colNames: ['ID','Type','Part#','Description','On-Hand','On-Order','On-Hold','Min Stock','Warehouse','Shelf','Bin'],
    colModel: [
        {   name:'id',
            index:'id',
            hidden:true,
            key:false,
            search:false,
            viewable:false
        },
        {   name:'type',
            index:'type',
            width:35,
            sortable:true,
            editable:false,
            align:'center',
            editoptions:{defaultValue:'B'},
            cellattr: function(rowId,val) {
                if (val == 'B') {
                    return 'class="blue_stock"';
                } else {
                    return 'class="gold_stock"';
                }
            }
        },
        {   name:'surplus_partno',
            index:'surplus_partno',
            width:140,
            sortable:false,
            editable:true,
            classes:'ui-ellipsis',
            edittype:'text',
            editoptions:{size:25},
            editrules:{required:true}
        },
        {   name:'description',
            index:'description',
            width:200,
            sortable:false,
            align:'left',
            editable:true,
            classes:'ui-ellipsis',
            edittype:'text',
            editoptions:{size:40},
            editrules:{required:true}
        },
        {   name:'on_hand',
            index:'on_hand',
            width:60,
            sortable:false,
            editable:true,
            align:'center',
            edittype:'text',
            editoptions:{size:6},
            editrules:{required:true,integer:true,minValue:0}
        },
        {   name:'on_order',
            index:'on_order',
            width:60,
            sortable:false,
            editable:true,
            align:'center',
            edittype:'text',
            editoptions:{size:6},
            editrules:{required:false,integer:true,minValue:0}
        },
        {   name:'on_hold',
            index:'on_hold',
            width:60,
            sortable:true,
            editable:true,
            align:'center',
            edittype:'text',
            editoptions:{size:6},
            editrules:{required:true,integer:true}
        },
        {   name:'min_threshold',
            index:'min_threshold',
            width:60,
            sortable:true,
            editable:true,
            align:'center',
            edittype:'text',
            editoptions:{size:6},
            editrules:{required:true,integer:true,minValue:0}
        },
        {   name:'vendor',
            index:'vendor',
            width:120,
            editable:true,
            align:'center',
            editoptions:{value:MSVendors},
            edittype:'select',
            editrules:{required:true,integer:true}
        },
        {   name:'shelf',
            index:'shelf',
            width:40,
            sortable:true,
            editable:true,
            align:'center',
            editoptions:{size:10}
        },
        {   name:'bin',
            index:'bin',
            width:40,
            sortable:true,
            editable:true,
            align:'center',
            editoptions:{size:10}
        }
    ],
    pager:'#surplusFoot',
    sortname:'b.id',
    caption:'Surplus Stock Inventory',
    onSelectRow: function(id) {
        if(id && id !== lastSel) {
            surplusGrid.jqGrid('restoreRow',lastSel);
            var cm = surplusGrid.jqGrid('getColProp','vendor');
            if (id != 'new') { cm.editable = false; }
            lastSel = id;
        }
        surplusGrid.jqGrid('editRow',id,true);
        $('#surplusGrid_ilsave').removeClass('ui-state-disabled');
        $('#surplusGrid_ilcancel').removeClass('ui-state-disabled');
    },
    editurl:'/jqg/jqg.saveSurplusStockEdit.php'
});
surplusGrid.jqGrid('navGrid','#surplusFoot',{
    add:false,
    edit:false,
    del:false
});
surplusGrid.jqGrid('inlineNav','#surplusFoot',{
    add:true,
    edit:true,
    editParams:sEditParam,
    addParams:sAddParam
});
// re-size all grids when dialog box resizes
$('#special_dialog').dialog({
    resizeStop: function(e,ui) {
        surplusGrid.jqGrid('setGridWidth', ui.size.width - 30);
    }
});

}); });

Works GREAT on inline ADD - dropdown appears and value is passed to the editing URL as expected. 在内联ADD上工作出色-出现下拉菜单,并将值按预期传递给编辑URL。 On inline EDIT, however, the entire dropdown vanishes, leaving a " " as the cell content rather than the value that was there when the grid initially loaded. 但是,在内联EDIT上,整个下拉列表消失了,只剩下一个“”作为单元格内容,而不是最初加载网格时的值。

The usage of inlineNav is a special case of inline editing, because editRow will be called. inlineNav的用法是内联编辑的一种特殊情况,因为将调用editRow Starting with jqGrid 4.5.3 inline editing supports beforeEditRow and beforeAddRow callbacks which was introduced mostly to provide additional customization in case of usage inlineNav . 从jqGrid 4.5.3开始,内联编辑支持beforeEditRowbeforeAddRow回调,它们的引入主要是为了在使用inlineNav情况下提供其他自定义。 The method beforeEditRow is more interesting, because it will be called in any way of calling editRow . beforeEditRow方法更有趣,因为它将以任何调用editRow方式进行调用。

Before providing an example of usage beforeEditRow I would remark that you have to fix the bug in sAddParam , which you use as the option of addRow . 提供使用情况的一个例子之前beforeEditRow我想此话,你必须修复的bug sAddParam ,您作为选项使用addRow You use unneeded and danger parameter rowID: 'new' . 您可以使用不需要的危险参数rowID: 'new' As the result the id of every new row will be the same: "new". 结果, 每个新行的ID 将相同:“ new”。 In the way you will have id duplicates. 这样,您将获得ID重复项。 The same problem exists in early versions of jqGrid. 早期版本的jqGrid中存在相同的问题。 The current version of jqGrid uses $.jgrid.randId() to generate new unique id for new added row if rowID is null or undefined . 如果rowIDnullundefined ,则当前版本的jqGrid使用$.jgrid.randId()为添加的新行生成新的唯一 ID。 The default value of rowID is null . rowID的默认值为null So I strictly recommend you to remove rowID: 'new' option. 因此,我rowID: 'new'建议您删除rowID: 'new'选项。

The next important thing is the meaning of addParams option of inlineNav . 接下来的重要事情是addParams选项的inlineNav The method addRow calls internally the same editRow method. 该方法addRow内部调用相同editRow方法。 The addRowParams property of addParams allows to specify the option of editRow called by addRow . addRowParams财产addParams允许指定的选项editRow被称为addRow So I strictly recommend you to use addParams in the following form 因此,我addParams建议您以以下形式使用addParams

var sEditParam = {
        ... // any options or callbacks
    },
    sAddParam = {
        position: 'last',
        addRowParams: sEditParam
    };

In the way you will be sure that all callbacks and options of inline editing will be applied even in case of usage addRow . 这样,您将确保即使在使用addRow情况下,也将应用所有内联编辑的回调和选项。

Now back to you main question. 现在回到您的主要问题。 I suggest that you use beforeEditRow to change editable property of vendor column. 我建议您使用beforeEditRow更改vendor列的editable属性。 To test whether the current row are just add or not I suggest to test existence of jqgrid-new-row class. 为了测试当前行是否只是添加,我建议测试jqgrid-new-row类的存在。 The corresponding code could be like the following 相应的代码可能如下所示

var sEditParam = {
    beforeEditRow: function (option, rowid) {
        var tr = $(this).jqGrid("getGridRowById", rowid);

        $(this).jqGrid("setColProp", "vendor", {
            editable: !$(tr).hasClass("jqgrid-new-row")
        });
    }
};
surplusGrid.jqGrid("inlineNav", "#surplusFoot", {
    add: true,
    edit: true,
    editParams: sEditParam,
    addParams: {position: "last", addRowParams: sEditParam}
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM