Dynamically update and clear the Grid's source.
A live JavaScript/jQuery example of jqxGrid, the jQWidgets Grid component, with its complete source code below. It is one of 139 jqxGrid demos for jQuery.
theme property – Sets the widget's theme.height property – Sets or gets the Grid's height.width property – Sets or gets the Grid's width.columns property – Sets the Grid columns.source property – Sets and gets the source.clear method – Clears the Grid contents.updatebounddata method – Updates the bound data and refreshes the grid.To run it in your own page, load jqx.base.css, a theme and the library files below from npm (npm install jqwidgets-scripts) or the jsDelivr CDN, then add the markup and the script. jQuery is not required: jqxcore.js provides $. Getting started with jqxGrid shows the setup step by step.
The page loads jqx.base.css, a theme and these library files: jqxcore.js, jqxdata.js, jqxbuttons.js, jqxscrollbar.js, jqxmenu.js, jqxgrid.js, jqxgrid.selection.js, jqxexpander.js, jqxdragdrop.js.
It also loads helper scripts of this demo site, which are not part of the library: demos.js (getWidth() and other layout helpers), generatedata.js (generatedata(), random sample rows).
$(document).ready(function () {
var source =
{
localdata: generatedata(500),
datafields:
[
{ name: 'firstname', type: 'string' },
{ name: 'lastname', type: 'string' },
{ name: 'productname', type: 'string' },
{ name: 'quantity', type: 'number' },
{ name: 'price', type: 'number' },
{ name: 'total', type: 'number' }
],
datatype: "array"
};
var dataAdapter = new $.jqx.dataAdapter(source);
var columns = [
{ text: 'First Name', dataField: 'firstname', width: 130 },
{ text: 'Last Name', dataField: 'lastname', width: 130 },
{ text: 'Product', dataField: 'productname', width: 180 },
{ 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', minwidth: 100, cellsformat: 'c2' }
];
// create data grid.
$("#grid").jqxGrid(
{
width: getWidth('Grid'),
height: 300,
source: dataAdapter,
columns: columns
});
// init buttons.
$("#refresh").jqxButton({ theme: theme });
$("#clear").jqxButton({ theme: theme });
$("#refresh").click(function () {
source.localdata = generatedata(500);
// passing "cells" to the 'updatebounddata' method will refresh only the cells values when the new rows count is equal to the previous rows count.
$("#grid").jqxGrid('updatebounddata', 'cells');
});
$("#clear").click(function () {
$("#grid").jqxGrid('clear');
});
});
<div id="jqxWidget">
<div id="grid">
</div>
<div style="margin-top: 10px;">
<input id="refresh" type="button" value="Refresh Data">
<input id="clear" type="button" value="Clear">
</div>
</div>