1

I want to check before showing add form in jqgrid that variable myVar has value. Following is my code in add option to check whether myVar has value or not. If myVar is null then I don't want add form to be open.

}).navGrid('#mypager',{cloneToTop:true, edit:false,add:true,del:false,view:false,search: false,refresh:true},
    {},
    {
    beforeShowForm : function (formid)
    {
        if(myVar.length==0)
        {
            alert("Value can't be blank!");
            return[false,"Value can't be blank!"];
        }
    },
    recreateForm: true,
    reloadAfterSubmit:true,
    closeOnEscape:true, 
    modal:true,
    jqModal: false,
    savekey: [true,13],
    width:550,
    mtype:'POST',
    url: 'MyServlet',
    editData:{action:'ListInsert',myVar: function () {return myVar;}},
    afterSubmit: function (response)
    {
        var myInfo = '<div class="ui-state-highlight ui-corner-all">'+'<span class="ui-icon ui-icon-info" '+'style="float: left; margin-right:.3em;"></span>'+ response.responseText +'</div>';
        $infoTr = $("#TblGrid_" + $.jgrid.jqID(this.id) + ">tbody>tr.tinfo"),$infoTd = $infoTr.children("td.topinfo"); 
        $infoTd.html(myInfo);
        $infoTr.show();
        return [true, "", ""];                  
    },
    errorTextFormat: function (response)
    {
        return '<span class="ui-icon ui-icon-alert" ' +'style="float:left; margin-right:.3em;"></span>' +response.responseText;
    }
},

Above code shows the alert, but still shows the add form.
myVar contains id from other grid and If myVar doesn't have value then I don't want to show add form.

Thanks in advance.

Bhushan
  • 6,151
  • 13
  • 58
  • 91
  • Could you provide more code? Would be easier to tell whats going on with your form – Camathon Oct 28 '13 at 08:47
  • @Cammy I have added more code to the question. Let me know if you need more information. – Bhushan Oct 28 '13 at 09:56
  • I didnt realized you were using a framework. But I checked the documentation and didnt find anything that says you can abort the showing of the rest of the form in the 'beforeShowForm'. Or that it can have a return value -> return[false,"Value can't be blank!"]; maybe reconsider this line – Camathon Oct 28 '13 at 10:45
  • You could close the form yourself: `if(!myVar){$('#btCancel').click(); alert('...')}` where btCancel is the cancel button of the form. – Rudy Oct 28 '13 at 11:49

1 Answers1

1

One can't deny opening of Add/Edit form by returning some value from beforeShowForm. What you can do is closing of the form immediately after opening. You can use afterShowForm for example. The code could be something like

afterShowForm: function () {
    var idSelector = $.jgrid.jqID(this.p.id);
    if(myVar.length==0) {
        $.jgrid.hideModal("#editmod" + idSelector, {gbox: "#gbox_" + idSelector});
        alert("Value can't be blank!");
    }
}

See the demo here.

Oleg
  • 220,925
  • 34
  • 403
  • 798