The Ignite UI for jQuery® igGrid
, allows binding to existing plain HTML tables, through the igDataSource
control. There are several points to consider when binding to a HTML table.
dataSource
. If you are instantiating the igGrid
widget on the same table to which you would like to binddataSource
option of the grid to the DOM element pointing to the HTML table containing your data. Listing 1 shows how to do thatListing 1: HTML TABLE as dataSource
In Javascript:
dataSource: $('#myTable')[0]
Note: the indexer is needed in order to select the DOM element containing the data.
headerText
for the jQuery grid.headerText
or the dataType
of a column, by specifying the settings in code. For example if you know that a specific column contains integers, but you don’t want this to be parsed as strings.Listing 2 shows a complete example that shows how to bind the grid to an existing HTML table and configured to shows fewer columns than are in the table.
Listing 2: Binding to the same HTML Table
In Javascript:
$("#t1").igGrid({
autoGenerateColumns: false,
columns: [
{ headerText: "ProductID", key: "ProductID", width: "100px", dataType: "number" },
{ headerText: "Name", key: "Name", width: "80px", dataType: "string" },
],
defaultColumnWidth: 150,
features: [
{
name: 'Sorting',
type: 'local'
}
]
});
In HTML:
<table id="t1" cellpadding="5" cellspacing="0" border="1">
<thead>
<tr>
<th>
Product ID
</th>
<th>
Product Name
</th>
<th>
Product Number
</th>
</tr>
</thead>
<tbody>
<tr><td>1</td><td>Adjustable Race</td><td>AR-5381</td></tr>
<tr><td>2</td><td>Bearing Ball</td><td>BA-8327</td></tr>
<tr><td>3</td><td>BB Ball Bearing</td><td>BE-2349</td></tr>
<tr><td>4</td><td>Headset Ball Bearings</td><td>BE-2908</td></tr>
<tr><td>316</td><td>Blade</td><td>BL-2036</td></tr>
<tr><td>317</td><td>LL Crankarm</td><td>CA-5965</td></tr>
<tr><td>318</td><td>ML Crankarm</td><td>CA-6738</td></tr>
<tr><td>319</td><td>HL Crankarm</td><td>CA-7457</td></tr>
<tr><td>320</td><td>Chainring Bolts</td><td>CB-2903</td></tr>
<tr><td>321</td><td>Chainring Nut</td><td>CN-6137</td></tr>
</tbody>
</table>
Listing 3 shows how to bind the grid to a new HTML table and override the default data type of string when Table data is parsed when the jQuery Grid is bound to HTML all keys are named 1 to n, where "n" is the number of cells in a table row.
Listing 3: Binding to a new HTML Table
In Javascript:
$("#t2").igGrid({
columns: [
{key: 1, width: "100px", dataType: "number", headerText: "[Custom Header]" }
],
defaultColumnWidth: 150,
features: [
{
name: 'Sorting',
type: 'local'
}
],
dataSource: $("#t1")[0]
});
In HTML:
<table id="t1" cellpadding="5" cellspacing="0" border="1">
<thead>
<tr>
<th>
Product ID
</th>
<th>
Product Name
</th>
<th>
Product Number
</th>
</tr>
</thead>
<tbody>
<tr><td>1</td><td>Adjustable Race</td><td>AR-5381</td></tr>
<tr><td>2</td><td>Bearing Ball</td><td>BA-8327</td></tr>
<tr><td>3</td><td>BB Ball Bearing</td><td>BE-2349</td></tr>
<tr><td>4</td><td>Headset Ball Bearings</td><td>BE-2908</td></tr>
<tr><td>316</td><td>Blade</td><td>BL-2036</td></tr>
<tr><td>317</td><td>LL Crankarm</td><td>CA-5965</td></tr>
<tr><td>318</td><td>ML Crankarm</td><td>CA-6738</td></tr>
<tr><td>319</td><td>HL Crankarm</td><td>CA-7457</td></tr>
<tr><td>320</td><td>Chainring Bolts</td><td>CB-2903</td></tr>
<tr><td>321</td><td>Chainring Nut</td><td>CN-6137</td></tr>
</tbody>
</table>
<table id="t2"></table>
Running Sample
The igGrid
has known limitations that should be taken into account.
View on GitHub