【问题标题】:JQgrid, Set a Cell value empty in change event of another cellJQgrid,在另一个单元格的更改事件中将单元格值设置为空
【发布时间】:2016-09-08 00:39:43
【问题描述】:

我有一个三列的 jQGrid。第一个是下拉(类型:选择)字段,第二个是复选框列表的自定义类型,第三个是文本字段。要求是当用户更改下拉字段(DocGroupname)的值时,DocList 字段应为空。所以当用户更改 DocGroupName 列的值时,我们需要将 DocList 列的值设置为空。

我尝试如下。在更改下拉值时,调用了一个名为 myfunction 的函数。在函数内部,我将 DocList 列的值设置为空。但它不起作用,那么我做错了什么?并请任何人帮助我找到解决方案。

JQGrid 代码,

function ReqKYCGrid()
{

$("#divmsgg").show();

                 var DocGroupName = $.ajax
                ({
                    type: 'GET',
                    async: false,
                    url: RootUrl + "EXEntityTypeKYC/GetDocumentGroupList",
                    cache: true,
                    contentType: 'application/json; charset=utf-8',
                    data: { "projectSeq":$("#Id").val(),"StartDate":$("#StartDate").val(),"EndDate":$("#EndDate").val() },
                    success: function (result) {
                        if (!result) alert('No Document Group Found !!');
                    },
                    error: function (error) {
                        alert('Error' + error);
                    }
                }).responseText; 


       jQuery("#grdForReqKYC").jqGrid({
         url: RootUrl+ 'EXEntityTypeKYC/ReqKYCGridData',
        datatype: 'json',
        mtype: 'POST',
        colNames: ['id', 'Document Group Name','Document Name','No of Mandatory'],
        colModel: [
                    { name: 'id', index: 'id', width: 30, sorttype: "int", editable: false,hidden:true },                       
                    { name: 'DocGroupName', index: 'DocGroupName', width: 310, editable: true, edittype: "select", editrules: { required: true} , 
                     editoptions: { 

                         dataInit: function (element) {
                                        $(element).focusout(function (e) {
                                        if(this.value!="")
                                        {

                                        }
                                        });
                                    },

                         dataEvents: [{type: 'change', fn: function(e){
                            myfunction(e);
                            }
                         }]}

                     },

                    { name: 'DocList', index: 'DocList', width: 310, hidden:false,editable: true,edittype: "custom", editoptions: {  dataInit: function (element) {

                                        $(element).click(function (e) {

                                        });
                                    },
                     custom_element:MultiCheckElem, custom_value:MultiCheckVal  }},
                     { name: 'Mandatory', index: 'Mandatory', width: 310, editable: true, edittype: "text", editoptions: { maxlength: "1" , dataInit: function (element) {
                                $(element).keydown(function (e,val) {
                                 var ctrl = e.ctrlKey;
                                    var key = e.charCode || e.keyCode || 0;
                                    if (ctrl && ((key == 88) /* CUT */ || (key == 67) /* COPY */ || (key == 86) /* PASTE */ )) { return false;
                                    }
                                    if (e.which != 8 && e.which != 0 && e.which != 46 && (e.which < 48 || e.which > 57)) {
                                        return false;
                                    }

                                    var rowDataObj=$('#grdForReqKYC').jqGrid('getRowData',cellRowId);
                                    var rowData =JSON.parse(JSON.stringify(rowDataObj)); 
                                    var docListValue=rowData["DocList"].split(',');

                                    if(((docListValue.length -1)==0 )  && rowData["DocList"]=='')
                                    {
                                        alert("Please Choose Document Name");
                                        return false;
                                    }

                                    if(e["char"]> (docListValue.length ))
                                    {
                                     return false;
                                    }




                                });
                            }}, editrules: { required: true} }
                    //, list:'Check1,Check2,Check3,Check4'
                 ],  

         loadComplete: function () {      
                if(DocGroupName!=null )
                        $("#grdForReqKYC").setColProp('DocGroupName', { editoptions: { value: JSON.parse(DocGroupName)} });

                 //if(DocumentName!=null )
                 //alert(DocumentName+ ' ---- '+ JSON.parse(DocumentName));
                       // $("#grdForReqKYC").setColProp('DocumentName', { editoptions: { value: JSON.parse(DocumentName)} });
                        //$("#grdForReqKYC").setColProp('DocList', { editoptions: { list: CheckedDocumentName} });


        },

        cellEdit: true,
        rowNum: 1000,
        //gridview: true,
        rownumbers: true,
        //autoencode: true,           
        height: 120,
         width: 700,
        cellsubmit: 'clientArray',
        caption: "Required Document for KYC",
        multiselect: true,
        // onSelectRow: function(id){alert(id)},

        postData: {
            "MSTSeq": $("#Id").val(),
            "data": $("#KYCGrid").val()
        },
        onCellSelect: function (rowid, iCol, cellcontent, e) {
         //alert(rowid);
         cellRowId=rowid;
        }
    });


    //End Of Grid

      $(window).on("resize", function () {
        var newWidth = $("#grdForReqKYC").closest(".ui-jqgrid").parent().width();
        if(newWidth>900)
        {
        jQuery("#grdForReqKYC").jqGrid("setGridWidth", 900, true);

        }
        else{
         jQuery("#grdForReqKYC").jqGrid("setGridWidth", newWidth, true);
        }
    });

    // Start of AddNew/Delete function for Grid
   $("#btnAddNew").click(function () {

        if (ValidateRow($("#grdForReqKYC"))) {
            var idAddRow = $("#grdForReqKYC").getGridParam("reccount")
            emptyItem = [{ id: idAddRow + 1, FinancialYearNames: "", EntityName: "", AllocatedValue: "", ProjectDesc: ""}];
            jQuery("#grdForReqKYC").jqGrid('addRowData', 0, emptyItem);
        }
    });


    $("#btnDelete").click(function () {
         $("#grdForReqKYC").jqGrid("editCell", 0, 0, false);
        var gr = jQuery("#grdForReqKYC").jqGrid('getGridParam', 'selarrrow');
        if (gr != "") {
            for (var i = 0; i <= gr.length; i++) {
                jQuery("#grdForReqKYC").jqGrid('delRowData', gr[i]);
            }
            for (var i = 0; i <= gr.length; i++) {
                jQuery("#grdForReqKYC").jqGrid('delRowData', gr[i]);
            }
        }
        else
            alert("Please Select Row(s) to delete!");
    });

    // End of AddNew/Delete function for Grid

}

我的功能代码,

function myfunction(e)
{ 
  // grid.jqGrid('setCell', 1, 'DocList', '', null, null, true);
   $("#grdForReqKYC").setColProp('DocList', { editoptions: { formatter: nullFormatter} });
 }

 function nullFormatter()
 {
    return '';
 }

【问题讨论】:

    标签: jquery jqgrid onchange formatter mvcjqgrid


    【解决方案1】:

    您需要做的是更改 beforeSaveCell 函数上的单元格值 Here 是我在 jsfiddle 上为您创建的解决方案。

    var currentRow = 0;  
    
     function getAllSelectOptions(){
         var states = { '1': 'Alabama', '2': 'California', '3': 'Florida', 
                       '4': 'Hawaii', '5': 'London', '6': 'Oxford' };
    
          return states;
    
        }
                validateText = function (value, colname) {
             // do validations if any and return false               
                  return [true];
                };
    
                "use strict";
                var mydata = [
                        {id:"1", DocGroupName: "2", DocList: "y", Mandatory: "z"},
                        {id:"2", DocGroupName: "6", DocList: "y", Mandatory: "z"},
                    ];
                $("#list").jqGrid({
    
    
                    //url:'php.scripts/customers.get.php',
                    //datatype: 'xml',
                    //mtype: 'POST',
                    datatype: "local",
                    data: mydata,
                    height: "auto",
                  colNames: ['id', 'Document Group Name','Document Name','No of Mandatory'],
                  colModel :[
                        {name:'id', index:'id', width:55},
                        {name:'DocGroupName', index:'DocGroupName', width:90, editable: true,edittype: 'select', 
                                                formatter: 'select',
    
                                                editoptions:{value: getAllSelectOptions()}, "editrules":{"custom":true,"custom_func":validateText}},
                        {name:'DocList', index:'DocList', width:90, editable: true },
                        {name:'Mandatory', index:'Mandatory', width:90, editable: true}
    
                    ],
                    pager: '#pager',
                    rowNum:10,
                    rowList:[10,20,30],
                    sortname: 'idcustomers',
                    sortorder: 'asc',
                    viewrecords: true,
                    gridview: true,
                    caption: 'Customers',
                    cellEdit: true,
                    cellsubmit: 'clientArray',
                    afterSaveCell: function(rowid,name,val,iRow,iCol) {
                    if(name=='DocGroupName')
                     {
                         var row = $('#list').jqGrid('getRowData',currentRow);      
                         row.DocList='';
                         $('#list').jqGrid('setRowData',currentRow,row);       
                     }                  
                    },
                    beforeSaveCell: function(rowid,name,val,iRow,iCol) {
                     // var row = $("#list").getRowData(rowid);
                       var row = $('#list').jqGrid('getRowData',rowid);
                       currentRow= rowid;
    
                    },
    
     });
    

    【讨论】:

    • @Amete Blessed:我试过了,如果我更改了值,那么 DocList 会变空,但是当我在更改 DocGroupname 字段后编辑 DocList 字段时,我无法在 DocList 中输入任何值。怎么办??
    • @MdAslam 我修复了问题和 jsfiddle。我更改了 afterSaveCell 以检查正在编辑哪一列。
    • @Amote 是的,你给出了解决方案。真的,这对我很有帮助,但我不能在我的应用程序中使用它,因为这不是一个合适的解决方案。我的意思是我们在清空该字段后无法再次编辑该字段。请检查您在 jsfiddle 中的代码,它在 jsfiddle 中也不起作用。举个例子(更改 jsfiddle 中的“DocGroup”并单击“Docname”。文档名称变空,这很好,但编辑文档名称并单击任何无法保留所选值的位置,为什么?)请在 jsfiddle 中检查
    • 您说的是哪个选定值?当您说单击任意位置时,这是什么意思?我将文档组更改为 london,文档名称为空,然后将文档名称更改为 xxx,然后按回车键,两者都是 london 和 xxx,这是预期的。
    • 单击任意位置意味着.. 更改 DocGroup 后,只有一次 DocName 应该为空。但是在更改 docGroup 后,无论何时单击任何位置,它都会变空。所以我想在更改 DocGroup 后只做一次空,因为在更改 DocGroup 用户之后也必须更改 DocName。所以他们会更改DocName,那时Docname不应该为空,因为它已经选择了'changed the DocGroup'对吗?那为什么我希望它一次又一次地设置为空。所以请再次帮助我,我会勾选你的答案是正确的。