0

I have a jQGrid with three columns. First one is a dropdown (type: select) field, second one is custom type for Check box list and the third one is text field. The requirement is when the user change the value of the dropdown field (DocGroupname), then the DocList field should be empty. So we need to set DocList column's value empty when user change the value of DocGroupName column.

I tried like following. While changing the dropdown value, called a function named myfunction.Inside the function I set the value for DocList column is null. But its not working, So what i did wrong? and please anybody help me to find the solution.

JQGrid Code,

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

}

myfunction Code,

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

 function nullFormatter()
 {
    return '';
 }
Md Aslam
  • 1,228
  • 8
  • 27
  • 61

1 Answers1

1

What You need to do is change the cell value on beforeSaveCell function Here is a solution I created for you on 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;

                },

 });
Helen Araya
  • 1,886
  • 3
  • 28
  • 54
  • @ Amete Blessed: I tried it, If i changed the value, then the DocList getting empty, but I can not enter any value in DocList when I edit the DocList field after changed the DocGroupname field. What to do?? – Md Aslam May 13 '16 at 06:52
  • @MdAslam I fixed the issue and the jsfiddle. I changed afterSaveCell to have a check on which column is being line edited. – Helen Araya May 13 '16 at 14:34
  • @ Amete Yes, you gave a solution. Really, that's helpful to me, but I can not use it in my application because this is not a proper solution. I mean we cannot edit the field again after empty the field. Please check your code in jsfiddle, it is not working in jsfiddle also. Say an example ( Change the 'DocGroup' in jsfiddle and click on 'Docname'. The Document name getting empty, this is fine, but edit the document name and click anywhere you cannot keep the selected value why ? ) Plz check it in jsfiddle – Md Aslam May 17 '16 at 04:19
  • Which selected value are you talking about? And when you say click anywhere what does that mean? I changed the doc group to london and the doc name was empty and then i changed the doc name to xxx and then hit enter and both are london and xxx which is as expected. – Helen Araya May 17 '16 at 18:56
  • Click anywhere means.. After changing the DocGroup only the DocName should be empty only one time. But its getting empty whenever click anywhere after changing the docGroup. So i want to do empty only once after changing the DocGroup because after changing the DocGroup user must change the DocName also. So they will change the DocName, at that time the Docname should not be empty, Because it has chosen based on 'changed the DocGroup' right? Then why I want it to set empty again and again. So please help me once again , and i will tick your answer is correct. – Md Aslam May 19 '16 at 09:26