Below is a sample JSON data that we want to use as a data source. In this post, we will assume that this data is in a file called beverages.txt.
[{
"id": "1",
"name": "Hot Chocolate",
"type": "Chocolate Beverage",
"calories": "370",
"totalfat": "16g",
"protein": "14g"
}, {
"id": 2,
"name": "Peppermint Hot Chocolate",
"type": "Chocolate Beverage",
"calories": "440",
"totalfat": "16g",
"protein": "13g"
}, {
"id": "3",
"name": "Salted Caramel Hot Chocolate",
"type": "Chocolate Beverage",
"calories": "450",
"totalfat": "16g",
"protein": "13g"
}, {
"id": "4",
"name": "White Hot Chocolate",
"type": "Chocolate Beverage",
"calories": "420",
"totalfat": "16g",
"protein": "12g"
}, {
"id": "5",
"name": "Caffe Americano",
"type": "Espresso Beverage",
"calories": "15",
"totalfat": "0g",
"protein": "1g"
}, {
"id": "6",
"name": "Caffe Latte",
"type": "Espresso Beverage",
"calories": "190",
"totalfat": "7g",
"protein": "12g"
}, {
"id": "7",
"name": "Caffe Mocha",
"type": "Espresso Beverage",
"calories": "330",
"totalfat": "15g",
"protein": "13g"
}, {
"id": "8",
"name": "Cappuccino",
"type": "Espresso Beverage",
"calories": "120",
"totalfat": "4g",
"protein": "8g"
}, {
"id": "9",
"name": "Caramel Brulee Latte",
"type": "Espresso Beverage",
"calories": "420",
"totalfat": "9g",
"protein": "8g"
}, {
"id": "10",
"name": "Caramel Macchiato",
"type": "Espresso Beverage",
"calories": "240",
"totalfat": "11g",
"protein": "10g"
}, {
"id": "11",
"name": "Peppermint Hot Chocolate",
"type": "Espresso Beverage",
"calories": "440",
"totalfat": "10g",
"protein": "13g"
}, {
"id": "12",
"name": "Cinnamon Dolce Latte",
"type": "Espresso Beverage",
"calories": "260",
"totalfat": "6g",
"protein": "10g"
}, {
"id": "13",
"name": "Eggnog Latte",
"type": "Espresso Beverage",
"calories": "460",
"totalfat": "16g",
"protein": "13g"
}]
To initialize the Grid, we need to add the references to the javascript and css files.
<link rel="stylesheet" href="styles/jqx.base.css" type="text/css" />
<link rel="stylesheet" href="styles/jqx.classic.css" type="text/css" />
<script type="text/javascript" src="jquery-1.7.1.min.js"></script>
<script type="text/javascript" src="jqxcore.js"></script>
<script type="text/javascript" src="jqxbuttons.js"></script>
<script type="text/javascript" src="jqxscrollbar.js"></script>
<script type="text/javascript" src="jqxmenu.js"></script>
<script type="text/javascript" src="jqxgrid.js"></script>
<script type="text/javascript" src="jqxdata.js"></script>
Add a div tag to the document’s body. The div tag will be used for the Grid widget.
<div id="jqxgrid"></div>
Now, lets create the ‘data’ object. We want to bind the Grid to JSON data, so we set the ‘datatype’ member to ‘json’. The ‘url’ member points to the url where the beverages.txt file is. The ‘datafields’ array member represents the Grid columns definitions. Each member should have ‘name’ and optionally a ‘type’ member such as ‘int’, ‘float’, ‘date’, and ‘bool’.
// prepare the data
var data =
{
datatype: "json",
datafields: [
{ name: 'name'},
{ name: 'type'},
{ name: 'calories'},
{ name: 'totalfat'},
{ name: 'protein'},
],
id: 'id',
url: "beverages.txt"
};
Populate the Grid. Select the ‘jqxgrid’ element and call the jqxGrid constructor. In the constructor, set the source property to the ‘data’ object and create the grid columns. The ‘text’ member specifies the text displayed in the column’s header and the ‘datafield’ member associates the grid column(ex: ‘Name’) to the data column(ex: ‘name’).
$("#jqxgrid").jqxGrid(
{
source: data
theme: 'classic',
columns: [
{ text: 'Name', datafield: 'name', width: 250},
{ text: 'Beverage Type', datafield: 'type', width: 250 },
{ text: 'Calories', datafield: 'calories', width: 180 },
{ text: 'Total Fat', datafield: 'totalfat', width: 120 },
{ text: 'Protein', datafield: 'protein', width: 120 }
]
});