Simple-data-grid is a datagrid widget.
The project is hosted on github and has unit tests.
Name | Latin name |
---|
<table id="demo-table" data-url="/fruits.json">
<thead>
<tr>
<th>Name</th>
<th>Latin name</th>
</tr>
</thead>
</table>
$(function() {
$('#demo-table').simple_datagrid();
});
The result is in example1.html.
First, we need an html table.
<table id="example1">
</table>
Lets's add some columns. We can define them in <th> elements.
<table id="example1">
<thead>
<tr>
<th>Name</th>
<th>Latin name</th>
</tr>
</thead>
</table>
We need to include some javascript and css.
<link rel="stylesheet" href="simple.datagrid.css">
<script src="extra/jquery-1.8.2.min.js"></script>
<script src="simple.datagrid.js"></script>
Fill the grid with some data:
<script>
$(function() {
$('#example1').simple_datagrid({
data: [
["Avocado", "Persea americana"],
["Bell pepper", "Capsicum annuum"],
["Bitter melon", "Momordica charantia"]
]
});
});
</script>
The result looks like this:
Name | Latin name |
---|
In example 1 the data is defined in a javascript array. We can also get the data from the server via ajax.
The result is in example1a.html.
Define a table with a data-url property:
<table id="example1a" data-url="extra/fruits.json">
<thead>
<tr>
<th>Name</th>
<th>Latin name</th>
</tr>
</thead>
</table>
Initialize the grid:
<script>
$(function() {
$('#example1a').simple_datagrid();
});
</script>
A column has the following properties:
You can define columns in the <th> elements of a table. The elements must be in the <thead>.
<table>
<thead>
<th>Name</th>
<th>Latin name</th>
</thead>
</table>
You can also set the key of a column in the data-key property.
For example, to set the key for the Latin name column to latin:
<table>
<thead>
<th>Name</th>
<th data-key="latin">Latin name</th>
</thead>
</table>
You can define the columns in the columns property of the javascript options.
$('#table1').simple_datagrid({
columns: ['Name', 'Latin name']
});
You can also define columns as an object:
$('#table1').simple_datagrid({
columns: [
{
title: 'Name'
},
{
title: 'Latin name',
key: 'latin'
}
]
});
The data is an array of rows. You can define a row as an array or as an object.
var data = [
['Avocado', 'Persea americana'],
['Bell pepper', 'Capsicum annuum']
];
var data = [
{
name: 'Avocado',
'latin_name': 'Persea americana'
},
{
name: 'Bell pepper',
'latin_name': 'Capsicum annuum'
}
];
You can set an url in the data-url property of the table.
<table data-url="/fruits.json">
</table>
You can also set the url in javascript.
$('#table1').simple_datagrid({
url: '/fruits.json'
});
You can define the data in a javascript array:
$('#table1').simple_datagrid({
data: [
["Avocado", "Persea americana"],
["Bell pepper", "Capsicum annuum"],
["Bitter melon", "Momordica charantia"]
]
});
You can also define the data as an array of objects:
$('#table1').simple_datagrid({
data: [
{
name: 'Avocado',
'latin_name': 'Persea americana'
},
{
name: 'Bell pepper',
'latin_name': 'Capsicum annuum'
}
]
});
Reload the data.
$('#tree1').simple_datagrid('reload');