3

I'm using latest free-jqgrid 4.9.2

My toolbar(toppager) buttons goes to next line though loads of space there in grid width. Please find attached screen shot. Any reason ?

I tried the below CSS code as well, but no luck;

  /*Toolbar customization*/
    .ui-jqgrid .ui-jqgrid-toppager {    
        width: auto !important;/*1200 px*/
    }

Full jqGrid script -

$(document).ready(
  function() {

    /**
     * User code tab
     */
    var userGrid = $("#userGrid"),
      oldFrom = $.jgrid.from,
      lastSelected;
    var oldFrom = $.jgrid.from;
    var lastSelected;

    $.jgrid.from = function(source, initalQuery) {
      var result = oldFrom.call(this, source, initalQuery),
        old_select = result.select;
      result.select = function(f) {
        lastSelected = old_select.call(this, f);
        return lastSelected;
      };
      return result;
    };

    $("#userGrid").jqGrid({
      //sortable: true,//Columns reordering need to be disabled for frozen column working
      //scroll: 1,//true scrolling rows - paging is not working when we enable scrolling. give fixed height then this will go good.. :)
      url: 'list_user_detail',
      datatype: 'json',
      colNames: [jQuery.i18n.prop('userdetail.table.column.id'),
        jQuery.i18n.prop('userdetail.table.column.username'),
        jQuery.i18n.prop('userdetail.table.column.password'),
        jQuery.i18n.prop('userdetail.table.column.active'),
        jQuery.i18n.prop('userdetail.table.column.firstname'),
        jQuery.i18n.prop('userdetail.table.column.lastname'),
        jQuery.i18n.prop('userdetail.table.column.department'),
        jQuery.i18n.prop('userdetail.table.column.email'),
        jQuery.i18n.prop('userdetail.table.column.dob'),
        jQuery.i18n.prop('userdetail.table.column.country'),
        jQuery.i18n.prop('userdetail.table.column.role'),
        ""
      ],
      colModel: [{
        name: 'id',
        index: 'id',
        width: 80,
        editable: true,
        sortable: true,
        align: 'center',
        sorttype: 'integer',
        searchoptions: {
          sopt: ['eq', 'ne', 'le', 'lt', 'gt', 'ge'] //for filter functionality - specify the search options
        },
        editoptions: {
          readonly: true,
          size: 5
        },
        formoptions: {
          rowpos: 1,
          colpos: 1
        }
      }, {
        name: "username",
        index: "username",
        width: 120,
        align: 'center',
        sortable: true,
        editable: true,
        searchoptions: {
          sopt: ['cn', 'eq', 'ne', 'bw', 'bn', 'nc', 'ew', 'en']
        }, //for Search/Filter functionality - specify the search options
        frozen: true,
        editoptions: {
          size: 30
        },
        editrules: {
          required: true,
          custom: true,
          custom_func: usernameCheck
        },
        formoptions: {
          label: jQuery.i18n.prop('userdetail.table.form.label.username')
        }
      }, {
        name: "password",
        index: "password",
        width: 100,
        align: 'center',
        sortable: false,
        editable: true,
        hidden: true,
        hidedlg: true, //to remove this column from column chooser as well
        searchoptions: {
          sopt: ['cn', 'eq', 'ne', 'bw', 'bn', 'nc', 'ew', 'en']
        }, //for Search/Filter functionality - specify the search options
        frozen: true,
        editoptions: {
          size: 30
        },
        //editrules are set as part of form edit validation so we can mark required for all mandatory fields
        editrules: {
          required: true,
          custom: true,
          edithidden: true,
          custom_func: passwordCheck
        },
        formoptions: {
          label: jQuery.i18n.prop('userdetail.table.form.label.password')
        }
      }, {
        name: "active",
        index: "active",
        width: 80,
        sortable: true,
        align: 'center',
        stype: "select", //if stype is enabled then sopt will not work - stype is needed only when search should have select option
        editable: true,
        edittype: "select",
        formatter: "select",
        editoptions: {
          value: ":;1:Yes;0:No" //Default 0
        },
        cellattr: function(rowId, val, rawObject) {
          if (val == 1) {
            return " class='ui-state-useractive'";
          } else if (val == 0) {
            return " class='ui-state-userinactive'";
          }
        },
        formoptions: {
          //rowpos: 3,
          //colpos: 1
        }
      }, {
        name: "firstname",
        index: "firstname",
        width: 120,
        align: 'center',
        sortable: true,
        searchoptions: {
          sopt: ['cn', 'eq', 'ne', 'bw', 'bn', 'nc', 'ew', 'en']
        }, //for Search/Filter functionality - specify the search options
        editable: true,
        editoptions: {
          size: 30
        },
        editrules: {
          required: false,
          custom: true,
          custom_func: onlyAblhabetCheck
        }
      }, {
        name: "lastname",
        index: "lastname",
        width: 120,
        align: 'center',
        sortable: true,
        editable: true,
        searchoptions: {
          sopt: ['cn', 'eq', 'ne', 'bw', 'bn', 'nc', 'ew', 'en']
        }, //for Search/Filter functionality - specify the search options
        editoptions: {
          size: 30
        },
        editrules: {
          required: false,
          custom: true,
          custom_func: onlyAblhabetCheck
        }
      }, {
        name: "department",
        index: "department",
        width: 120,
        align: 'center',
        sortable: true,
        editable: true,
        searchoptions: {
          sopt: ['cn', 'eq', 'ne', 'bw', 'bn', 'nc', 'ew', 'en']
        }, //for Search/Filter functionality - specify the search options
        editoptions: {
          size: 30
        },
        editrules: {
          required: false,
          custom: true,
          custom_func: onlyAblhabetCheck
        }
      }, {
        name: "email",
        index: "email",
        align: 'center',
        width: 220,
        sortable: false,
        editable: true,
        searchoptions: {
          sopt: ['cn', 'eq', 'ne', 'bw', 'bn', 'nc', 'ew', 'en']
        }, //for Search/Filter functionality - specify the search options
        editoptions: {
          size: 30
        },
        editrules: {
          required: true,
          custom: true,
          custom_func: emailCheck
        },
        formoptions: {
          label: jQuery.i18n.prop('userdetail.table.form.label.email')
        }
      }, {
        name: "birthday",
        index: "birthday",
        width: 120,
        sortable: true,
        align: 'center',
        searchoptions: {
          sopt: ['cn', 'eq', 'ne', 'bw', 'bn', 'nc', 'ew', 'en'],
          dataInit: function(e) {
            $(e).datepicker({
              dateFormat: 'dd-mm-yy',
              maxDate: new Date()
            });
          }
        }, //for Search/Filter functionality - specify the search options
        editable: true,
        editoptions: {
          size: 10,
          dataInit: function(e) {
            $(e).datepicker({
              dateFormat: 'dd-mm-yy',
              maxDate: new Date()
            });
          }
        },
        formoptions: {
          //rowpos: 3,
          //colpos: 2
        }
      }, {
        name: "country",
        index: "country",
        width: 120,
        align: 'center',
        sortable: true,
        editable: true,
        searchoptions: {
          sopt: ['cn', 'eq', 'ne', 'bw', 'bn', 'nc', 'ew', 'en']
        }, //for Search/Filter functionality - specify the search options
        editoptions: {
          edittype: 'text',
          size: 30
        },
        editrules: {
          required: false,
          custom: true,
          custom_func: onlyAblhabetCheck
        }
      }, {
        name: "role",
        index: "role",
        width: 100,
        sortable: true,
        align: 'center',
        stype: "select", //if stype is enabled then sopt will not work - stype is needed only when search should have select option
        //searchoptions: {sopt:['cn','eq','ne','bw','bn','nc','ew','en']},//for Search/Filter functionality - specify the search options
        editable: true,
        edittype: "select",
        formatter: "select",
        editoptions: {
          value: ":;ADMIN:Admin;USER:User" //Default User
        },
        formoptions: {
          //rowpos: 3,
          //colpos: 1
        }
      }, {
        name: "oldPassword",
        index: "oldPassword",
        width: 10,
        sortable: false,
        editable: true,
        hidden: true,
        hidedlg: true //to remove this column from column chooser as well
      }],
      pager: "#userPager",
      iconSet: "fontAwesome",
      rowNum: 8,
      rownumbers: false, //Row numbers
      rowList: [8, 12, 20],
      height: 'auto',
      width: 'auto', //Autowidth
      loadonce: true,
      sortable: true,
      sortname: 'No',
      sortorder: 'desc',
      viewrecords: true,
      shrinkToFit: false,
      ignoreCase: true, //Case insensitive search
      gridview: true, //renders data more faster??
      //viewsortcols: true,//easily show what columns are sortable - With this sorting not working.
      caption: jQuery.i18n.prop('userdetail.table.caption'),
      editurl: "edit_user_detail",
      multiselect: false,
      toppager: true, //Toolbar option
      cloneToTop: true, //Toolbar option
      loadComplete: function() {
        this.p.lastSelected = lastSelected; // set this.p.lastSelected - to find out selected data from filter..
      }
    });

    // Navigator tab inside pager grid
    jQuery("#userGrid").jqGrid('navGrid', '#userPager', {
      //General options
      edit: false,
      add: false,
      del: false,
      search: false,
      view: false,
      refresh: false,
      cloneToTop: true //Must for top tool bar - this will close all the pager items in top tool bar.
    }, {
      // edit options
      height: 'auto',
      width: 'auto',
      top: 75,
      left: 350,
      modal: true,
      editCaption: jQuery.i18n.prop('userdetail.table.button.edituser'),
      processData: jQuery.i18n.prop('application.common.message.processing'),
      recreateForm: true,
      bottominfo: jQuery.i18n.prop('application.common.message.mandatoryfields'),
      bSubmit: jQuery.i18n.prop('application.common.button.save'),
      reloadAfterSubmit: false,
      closeOnEscape: true,
      //checkOnUpdate:true, //Form Navigation option
      //savekey: [true,13], //Form Navigation option
      //navkeys: [true,38,40], //Form Navigation option
      //checkOnSubmit : true,//Form Navigation option
      closeAfterEdit: true,
      afterSubmit: refreshData // Need to refresh the data in the table to reflect the hashed password back to this table.
    }, {
      //Add options
      height: 'auto',
      width: 'auto',
      top: 75,
      left: 350,
      modal: true,
      addCaption: jQuery.i18n.prop('userdetail.table.button.adduser'),
      processData: jQuery.i18n.prop('application.common.message.processing'),
      recreateForm: true,
      reloadAfterSubmit: false,
      closeAfterAdd: true,
      //checkOnUpdate:true,//Form Navigation option
      //savekey: [true,13], //Form Navigation option
      //navkeys: [true,38,40],//Form Navigation option
      //checkOnSubmit : true,//Form Navigation option
      closeOnEscape: true,
      bottominfo: jQuery.i18n.prop('application.common.message.mandatoryfields'),
      bSubmit: jQuery.i18n.prop('application.common.button.save'),
      afterSubmit: refreshData // Need to refresh the data in the table to reflect the primary key added to this table.
    }, {
      // del options
      reloadAfterSubmit: false,
      closeOnEscape: true
    }, {
      //Search options
      multipleSearch: true,
      multipleGroup: true
    }, {
      //View options
      height: 'auto',
      width: 'auto',
      top: 75,
      left: 350,
      viewCaption: jQuery.i18n.prop('userdetail.table.button.viewuser'),
      closeOnEscape: true
    });

    /*Search/Filter toolbar*/
    jQuery("#userGrid").jqGrid('filterToolbar', {
      searchOperators: true,
      searchOnEnter: false,
      jqModal: false,
      multipleSearch: true,
      multipleGroup: true,
      recreateFilter: true,
      closeOnEscape: true,
      overlay: 0,
      stringResult: true
    });

    //Top Toolbar options & Buttons
    //before adding any buttons remove all the items copied/cloned from the bottom pager
    $(".ui-paging-info", userGrid_toppager).remove();
    $("#add_userGrid_top", userGrid_toppager).remove(); //remove add icon from top pager
    $("#edit_userGrid_top", userGrid_toppager).remove(); //remove edit icon from top pager
    $("#del_userGrid_top", userGrid_toppager).remove(); //remove delete icon from top pager
    $("#view_userGrid_top", userGrid_toppager).remove(); //remove view icon from top pager
    $("#search_userGrid_top", userGrid_toppager).remove(); //remove search icon from top pager
    $("#refresh_userGrid_top", userGrid_toppager).remove(); //remove refresh icon from top pager
    $("#userGrid_toppager_center", userGrid_toppager).remove(); //remove center pager details from top pager

    //Toolbar button to add a config
    jQuery("#userGrid").jqGrid('navButtonAdd', '#userGrid_toppager', {
      caption: jQuery.i18n.prop('userdetail.table.button.adduser'),
      title: jQuery.i18n.prop('userdetail.table.title.add'),
      buttonicon: 'fa-user-plus',
      onClickButton: function() {
        jQuery("#userGrid").jqGrid('editGridRow', "new", {
          //Add options
          height: 'auto',
          width: 'auto',
          top: 75,
          left: 350,
          modal: true,
          addCaption: jQuery.i18n.prop('userdetail.table.button.adduser'),
          processData: jQuery.i18n.prop('application.common.message.processing'),
          recreateForm: true,
          reloadAfterSubmit: false,
          closeOnEscape: true,
          //checkOnUpdate:true,//Form Navigation option
          //savekey: [true,13], //Form Navigation option
          //navkeys: [true,38,40],//Form Navigation option
          //checkOnSubmit : true,//Form Navigation option
          bottominfo: jQuery.i18n.prop('application.common.message.mandatoryfields'),
          bSubmit: jQuery.i18n.prop('application.common.button.save'),
          afterSubmit: refreshData, // Need to refresh the data in the table to reflect the primary key added to this table.
          closeAfterAdd: true,
          /*beforeShowForm: function() { //NOT WORKING
                             // "editmodlist"
                             var dlgDiv = $("#editmod" + grid[0].id);
                             var parentDiv = dlgDiv.parent();
                             var dlgWidth = dlgDiv.width();
                             var parentWidth = parentDiv.width();
                             var dlgHeight = dlgDiv.height();
                             var parentHeight = parentDiv.height();
                             // TODO: change parentWidth and parentHeight in case of the grid
                             //       is larger as the browser window
                             dlgDiv[0].style.top = Math.round((parentHeight-dlgHeight)/2) + "px";
                             dlgDiv[0].style.left = Math.round((parentWidth-dlgWidth)/2) + "px";
                          }*/

        });
      }
    });

    //Toolbar button to edit a config
    jQuery("#userGrid").jqGrid('navButtonAdd', '#userGrid_toppager', {
      caption: jQuery.i18n.prop('userdetail.table.button.edituser'),
      title: jQuery.i18n.prop('userdetail.table.title.edit'),
      buttonicon: 'fa-users',
      onClickButton: function() {
        var gr = jQuery("#userGrid").jqGrid('getGridParam', 'selrow');
        if (gr != null)
          jQuery("#userGrid").jqGrid('editGridRow', gr, {
            //Edit options
            height: 'auto',
            width: 'auto',
            top: 75,
            left: 350,
            modal: true,
            editCaption: jQuery.i18n.prop('userdetail.table.button.edituser'),
            processData: jQuery.i18n.prop('application.common.message.processing'),
            recreateForm: true,
            bottominfo: jQuery.i18n.prop('application.common.message.mandatoryfields'),
            bSubmit: jQuery.i18n.prop('application.common.button.save'),
            reloadAfterSubmit: false,
            closeOnEscape: true,
            //checkOnUpdate:true,//Form Navigation option
            //savekey: [true,13], //Form Navigation option
            //navkeys: [true,38,40],//Form Navigation option
            //checkOnSubmit : true,//Form Navigation option
            closeAfterEdit: true,
            afterSubmit: refreshData, // Need to refresh the data in the table to reflect the hashed password
            /*beforeShowForm: function() { //NOT WORKING !!
                               // "editmodlist"
                               var dlgDiv = $("#editmod" + grid[0].id);
                               var parentDiv = dlgDiv.parent();
                               var dlgWidth = dlgDiv.width();
                               var parentWidth = parentDiv.width();
                               var dlgHeight = dlgDiv.height();
                               var parentHeight = parentDiv.height();
                               // TODO: change parentWidth and parentHeight in case of the grid
                               //       is larger as the browser window
                               dlgDiv[0].style.top = Math.round((parentHeight-dlgHeight)/2) + "px";
                               dlgDiv[0].style.left = Math.round((parentWidth-dlgWidth)/2) + "px";
                            }*/
          });
        else {
          $.jgrid.info_dialog.call(
            this,
            jQuery.i18n.prop('userdetail.table.dialog.message.caption'), // dialog title
            jQuery.i18n.prop('userdetail.table.dialog.message.selectrow'), // text inside of dialog
            jQuery.i18n.prop('application.common.button.close') // text in the button
          );
        }
      }
    });

    //Toolbar button to delete a user
    jQuery("#userGrid").jqGrid('navButtonAdd', '#userGrid_toppager', {
      caption: jQuery.i18n.prop('userdetail.table.button.deleteuser'),
      title: jQuery.i18n.prop('userdetail.table.title.delete'),
      buttonicon: 'fa-user-times',
      onClickButton: function() {
        var gr = jQuery("#userGrid").jqGrid('getGridParam', 'selrow');
        if (gr != null)
          jQuery("#userGrid").jqGrid('delGridRow', gr, {
            reloadAfterSubmit: false,
            closeOnEscape: true
          });
        else {
          $.jgrid.info_dialog.call(
            this,
            jQuery.i18n.prop('userdetail.table.dialog.message.caption'), // dialog title
            jQuery.i18n.prop('userdetail.table.dialog.message.selectrow'), // text inside of dialog
            jQuery.i18n.prop('application.common.button.close') // text in the button
          );
        }
      }
    });

    //Column chooser button in top toolbar
    jQuery("#userGrid").jqGrid('navButtonAdd', '#userGrid_toppager', {
      caption: jQuery.i18n.prop('application.common.button.columns'),
      title: jQuery.i18n.prop('userdetail.table.title.choosecolumn'),
      buttonicon: 'fa-columns',
      onClickButton: function() {
        jQuery("#userGrid").jqGrid('columnChooser');
      }
    });

    //Button to toggle toolbar search/filter option
    jQuery("#userGrid").jqGrid('navButtonAdd', "#userGrid_toppager", {
      caption: jQuery.i18n.prop('application.common.button.search'),
      title: jQuery.i18n.prop('userdetail.table.title.search'),
      buttonicon: 'fa-toggle-on',
      onClickButton: function() {
        userGrid[0].toggleToolbar();
      }
    });

    //Refresh Button as auto refresh not exists for user detail
    jQuery("#userGrid").jqGrid('navButtonAdd', '#userGrid_toppager', {
      caption: jQuery.i18n.prop('application.common.button.refresh'),
      title: jQuery.i18n.prop('userdetail.table.title.refreshuserdetail'),
      buttonicon: 'fa fa-cog fa-spin',
      onClickButton: function() {
        refreshData();
      }
    });

    //Frozen columns - NOT needed as of now as no. of columns are limited.
    //jQuery("#userGrid").jqGrid('setFrozenColumns');

    //Grid resize
    jQuery("#userGrid").jqGrid('gridResize', {
      minWidth: 550,
      maxWidth: 1300,
      minHeight: 80,
      maxHeight: 350
    });

    /**
     * Function to refresh the data after a new row inserted in DB. If
     * not then when we update the same row again, because of no primary
     * key reflected in client side, a fresh row will be inserted. Also
     * if we are refreshing the grid, then immediate delete of this row
     * will fail because again primary key(i.e. id) not reflected in the
     * client side
     *
     * NOT SURE IS THERE ANY OTHER BETTER WAY TO DO THIS !!!!
     */
    function refreshData() {
      jQuery("#userGrid").jqGrid("setGridParam", {
        datatype: 'json',
        url: 'list_user_detail',
        page: 1
      }).trigger("reloadGrid");
      return [true, '']; //TO make sure that control goes back to jqGrid inbuild configs like closeafterAdd etc.
    }

    //Username Validation checks !!
    function usernameCheck(value) {
      if (checkLength(value, 6, 50) && checkRegexp(value, /^[a-z]([0-9a-z_.])+$/i)) {
        return [true, "", ""];
      } else {
        return [false, jQuery.i18n.prop('userdetail.validation.message.username'), ""];
      }
    }

    //Password Validation checks !!
    function passwordCheck(value) {
      if (checkLength(value, 6, 60)) {
        return [true, "", ""];
      } else {
        return [false, jQuery.i18n.prop('userdetail.validation.message.password'), ""];
      }
    }

    //Firstname & Lastname Validation checks !!
    function onlyAblhabetCheck(value) {
      if (checkLength(value, 1, 50) && checkRegexp(value, /^[a-z]([a-z ])+$/i)) {
        return [true, "", ""];
      } else {
        return [false, jQuery.i18n.prop('userdetail.validation.message.firstname'), ""];
      }
    }

    //Email Validation checks !!
    function emailCheck(value) {
      if (checkLength(value, 6, 100) && checkRegexp(value, /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i)) {
        return [true, "", ""];
      } else {
        return [false, jQuery.i18n.prop('userdetail.validation.message.email'), ""];
      }
    }

    //Birthday Validation checks !!
    function birthdayCheck(value) {
      var now = new Date();
      if (value < now) {
        return [true, "", ""];
      } else {
        return [false, jQuery.i18n.prop('userdetail.validation.message.dob'), ""];
      }
    }

    /*Check length of particular field*/
    function checkLength(o, min, max) {
      if (o.length > max || o.length < min) {
        //o.addClass("ui-state-error");
        return false;
      } else {
        return true;
      }
    }

    /*Check regex/format of particular field*/
    function checkRegexp(o, regexp) {
      if (!(regexp.test(o))) {
        //o.addClass("ui-state-error");
        return false;
      } else {
        return true;
      }
    }


  });

Image attached - with round & question mark..! enter image description here

Sundar
  • 119
  • 1
  • 16
  • Wrapping of the toolbar icons is required by design. See [the wiki article](https://github.com/free-jqgrid/jqGrid/wiki/Redesign-of-the-structure-of-Navigator-Bar-of-the-pagers). You write about some images, but you don't included any images in the text. It would be helpful if you include the code which creates jqGrid. Which `width` have every column in `colModel`? Do you use `width` option of jqGrid? Many lines with `$("#...", userGrid_toppager).remove();` looks very strange. Why you not use just the options of jqGrid and `navGrid` to remove unneeded toolbar icons. It's better not create there – Oleg Sep 27 '15 at 18:28
  • Hi Oleg, infact I was looking for your reply.. Sadly I'm new to stackoverflow and not have enough points to upload images :( so had to remove the image to post my question... Can i share the code via Github ? – Sundar Sep 27 '15 at 18:36
  • You can just click on "edit" button under your question and modify the text. You can include the code of jqGrid. There are many common places in internet where you can place the picture at least temporary. You can include the URL in the text of your question and I would be edit the question and include the picture directly. – Oleg Sep 27 '15 at 19:16
  • Hi Oleg, Updated full script & as well as image now. On a seperate note, I'm facing a seperate issue regarding add/edi dialog not coming in cenre of the screen. Same jqgrid code as well there.. any suggestions?http://stackoverflow.com/questions/32807931/jqgrid-add-edit-dialog-not-aligned-to-centre-of-the-screen – Sundar Sep 27 '15 at 22:50

1 Answers1

4

You can solve your main problem by adding one line of code:

$("#userGrid_toppager_right").hide();

It hides the right part of the top pager and make the whole top page be used for the toolbar.

Additionally I would recommend you to review your existing code. It could be reduce in more as twice time and could be simplified if you would use more possibilities of free jqGrid. I'll mention below only some suggestions.

  • The code at the beginning overloads $.jgrid.from and later in loadComplete set lastSelected parameter of jqGrid. It's not needed in free jqGrid. You cen remove the code and just use built-in lastSelectedData option instead of lastSelected. The option lastSelectedData exist since free jqGrid 4.8 (see the demo from README48)
  • You should remove all index properties from all colModel items.
  • The code of colModel contains a lot of duplicates. I would recommend you to use column templates. You can either define some local objects and use there are a templates or to define the templates under $.jgrid.cmTemplate The properties of $.jgrid.cmTemplate can be used as string values of template property in colModel. Some common properties (like align: 'center' or editable: true) you can include in cmTemplate option of jqGrid. The option changes the default value of colModel properties and you can remove the duplicates from every colModel items. I would recommend you to read the old answer and the pull request.
  • You use many times in your code jQuery("#userGrid") and jQuery.i18n.prop. You should understand that it's expression which will be evaluated every time in your code. It would be practical to introduce variables like var $grid = jQuery("#userGrid"), localeProp = jQuery.i18n.prop; and use the variables in your code. The function refreshData and all onClickButton can use jQuery(this) instead of jQuery("#userGrid").
  • Working with pagers and toolbars can be simplified essentially. First of all you can replace pager: "#userPager" to pager: true and remove <div id="userPager"></div> from HTML markup. Free jqGrid will create the div for the page in the same way like it creates the div for top pager.
  • You can replace the long call of navGrid to the following:
jQuery("#userGrid").jqGrid('navGrid', '#userGrid_toppager', {
    //General options
    edit: false,
    add: false,
    del: false,
    search: false,
    view: false,
    refresh: false
});

which creates navigator bar only for the top pager.

  • the code block where you use $("#...", userGrid_toppager).remove(); can be replaced with one line of code
$("#userGrid_toppager_center,#userGrid_toppager_right").hide();

which hides the right and the middle parts of the top pager.

  • I would recommend you to read the wiki article and to use new style of options of form editing and navGrid. You can define navOptions, formEditing, formDeleting, searching as jqGrid parameters which would specify default values from navGrid, editGridRow and so on. If can reduce and simplify the code, make it more readable and easy to maintain.
Community
  • 1
  • 1
Oleg
  • 220,925
  • 34
  • 403
  • 798
  • Many thanks Oleg.. I addressed all other review comments as well.. I'm doing a project to practice only but it's good to know the best practice... Couple of minor points just to verify whether i got it right... jQuery(this) i snot working in refreshData() method but $grid is working. And also custom template all working well.. but can i define few property in cmTemplate and then override a particular property in each colModel ? E.g. i define sorttype='text' in cmTemplate but for some column i'll define sorttype='integer' in colModel... – Sundar Sep 29 '15 at 22:05
  • @Sundar: You are welcome! I think you understand all correctly with exception of `$grid`. You have already `var userGrid = $("#userGrid")`. So you can use `userGrid` variable not only in one place of your code, but everywhere instead of `jQuery("#userGrid")`. If you define and assign a value in **outer function** then it's accessible in every internal function. For example you can write `userGrid.jqGrid({...})` instead of `$("#userGrid").jqGrid({...})` – Oleg Sep 30 '15 at 05:41
  • I used it in same way Oleg and also I implemented all the valuable feedback which you gave as well. But here my question is bit different on cmTemplate.. "Can I define few property in cmTemplate and then override a particular property in each colModel ? E.g. i define sorttype='text' in cmTemplate but for some column i'll define sorttype='integer' in colModel...? Also one more selfish question after practicing client technology for last 6 months.. how can i learn better the client technologies.. spcly Java script, Jquery !! Any tips from your great experience..? – Sundar Sep 30 '15 at 20:29
  • @Sundar: You can define `cmTemplate` option of jqGrid, `template` property in `colModel` and some properties directly in `colModel`. All three sources will be combined. The highest priority have the property defined in `colModel` item. It overwrites any property from `template` and `cmTemplate`. In the same way `template` have the next priority and the lowest priority have `cmTemplate`. For example if I have one or two non-editable column and all ather are editable then I define `cmTemplate: {editable: true}` and use `editable: false` in the one-two columns. – Oleg Sep 30 '15 at 20:49
  • @Sundar: About JavaScript. It was for me very important someday to understand that JavaScript is **absolutely another language** as C/C++/C#/Java. It looks just close, but it have **another semantic**. Many newcomers use the language in the same way like his favorite language and they produce very bad JavaScript code which runs not like it looks. For me the most important was understanding the importance of other formatting, `var` declaration, the scope of variables, the time of live the variables, closure (function which access outer variables) and the executing context created for closure. – Oleg Sep 30 '15 at 21:03
  • @Sundar: The books which I read to understand JavaScript was "JavaScript: The Good Parts" by Douglas Crockford. He like too much his own style of codding, but I studied many important thing from his small book. The next book was "JavaScript Patterns" by Stoyan Stefanov. One more book which I find very good is "Effective JavaScript" by by David Herman. Of cause one can find many free information in internet, but I personally like to read books too. – Oleg Sep 30 '15 at 21:10
  • @Sundar: I find jQuery not so complex in usage. Of cause one should study the most functions, but for me it was important to understand that jQuery objects are mostly the wrapper on DOM elements. One get it by using `[0]` for example. `$("#list")` if the wrapper to DOM elements which have `id="list"`. It was important to understand that jQuery provides only *common* API existing in root DOM class, but many *specific* elements (``, ``, `
    `) have specific methods or properties. See [here](http://www.w3schools.com/jsref/dom_obj_tabledata.asp) for example `cellIndex` property of td.
    – Oleg Sep 30 '15 at 21:18
  • @Sundar: I think that [the source](http://learn.jquery.com/) contains enough information which one need for understanding of jQuery. One need additionally invest more time in understanding of specific functions of jQuery, but I personally find it not so complex as good understanding of JavaScript language. – Oleg Sep 30 '15 at 21:21
  • Many Thanks Oleg for your comment on cmTemplate, I'll try to optimize some more with this feedback. But I'm really enjoying your review comments and I wish you see my end to end code so I can learn more someday. I know you might be busy and don't want to take much of your time but do let me know i'm happy to share some of my code at later stage. My idea is to develop a demo of all the features of jqgrid or any technology I learn. :) Thanks for your insight on javascript and jquery I should find some time to learn that once i complete my current learnings !! :) – Sundar Sep 30 '15 at 21:33