datatable-basic.mustache revision 819e90d415ed17d59af3a247b2ad9d6feb0c21b5
<style scoped>
/* custom styles for this example */
.dt-example {margin:1em;}
/* css to counter global site css */
.dt-example th {text-transform:none;}
.dt-example table {width:auto;}
.dt-example caption {display:table-caption;}
</style>
<div class="intro">
<p>The DataTable widget displays data in a tabular format. Use plugins and extensions to add features and functionality to the `Y.DataTable.Base` class.</p>
</div>
<div class="example yui3-skin-sam">
{{>datatable-basic-source}}
</div>
<h2>Simple DataTable Use Cases</h2>
```
<div id="example" class="yui3-skin-sam"></div>
```
<p>A basic table can be rendered into a given container by passing in a simple array of columns and an array of data objects into the constructor. As long as the columns map directly to the keys of the data objects, the table will be generated automatically from the data provided.</p>
```
YUI().use("datatable-base", function(Y) {
var cols = ["id","name","price"],
data = [
{id:"ga-3475", name:"gadget", price:"$6.99"},
{id:"sp-9980", name:"sprocket", price:"$3.75"},
{id:"wi-0650", name:"widget", price:"$4.25"}
],
dt = new Y.DataTable.Base({
columnset: cols,
recordset: data,
summary: "Price sheet for inventory parts",
caption: "Example table with simple columns"
}).render("#example");
});
```
<p>Your column definitions may be an array of object literals in order to support any number of configurations that are supported by the `Y.Column` class. For instance, you may wish to define a `label` that will be displayed in each column header. Use the `key` property to define where data for the column will be coming from.</p>
```
YUI().use("datatable-base", function(Y) {
var cols = [
{ key: "mfr_parts_database_id", label: "Mfr Part ID", abbr: "ID"},
{ key: "mfr_parts_database_name", label: "Mfr Part Name", abbr: "Name"},
{ key: "mfr_parts_database_price", label: "Wholesale Price", abbr: "Price"}
],
data = [
{ mfr_parts_database_id: "ga_3475", mfr_parts_database_name: "gadget", mfr_parts_database_price: "$6.99"},
{ mfr_parts_database_id: "sp_9980", mfr_parts_database_name: "sprocket", mfr_parts_database_price: "$3.75"},
{ mfr_parts_database_id: "wi_0650", mfr_parts_database_name: "widget", mfr_parts_database_price: "$4.25"}
],
dt = new Y.DataTable.Base({
columnset: cols,
recordset: data,
summary: "Price sheet for inventory parts",
caption: "These columns have labels and abbrs"
}).render("#example");
});
```