Search code examples
javascriptjquerycssjqgridfree-jqgrid

How to place select element and toggle button to free jqgrid toolbar


I'm looking for a way to place select element and toggle button to free jqgrid top toolbar if font awesome icon set is used. I tried code which worked in 4.6:

    var i,
        selectElem= '<select tabindex="-1" id="_layout">';

    for (i=0; i<10; i++) {
        selectElem += '<option value="'+i+'" ';
        if (i==layout)
            selectElem += ' selected';
        selectElem += '>Form ' + i + '</option>'
    }

    $("#grid_toppager_left table.navtable tbody tr").append(
           '<td class="ui-pg-button ui-corner-all">' +
           '<div class="ui-pg-div my-nav-checkbox">' +
           selectElem +
           '</select>' +
           '</div></td>'
           );

but select element does not appear.

For toggle button I tried

    $("#grid_toppager_left table.navtable tbody tr").append(
           '<td class="ui-pg-button ui-corner-all">' +
               '<div class="ui-pg-div my-nav-checkbox">' +
               '<input tabindex="-1" type="checkbox" id="AutoEdit" ' + (autoedit ? 'checked ' : '')+'/>' +
               '<label ' +' for="AutoEdit">Toggle</label></div></td>'
                      );
    $("#AutoEdit").button({
        text: false,
        icons: {primary: "fa-star"}
    }).click(function () {
            autoedit = !autoedit;
        }
    });

but toggle button also does not appear in toolbar. How to force those elements to appear ?


Solution

  • You should change because .navtable is now not table. See the wiki article for more details. So the code should be something like the following:

    $("#grid_toppager_left .navtable").append(
     '<div class="ui-pg-button ui-corner-all">' +
         '<div class="ui-pg-div my-nav-checkbox">' +
         '<input tabindex="-1" type="checkbox" id="AutoEdit" ' + (autoedit ? 'checked ' : '')+'/>' +
         '<label ' +' for="AutoEdit">Toggle</label></div></div>'
                );
    $("#AutoEdit").button({
        text: false,
        icons: {primary: "fa fa-lg fa-fw fa-star"}
    }).click(function () {
            autoedit = !autoedit;
        }
    );
    var $label = $("#AutoEdit").next("label");
    $label.children(".ui-button-icon-primary").removeClass("ui-icon");
    $label.find(".ui-button-text").hide();
    

    Additionally I added small CSS fixes

    .my-nav-checkbox > .ui-button-icon-only { margin-top: 5px; }
    .my-nav-checkbox > .ui-button-icon-only > .ui-button-icon-primary.fa { margin-left: 6px; }
    .my-nav-checkbox:hover  { margin: 1px;  }
    

    The demo shows the results:

    enter image description here

    UPDATED: I though more about your question and I can suggest better solution of the same problem. I think that it would be easier don't use jQuery UI Button at all. instead of that one can mark the button as "checked" just by toggling ui-state-active class. So I suggest to add the CSS rule

    .ui-jqgrid .ui-pg-table .ui-pg-button.ui-state-active { margin: 1px; }
    

    first of all. The implementation of the checked button one can make by using navButtonAdd method:

    var autoedit = false;
    $grid.jqGrid("navButtonAdd", "#grid_toppager", {
        buttonicon: "fa-star",
        caption: "",
        id: "AutoEdit",
        title: "Toggle autoedit",
        onClickButton: function (options, e) {
            autoedit = !autoedit;
            //$("#"+options.id)[autoedit ? "addClass" : "removeClass"]("ui-state-active");
            $(e.currentTarget)[autoedit ? "addClass" : "removeClass"]("ui-state-active");
        }
    });
    

    The corresponding demo seems to work without any side effects and be very simple. The displayed results of "checked" button look like on the picture below:

    enter image description here