jQuery Grid with Custom Pager

In this post, we will show you how to create a custom pager for our jQuery Grid widget and make it look like in the screenshot below: jquery-datagrid-pager 1. Add the references to the JavaScript and CSS files.
<link rel="stylesheet" href="../../jqwidgets/styles/jqx.base.css" type="text/css" />
<script type="text/javascript" src="../../scripts/jquery-1.7.1.min.js"></script>
<script type="text/javascript" src="../../jqwidgets/jqxcore.js"></script>
<script type="text/javascript" src="../../jqwidgets/jqxdata.js"></script>
<script type="text/javascript" src="../../jqwidgets/jqxbuttons.js"></script>
<script type="text/javascript" src="../../jqwidgets/jqxscrollbar.js"></script>
<script type="text/javascript" src="../../jqwidgets/jqxmenu.js"></script>
<script type="text/javascript" src="../../jqwidgets/jqxgrid.js"></script>
<script type="text/javascript" src="../../jqwidgets/jqxgrid.selection.js"></script>
<script type="text/javascript" src="../../jqwidgets/jqxgrid.columnsresize.js"></script>
<script type="text/javascript" src="../../jqwidgets/jqxlistbox.js"></script>
<script type="text/javascript" src="../../jqwidgets/jqxgrid.pager.js"></script>
<script type="text/javascript" src="../../jqwidgets/jqxdropdownlist.js"></script>
2. Create a new instance of the jqxDataAdater plug-in and initialize it with sample data.
var data = new Array();
var firstNames =
[
"Andrew", "Nancy", "Shelley", "Regina", "Yoshi", "Antoni", "Mayumi", "Ian", "Peter", "Lars", "Petra", "Martin", "Sven", "Elio", "Beate", "Cheryl", "Michael", "Guylene"
];
var lastNames =
[
"Fuller", "Davolio", "Burke", "Murphy", "Nagase", "Saavedra", "Ohno", "Devling", "Wilson", "Peterson", "Winkler", "Bein", "Petersen", "Rossi", "Vileid", "Saylor", "Bjorn", "Nodier"
];
var productNames =
[
"Black Tea", "Green Tea", "Caffe Espresso", "Doubleshot Espresso", "Caffe Latte", "White Chocolate Mocha", "Cramel Latte", "Caffe Americano", "Cappuccino", "Espresso Truffle", "Espresso con Panna", "Peppermint Mocha Twist"
];
var priceValues =
[
"2.25", "1.5", "3.0", "3.3", "4.5", "3.6", "3.8", "2.5", "5.0", "1.75", "3.25", "4.0"
];
for (var i = 0; i < 51; i++) {
var row = {};
var productindex = Math.floor(Math.random() * productNames.length);
var price = parseFloat(priceValues[productindex]);
var quantity = 1 + Math.round(Math.random() * 10);
row["firstname"] = firstNames[Math.floor(Math.random() * firstNames.length)];
row["lastname"] = lastNames[Math.floor(Math.random() * lastNames.length)];
row["productname"] = productNames[productindex];
row["price"] = price;
row["quantity"] = quantity;
row["total"] = price * quantity;
data[i] = row;
}
var source =
{
localdata: data,
datatype: "array"
};
var dataAdapter = new $.jqx.dataAdapter(source);
3. Add a DIV tag for the jqxGrid to the document’s body.
<div id="jqxgrid"></div>
4. Initialize the Grid and set its pagerrenderer property to point to a custom function called pagerrenderer.
$("#jqxgrid").jqxGrid(
{
source: dataAdapter,
columnsresize: true,
width: 650,
pageable: true,
autoheight: true,
pagerrenderer: pagerrenderer,
columns: [
{ text: 'First Name', dataField: 'firstname', width: 100 },
{ text: 'Last Name', dataField: 'lastname', width: 100 },
{ text: 'Product', dataField: 'productname', width: 170 },
{ text: 'Quantity', dataField: 'quantity', width: 80, cellsalign: 'right' },
{ text: 'Unit Price', dataField: 'price', width: 90, cellsalign: 'right', cellsformat: 'c2' },
{ text: 'Total', dataField: 'total', cellsalign: 'right', cellsformat: 'c2' }
]
});
5. Implement the pagerrenderer function. The pagerrenderer function creates a new pager element with navigation button. Each navigation button, calls the appropriate Grid API functions for navigation through the pages. The function returns as a result the new pager element to the Grid.
var pagerrenderer = function () {
var element = $("<div style='margin-top: 5px; width: 100%; height: 100%;'></div>");
var datainfo = $("#jqxgrid").jqxGrid('getdatainformation');
var paginginfo = datainfo.paginginformation;
// create navigation buttons.
var leftButton = $("<div style='padding: 1px; float: left;'><div style='margin-left: 9px; width: 16px; height: 16px;'></div></div>");
leftButton.find('div').addClass('icon-arrow-left');
leftButton.width(36);
leftButton.jqxButton();
var rightButton = $("<div style='padding: 1px; margin: 0px 3px; float: left;'><div style='margin-left: 9px; width: 16px; height: 16px;'></div></div>");
rightButton.find('div').addClass('icon-arrow-right');
rightButton.width(36);
rightButton.jqxButton();
// append the navigation buttons to the container DIV tag.
leftButton.appendTo(element);
rightButton.appendTo(element);
// create a page information label and append it to the container DIV tag.
var label = $("<div style='font-size: 11px; margin: 2px 3px; font-weight: bold; float: left;'></div>");
label.text("1-" + paginginfo.pagesize + ' of ' + datainfo.rowscount);
label.appendTo(element);
// navigate to the next page when the right navigation button is clicked.
rightButton.click(function () {
$("#jqxgrid").jqxGrid('gotonextpage');
var datainfo = $("#jqxgrid").jqxGrid('getdatainformation');
var paginginfo = datainfo.paginginformation;
label.text(1 + paginginfo.pagenum * paginginfo.pagesize + "-" + Math.min(datainfo.rowscount, (paginginfo.pagenum + 1) * paginginfo.pagesize) + ' of ' + datainfo.rowscount);
});
// navigate to the previous page when the right navigation button is clicked.
leftButton.click(function () {
$("#jqxgrid").jqxGrid('gotoprevpage');
var datainfo = $("#jqxgrid").jqxGrid('getdatainformation');
var paginginfo = datainfo.paginginformation;
label.text(1 + paginginfo.pagenum * paginginfo.pagesize + "-" + Math.min(datainfo.rowscount, (paginginfo.pagenum + 1) * paginginfo.pagesize) + ' of ' + datainfo.rowscount);
});
// return the new pager element.
return element;
}

About admin


This entry was posted in JavaScript, JavaScript Plugins, JavaScript UI, JavaScript UI Plugins, JavaScript UI Widgets, JavaScript Widgets, jQuery, jQuery Plugins, jQuery UI, jQuery UI Plugins, jQuery UI Widgets, jQuery Widgets, jQWidgets, jqxButton, jqxGrid and tagged , , , , , , , , , , , , , , , , , , , , , , , , , . Bookmark the permalink.



2 Responses to jQuery Grid with Custom Pager

  1. David says:

    I appreciate the example, but using id selectors in the pagingrenderer just kdoesn’t look good – we want ths to be as generic as possible, and your example wou.d basically require us to generate this bit of code for each page or grid we render, which isn’t really great for reuse 🙂

    • admin says:

      Hi David,

      It is not necessary to use ‘id’ selectors and it is also not necessary to implement the function in every page. You can implement it once in a new or existing javascript file and include that file in your pages,.

      Best Regards,
      Peter Stoev

Leave a Reply