Preventing the opening of a form on a add button c

2019-02-20 16:06发布

问题:

Did you guys know how to prevent the open of a Form when I click on a add button?

Maybe using beforeShowForm?

function(formid)
{
    if(jQuery('#gridap').getGridParam('selrow'))
    {

        idgridap=jQuery('#gridap').getGridParam('selrow');
        jQuery('#FK_numerocontrato_ap',formid).val(idgridap).attr('readonly','readonly');

    }
    else 
    {
         // I want to prevent the openning of the add form here and maybe show an alert using the "alertcap"

    }
}
CHECAROW;

$grid->setNavEvent('add','beforeShowForm',$checarowid);

BTW, there's a way to call the alertmod of jqgrid and add a custom message to it?

tks!

回答1:

I don't understand why you not just remove "Add" button from the navigation bar. To create a navigation bar you explicitly call navGrid method of jqGrid

jQuery("#grid_id").navGrid('#gridpager'); 

or

jQuery("#grid_id").jqGrid('navGrid', '#gridpager');

but navGrid has additional parameters (see http://www.trirand.com/jqgridwiki/doku.php?id=wiki:navigator). So if you use

jQuery("#grid_id").navGrid('#gridpager', {add: false}); 

you will be have no "Add" button.

If you do need have "Add" button, please explain your situation more clear. By the way with the way described in http://www.trirand.com/jqgridwiki/doku.php?id=wiki:custom_buttons you can add an custom button with full control from your side. The name of icon you can find on the page http://jqueryui.com/themeroller/ if you place a cursor over the icon on the "Framework Icons" area on the bottom of the page. The custom button can have the same icon as the "Add" button has. Can it solve your problem?

UPDATED: Now after your comment I understand your problem. I can suggest to use addfunc option of navGrid (see http://www.trirand.com/jqgridwiki/doku.php?id=wiki:navigator&s[]=navgrid). So the code could looks like following:

var grid = jQuery("#grid_id").navGrid('#gridpager', {addfunc: function() {
    var sel_id = grid.getGridParam('selrow');
    if (sel_id) {
        grid.editGridRow("new", pAddOption);
    } else {
        viewModal("#alertmod", { gbox: "#gbox_" + grid_id, jqm: true });
        jQuery("#jqg_alrt").focus();
    }
}});

In this example will be allowed to click "Add" button only if a row is selected. You will see a message box with the text like "Please, select row" (the text which defines $.jgrid.nav.alerttext inside of grid.locale-en.js or other localization file which you use). You can place this code fragment in your master grid.

The code in case of denying of "Add" operation can be easier, I just copied here a code fragment which use jqGrid itself. You can display your custom error message instead.



回答2:

Cool, tks Oleg!!! BTW, I came with another(but not beautifull) solution:

** This is a aftershowform action. If we do not have a selected row on the main grid(#gridap), we hide the form modal with jqmHide(). Then, I use your solution to show the alertcap.


$closeform = <<< CLOSEF
function(formid)
{
    if(!jQuery('#gridap').getGridParam('selrow'))
    {
        $('#editmodgridbal').jqmHide();
        viewModal('#alertmod', { gbox: '#gbox_', jqm: true });
    }
}
CLOSEF;

$grid->setNavEvent('add','afterShowForm',$closeform);