如何根据同一行上不同单元格中的数据禁用单元格中的内联编辑

时间:2013-12-31 15:15:21

标签: jqgrid

我想根据在不同单元格中找到的数据启用/禁用单元格上的内联编辑。

我正在使用格式化程序操作来显示编辑图标,这似乎意味着我无法通过onSelectRow进行任何检查,因为单击“编辑”图标时似乎没有触发。在阅读了一些不同的问题/解决方案后,我认为我可以简单地禁用/启用整个列的编辑,但这似乎不适用于我的下面的代码,而且我认为这将是一个糟糕的路线,因为看起来像一个用户可以单击同一显示图标中多行的编辑图标,每行将同时显示可编辑。似乎最好的方法是在单元级别启用/禁用编辑,但是我已经通过afterInsertRow和操作中的onEdit尝试了这一点,但是对于是否可以编辑单元格似乎都没有任何影响。

这是使用JQGrid 4.5.2,jQuery UI 1.9.1和jQuery 1.8.2

<script>
        $(document).ready(function() {
            var noteIcon = '<div title="Job Details" style="float:left; margin-left:5px;" class="ui-pg-div ui-inline-edit" onmouseover="jQuery(this).addClass(\'ui-state-hover\');" onmouseout="jQuery(this).removeClass(\'ui-state-hover\');"><span class="ui-icon ui-icon-note"></span></div>';

            getColumnIndexByName = function(grid,columnName) {
                var cm = grid.jqGrid('getGridParam','colModel');
                for (var i=0,l=cm.length; i<l; i++) {
                    if (cm[i].name===columnName) {
                        return i; // return the index
                    }
                }
                return -1;
            };

            jQuery.extend(jQuery.jgrid.defaults, { 
                altRows:true,
                altclass:'myAltRowClass',
                datatype: "json", 
                jsonReader: {
                    root: "ROWS", 
                    page: "PAGE", 
                    total: "TOTAL", 
                    records: "RECORDS", 
                    cell: "", 
                    id: "0"
                }, 
                emptyrecords: "No jobs to display.",
                rowList: [25,50,100], 
                viewrecords: true, 
                height: "auto", 
                loadonce: true, 
                footerrow: true, 
                loadComplete: function (data) {
                    var $self = $(this), i = getColumnIndexByName($self, 'action');

                    $("tbody > tr.jqgrow > td:nth-child("+(i+1)+") > div:nth-child(1)",$self[0]).click(function(e) {
                        var tr=$(e.target,$self[0].rows).closest("tr.jqgrow");

                        e.preventDefault();
                        window.location.href = 'CenterView.cfm?table=ORDER_JOBS&ID=' + tr[0].id + '&backurl=%3DJobsInbox%2Ecfm%3FPICK%5FDMS%5FCENTERID%3D15';
                    });

                }, 
                colNames: [ "ID", "Delivery Type", "Service Type", "Pre-Commit", "Survey Name", "Client", "Order ID", "Job Number", "Client Reference","Internal Reference", "Type", "Product Name", "Submitted For", "Sales Coordinator", "Order Date", "Requested Date", "Standard Date", "Inbox Date", "Delivery Hold", "Pre-Commit Hold", "Analyst", "&nbsp;", "Actions" ], 
                colModel: [ 
                    { name: "id", index: "id", hidden: true, key: true }, 
                    { name: "delivery_type", index: "delivery_type", width: 70, formatter:'select', stype:'select', edittype:'select', editoptions: { value: ":All;External Delivery:External Delivery;Internal Data Access:Internal Data Access;Internal Delivery:Internal Delivery" } }, 
                    { name: "service_type", index: "service_type", width: 65, searchoptions:{sopt:['cn','eq','bw','bn','nc','ew','en']} }, 
                    { name: "isprecommit", index: "isprecommit", width: 50, align: "center", formatter:'select', stype:'select', edittype:'select', editoptions: { value: ":All;false:No;true:Yes" } }, 
                    { name: "survey_name", index: "survey_name", searchoptions:{sopt:['cn','eq','bw','bn','nc','ew','en']}, formatter:'showlink', formatoptions:{baseLinkUrl:'LinkStation.cfm', addParam: '&Type=JobsInboxSurvey&PICK_DMS_CENTERID=15'} }, 
                    { name: "corpname", index: "corpname", searchoptions:{sopt:['cn','eq','bw','bn','nc','ew','en']}, formatter:'showlink', formatoptions:{baseLinkUrl:'LinkStation.cfm', addParam: '&Type=JobsInboxClient&PICK_DMS_CENTERID=15'} }, 
                    { name: "orderid", index: "orderid", width: 40, formatter:'showlink', formatoptions:{baseLinkUrl:'LinkStation.cfm', addParam: '&Type=JobsInboxOrder&PICK_DMS_CENTERID=15'}, search:false }, 
                    { name: "jobsnumber", index: "jobsnumber", width: 60, searchoptions:{sopt:['cn','eq','bw','bn','nc','ew','en']}, formatter:'showlink', formatoptions:{baseLinkUrl:'CenterView.cfm', addParam: '&table=ORDER_JOBS&backurl=%3DJobsInbox%2Ecfm%3FPICK%5FDMS%5FCENTERID%3D15'} }, 
                    { name: "clientref", index: "clientref", width: 60, searchoptions:{sopt:['cn','eq','bw','bn','nc','ew','en']} }, 
                    { name: "internal_reference", index: "internal_reference", width: 33, formatter:'select', stype:'select', edittype:'select', editoptions: { value: ":;EOM:EOM;NO GO:NO GO" } }, 
                    { name: "survey_type", index: "survey_type", width: 35, align: "center", formatter:'select', stype:'select', edittype:'select', editoptions: { value: ":All;2D:2D;3D:3D" } }, 
                    { name: "product_name", index: "product_name", searchoptions:{sopt:['cn','eq','bw','bn','nc','ew','en']} }, 
                    { name: "submitted_for", index: "submitted_for", width: 70, searchoptions:{sopt:['cn','eq','bw','bn','nc','ew','en']} }, 
                    { name: "sales_coordinator", index: "sales_coordinator", width: 70, searchoptions:{sopt:['cn','eq','bw','bn','nc','ew','en']} }, 
                    { name: "order_date", index: "order_date", width: 50, align: "center", search:false, datefmt: 'mm/dd/yy' }, 
                    { name: "requested_date", index: "requested_date", width: 50, align: "center", search:false, datefmt: 'mm/dd/yy' }, 
                    { name: "dt_standard", index: "dt_standard", width: 50, align: "center", search:false, datefmt: 'mm/dd/yy' }, 
                    { name: "inbox_date", index: "inbox_date", width: 50, align: "center", search:false, datefmt: 'mm/dd/yy' }, 
                    { name: "isdeliveryhold", index: "isdeliveryhold", sortable: true, search: true, width: 50, align: "center", formatter:'select', stype:'select', editable:true, edittype:'select', editoptions: { value: ":;false:No;true:Yes" } }, 
                    { name: "isprecommithold", index: "isprecommithold", sortable: true, search: true, width: 50, align: "center", formatter:'select', stype:'select', editable:true, edittype:'select', editoptions: { value: ":;false:No;true:Yes" } }, 
                    { name: "analyst", index: "analyst", width: 115, align: "center", sortable: true, search: true, formatter:'select', stype:'select', editable:true, edittype:'select', editoptions: { value: ":;michael-19810202:Mike Hares;sara-20061004:Sara Kathleen Lampman" } }, 
                    { name: 'action', index: 'action', sortable: false, search: false, width: 25, formatter: function() { return noteIcon; } }, 
                    { name: "act", index: "act", width: 55, align: "center", sortable: false, search:false, formatter: "actions", 
                            formatoptions: {
                                keys: true, delbutton: false, afterSave: function() { $(this).jqGrid('setGridParam', {datatype:'json'}); $(this).trigger('reloadGrid', [{page:1}]); }, 
                                onEdit:function(rowid) {
                                    var localRowData = $(this).jqGrid('getLocalRow', rowid);

                                    if(localRowData.isprecommit == true) {
                                        // $(this).jqGrid('setColProp', 'isprecommithold', {editable:true});
                                    }
                                    else {
                                        // $(this).jqGrid('setColProp', 'isprecommithold', {editable:false});
                                        $(this).setCell(rowid,"isprecommithold","",'not-editable-cell');
                                    }
                                }
                            }
                    }
                ], 
                editurl: "Components/JobsInbox.cfc?method=SetJobUpdate&UPDATEDBY=aaron-20040630", 
                afterInsertRow: 
                    function(rowid, aData){ 
                        /*
                        if(aData.isprecommit == false) {
                            $(this).setCell(rowid,"isprecommithold","",'not-editable-cell');
                        }
                        */
                        if(aData.isdeliveryhold) {
                            $(this).setCell(rowid,"isdeliveryhold","",{"background":"#FF6A6A"});
                        }
                    }
            }); 

            $('#Jobs').jqGrid({
                url: "Components/JobsInbox.cfc?method=GetJobGrid&PICK_DMS_CENTERID=15&CFDateFormat=MM/DD/YY", 
                caption: "Jobs Inbox", 
                pager: $('#JobsPager')              
            });
            $('#Jobs').jqGrid('filterToolbar', { searchOperators: false, ignoreCase: true });
            $('#Jobs').jqGrid('navGrid', '#JobsPager', { add: false, edit: false, del: false, refresh: false });
            $('#Jobs').jqGrid('navButtonAdd', '#JobsPager', { caption: "", buttonicon: "ui-icon-refresh", title: $.jgrid.nav.refreshtitle, onClickButton: function() { $(this).jqGrid('setGridParam', {datatype:'json'}); $(this).trigger('reloadGrid', [{page:1}]); } });

        });
    </script>

    <style type="text/css">
        .myAltRowClass { background-color: #ECF6FC; background-image: none; }

        /* Wrap in header cell */
        .ui-jqgrid .ui-jqgrid-htable th div {
            height:auto;
            overflow:hidden;
            padding-right:4px;
            padding-top:2px;
            position:relative;
            vertical-align:text-top;
            white-space:normal !important;
        }

        /* Wrap in table cell */
        .ui-jqgrid tr.jqgrow td {
            white-space: normal !important;
            height:auto;
            vertical-align:text-top;
            padding-top:2px;
        }

        span.cellWithoutBackground {
            display:block;
            background-image:none;
            margin-right:-2px;
            margin-left:-2px;
            height:14px;
            padding:4px;
        }
    </style>

1 个答案:

答案 0 :(得分:2)

我通过Prevent editing of a specific editable row's cell in inline-edit mode

的另一个问题找到了基本答案

以下是工作代码:

<script>
    $(document).ready(function() {
        var noteIcon = '<div title="Job Details" style="float:left; margin-left:5px;" class="ui-pg-div ui-inline-edit" onmouseover="jQuery(this).addClass(\'ui-state-hover\');" onmouseout="jQuery(this).removeClass(\'ui-state-hover\');"><span class="ui-icon ui-icon-note"></span></div>';

        getColumnIndexByName = function(grid,columnName) {
            var cm = grid.jqGrid('getGridParam','colModel');
            for (var i=0,l=cm.length; i<l; i++) {
                if (cm[i].name===columnName) {
                    return i; // return the index
                }
            }
            return -1;
        };

        DoDeliveryHolds = function(grid) {
            var aryIDS  = $(grid).jqGrid('getDataIDs');

            if(aryIDS.length > 0) {
                // Add Ajax call to do toggle in database
                alert(aryIDS.length);
            }
        };

        // Here we set the altRows option globallly
        jQuery.extend(jQuery.jgrid.defaults, { 
            altRows:true,
            altclass:'myAltRowClass',
            datatype: "json", 
            jsonReader: {
                root: "ROWS", 
                page: "PAGE", 
                total: "TOTAL", 
                records: "RECORDS", 
                cell: "", 
                id: "0"
            }, 
            emptyrecords: "No jobs to display.",
            rowList: [25,50,100], 
            viewrecords: true, 
            height: "auto", 
            loadonce: true, 
            footerrow: true, 
            loadComplete: function (data) {
                var $self = $(this), i = getColumnIndexByName($self, 'action');

                // nth-child need 1-based index so we use (i+1) below
                $("tbody > tr.jqgrow > td:nth-child("+(i+1)+") > div:nth-child(1)",$self[0]).click(function(e) {
                    var tr=$(e.target,$self[0].rows).closest("tr.jqgrow");

                    e.preventDefault();
                    window.location.href = 'CenterView.cfm?table=ORDER_JOBS&ID=' + tr[0].id + '&backurl=%3DJobsInbox%2Ecfm%3FPICK%5FDMS%5FCENTERID%3D15';
                });

            }, 
            colNames: [ "ID", "Delivery Type", "Service Type", "Pre-Commit", "Survey Name", "Client", "Order ID", "Job Number", "Client Reference","Internal Reference", "Type", "Product Name", "Submitted For", "Sales Coordinator", "Order Date", "Requested Date", "Standard Date", "Inbox Date", "Delivery Hold", "Pre-Commit Hold", "Analyst", "&nbsp;", "Actions" ], 
            colModel: [ 
                { name: "id", index: "id", hidden: true, key: true }, 
                { name: "delivery_type", index: "delivery_type", width: 70, formatter:'select', stype:'select', edittype:'select', editoptions: { value: ":All;External Delivery:External Delivery;Internal Data Access:Internal Data Access;Internal Delivery:Internal Delivery" } }, 
                { name: "service_type", index: "service_type", width: 65, searchoptions:{sopt:['cn','eq','bw','bn','nc','ew','en']} }, 
                { name: "isprecommit", index: "isprecommit", width: 50, align: "center", formatter:'select', stype:'select', edittype:'select', editoptions: { value: ":All;false:No;true:Yes" } }, 
                { name: "survey_name", index: "survey_name", searchoptions:{sopt:['cn','eq','bw','bn','nc','ew','en']}, formatter:'showlink', formatoptions:{baseLinkUrl:'LinkStation.cfm', addParam: '&Type=JobsInboxSurvey&PICK_DMS_CENTERID=15'} }, 
                { name: "corpname", index: "corpname", searchoptions:{sopt:['cn','eq','bw','bn','nc','ew','en']}, formatter:'showlink', formatoptions:{baseLinkUrl:'LinkStation.cfm', addParam: '&Type=JobsInboxClient&PICK_DMS_CENTERID=15'} }, 
                { name: "orderid", index: "orderid", width: 40, formatter:'showlink', formatoptions:{baseLinkUrl:'LinkStation.cfm', addParam: '&Type=JobsInboxOrder&PICK_DMS_CENTERID=15'}, search:false }, 
                { name: "jobsnumber", index: "jobsnumber", width: 60, searchoptions:{sopt:['cn','eq','bw','bn','nc','ew','en']}, formatter:'showlink', formatoptions:{baseLinkUrl:'CenterView.cfm', addParam: '&table=ORDER_JOBS&backurl=%3DJobsInbox%2Ecfm%3FPICK%5FDMS%5FCENTERID%3D15'} }, 
                { name: "clientref", index: "clientref", width: 60, searchoptions:{sopt:['cn','eq','bw','bn','nc','ew','en']} }, 
                { name: "internal_reference", index: "internal_reference", width: 33, formatter:'select', stype:'select', edittype:'select', editoptions: { value: ":;EOM:EOM;NO GO:NO GO" } }, 
                { name: "survey_type", index: "survey_type", width: 35, align: "center", formatter:'select', stype:'select', edittype:'select', editoptions: { value: ":All;2D:2D;3D:3D" } }, 
                { name: "product_name", index: "product_name", searchoptions:{sopt:['cn','eq','bw','bn','nc','ew','en']} }, 
                { name: "submitted_for", index: "submitted_for", width: 70, searchoptions:{sopt:['cn','eq','bw','bn','nc','ew','en']} }, 
                { name: "sales_coordinator", index: "sales_coordinator", width: 70, searchoptions:{sopt:['cn','eq','bw','bn','nc','ew','en']} }, 
                { name: "order_date", index: "order_date", width: 50, align: "center", search:false, datefmt: 'mm/dd/yy' }, 
                { name: "requested_date", index: "requested_date", width: 50, align: "center", search:false, datefmt: 'mm/dd/yy' }, 
                { name: "dt_standard", index: "dt_standard", width: 50, align: "center", search:false, datefmt: 'mm/dd/yy' }, 
                { name: "inbox_date", index: "inbox_date", width: 50, align: "center", search:false, datefmt: 'mm/dd/yy' }, 
                { name: "isdeliveryhold", index: "isdeliveryhold", sortable: true, search: true, width: 50, align: "center", formatter:'select', stype:'select', editable:true, edittype:'select', editoptions: { value: ":;false:No;true:Yes" } }, 
                { name: "isprecommithold", index: "isprecommithold", sortable: true, search: true, width: 50, align: "center", formatter:'select', stype:'select', editable:true, edittype:'select', editoptions: { value: ":;false:No;true:Yes" } }, 
                { name: "analyst", index: "analyst", width: 115, align: "center", sortable: true, search: true, formatter:'select', stype:'select', editable:true, edittype:'select', editoptions: { value: ":;michael-19810202:Mike Hares;sara-20061004:Sara Kathleen Lampman" } }, 
                { name: 'action', index: 'action', sortable: false, search: false, width: 25, formatter: function() { return noteIcon; } }, 
                { name: "act", index: "act", width: 55, align: "center", sortable: false, search:false, formatter: "actions", 
                        formatoptions: {
                            keys: true, delbutton: false, afterSave: function() { $(this).jqGrid('setGridParam', {datatype:'json'}); $(this).trigger('reloadGrid', [{page:1}]); }
                        }
                }
            ], 
            editurl: "Components/JobsInbox.cfc?method=SetJobUpdate&UPDATEDBY=aaron-20040630", 
            afterInsertRow: 
                function(rowid, aData){ 
                    if(aData.isdeliveryhold) {
                        $(this).setCell(rowid,"isdeliveryhold","",{"background":"#FF6A6A"});
                    }
                }
        }); 

        $('#Jobs').jqGrid({
            url: "Components/JobsInbox.cfc?method=GetJobGrid&PICK_DMS_CENTERID=15&CFDateFormat=MM/DD/YY", 
            caption: "Jobs Inbox", 
            pager: $('#JobsPager')              
        });
        $('#Jobs').jqGrid('filterToolbar', { searchOperators: false, ignoreCase: true });
        $('#Jobs').jqGrid('navGrid', '#JobsPager', { add: false, edit: false, del: false, refresh: false });
        $('#Jobs').jqGrid('navButtonAdd', '#JobsPager', { caption: "", buttonicon: "ui-icon-refresh", title: $.jgrid.nav.refreshtitle, onClickButton: function() { $(this).jqGrid('setGridParam', {datatype:'json'}); $(this).trigger('reloadGrid', [{page:1}]); } });
        $('#Jobs').jqGrid('navButtonAdd', '#JobsPager', { caption: "Toggle Delivery Holds", buttonicon: "ui-icon-pencil", onClickButton: function() { var str = confirm("Toggle the Delivery Hold for all displayed Jobs?"); if(str) { DoDeliveryHolds($(this)); } } });

        var orgRowActions = $.fn.fmatter.rowactions;
        $.fn.fmatter.rowactions = function (act) {
            var $tr = $(this).closest("tr.jqgrow"),
                rid = $tr.attr("id"),
                $grid = $(this).closest("table.ui-jqgrid-btable"),
                rowData = $grid.jqGrid("getLocalRow", rid),
                isNonEditable = false,
                result; 

            // editable property of any column
            if (act === "edit" && rowData.isprecommit == false) {
                $grid.jqGrid("setColProp", "isprecommithold", {editable: false});
                isNonEditable = true;
            }
            result = orgRowActions.call(this, act);
            if (isNonEditable) {
                // reset the setting to original state
                $grid.jqGrid("setColProp", "isprecommithold", {editable: true});
            }
            return result;
        }

    });
</script>

<style type="text/css">
    .myAltRowClass { background-color: #ECF6FC; background-image: none; }

    /* Wrap in header cell */
    .ui-jqgrid .ui-jqgrid-htable th div {
        height:auto;
        overflow:hidden;
        padding-right:4px;
        padding-top:2px;
        position:relative;
        vertical-align:text-top;
        white-space:normal !important;
    }

    /* Wrap in table cell */
    .ui-jqgrid tr.jqgrow td {
        white-space: normal !important;
        height:auto;
        vertical-align:text-top;
        padding-top:2px;
    }

    span.cellWithoutBackground {
        display:block;
        background-image:none;
        margin-right:-2px;
        margin-left:-2px;
        height:14px;
        padding:4px;
    }
</style>