Shell
Shell

Reputation: 6849

jqGrid add button in Navigation Toolbar does not work

I am trying to create a grid that allows user to add, edit and delete the record. I have done working for populating grid now I am going to display dialog for add record. But, I don't why it doesn't work. Dialog does not getting displayed.

This is the snapshot of my grid.

enter image description here

and this is my js function

InfoDesk.GridManager.postsGrid = function (gridName, pagerName) {
    //Create the grid
    $(gridName).jqGrid({
        //server url and other ajax stuff
        url: '/Admin/Posts',
        datatype: 'json',
        mtype: 'GET',

        height: 'auto',

        //Columns
        colNames: colNames,
        colModel: columns,

        //pagination options
        toppager: true,
        pager: pagerName,
        rowNum: 10,
        rowList: [10, 20, 30],

        //row number columns
        rownumbers: true,
        rownumWidth: 40,

        //default sorting
        sortname: 'PostedOn',
        sortorder: 'desc',

        //display the no. of records message
        viewrecords: true,

        jsonReader: { repeatitems: false },

        afterInsertRow: function (rowid, rowdata, rowelem) {
            var tags = rowdata["Tags"];
            var tagStr = "";

            $.each(tags, function (i, t) {
                if (tagStr) tagStr += ", "
                tagStr += t.Name;
            });


            $(gridName).setRowData(rowid, { "Tags": tagStr });

        }


    });

    $(gridName).navGrid(pagerName,
                     {
                         cloneToTop: true,
                         search: false,
                         add:true
                     }, editOptions, addOptions, deleteOptions);                
    };

When I click on add button. Nothing is happening. This is my first project with jqGrid. So, I am completely blank about it.

EDITED:

I have found a fiddle example and modified for navigation bar. It is working fine but, still I could not recognize the issue where i am doing mistake in my code.

fiddle

Upvotes: 4

Views: 2971

Answers (1)

Oleg
Oleg

Reputation: 221997

It seems to me that your main goal to understand how you can correctly use navGrid with top and bottom pagers. So I'll explain exactly how all works.

It's not clear whether you need to add the navigator icons on the top pager or on the both top and bottom pager. It's unclear whether you use the bottom pager at all.

jqGrid have two main option which specify the page: pager and toppager options. To use pager option you need create <div> which have id attribute, place the div on the page somewhere and to use the id selector or the id name as the value of pager option. For example you can place <div id="mypager"></div> and to use pager: "#mypager" as jqGrid option. If you would use the value of pager option in another supported form: pager: "mypager" or pager: $("#mypager") then jqGrid will normalize the option to id selector. If you would use var thePager = $(gridName).jqGrid("getGridParam", "pager"); to get the value of "pager" option directly after the grid is created you will get thePager === "#mypager" independent from the way in which you used input pager parameter.

The option toppager works a little in another way. You should use toppager: true to create the top pager. In the case jqGrid create the corresponding <div> itself. The id of the div will be constructed from the id of the grid and the string _toppager. So in case of having gridName equal to #mygrid you would have the top pager with id="mygrid_toppager". If you would get the value of toppager option after the grid is created

var theTopPager = $(gridName).jqGrid("getGridParam", "pager");

you will get back the id selector of the top pager instead of true: theTopPager will be equal to "#mygrid_toppager" (gridName + "_toppager").

The value of the first parameter of navGrid should be the id selector of the pager on which you want to place the navigator buttons. So it should be $(gridName).navGrid(gridName + "_toppager", ...); to add the buttons on the top pager and $(gridName).navGrid(pagerName, ...); if you want to add buttons on the bottom pager. The usage $("#grid").navGrid('setGridParam', ... like you do in jsfiddle demo is wrong because 'setGridParam' is not the id selector of any pager.

In case if you have two pagers (at the bottom and at the top of the grid) you can use pager selector as the first parameter of navGrid and to use additional option cloneToTop: true. By the way the method navButtonAdd which can be used to add custom button don't have any cloneToTop: true option and you would have to specify the id selector of the pager directly.

So if you need create the grid with one top pager only you can remove unneeded jqGrid option pager: pagerName and to use the following code to create the navigator bar with Add, Edit, Delete and Refresh buttons:

$(gridName).navGrid($(gridName).getGridParam("toppager"), { search: false });

If you would like to create grid on both top and bottom pager then you have to use both options of jqGrid toppager: true and pager: pagerName and can use either

$(gridName).navGrid($(gridName).getGridParam("pager"), { search: false });
$(gridName).navGrid($(gridName).getGridParam("toppager"), { search: false });

or the short form

$(gridName).navGrid($(gridName).getGridParam("pager"),
    { search: false, cloneToTop: true });

By the way I use $(gridName).getGridParam("pager") instead of pagerName as the parameter of navGrid because I'm not sure whether you use id name (like "mypager") or id selector (like "#mypager") as the parameter of InfoDesk.GridManager.postsGrid. The method navGrid accept only the id selector.

If you would need to specify additional parameters of form editing methods used during Add, Edit or Delete operation you should specify additional optional parameters editOptions, addOptions, deleteOptions, searchOptions, viewOptions (see the documentation). You should of cause define the objects editOptions, addOptions, deleteOptions, searchOptions, viewOptions before the usage. The current code which you posted don't contains the definition of the option.

UPDATED: By the way I implemented some new features in the fork of jqGrid which I publish on GitHub here. 1) One can now use pager: true option like toppager: true. 2) one can use navGrid without the pager (like $(gridName).navGrid({search: false});). In the case jqGrid will add the buttons on all pagers which jqGrid have. 3) I added new option navGrid: iconsOverText:true which allows another look of navigator buttons with texts (see the demo). 4) the navigator buttons will be wrapped on the next row of pager automatically if too many icons is added and the grid have not so large width. You can see more demos of the features at the bottom of readme on the page.

Upvotes: 4

Related Questions