YUI 3.x Home -

YUI Library Examples: DataTable: Basic DataTable

DataTable: Basic DataTable

The DataTable widget displays data in a tabular format. Use plugins and extensions to add features and functionality to the Y.DataTable.Base class.

Example table with simple columns
id
name
price
ga_3475
gadget
$6.99
sp_9980
sprocket
$3.75
wi_0650
widget
$4.25
These columns have labels and abbrs
Mfr Part ID
Mfr Part Name
Wholesale Price
ga_3475
gadget
$6.99
sp_9980
sprocket
$3.75
wi_0650
widget
$4.25

Simple DataTable Use Cases

A basic table can be rendered by passing in a simple array of columns and an array of data objects. As long as the columns map directly to the keys of the data objects, the table will be generated automatically from the data provided.

  1. YUI().use("datatable-base", function(Y) {
  2. var cols = ["id","name","price"],
  3. data = [
  4. {id:"ga-3475", name:"gadget", price:"$6.99"},
  5. {id:"sp-9980", name:"sprocket", price:"$3.75"},
  6. {id:"wi-0650", name:"widget", price:"$4.25"}
  7. ],
  8. dt = new Y.DataTable.Base({
  9. columnset: cols,
  10. recordset: data,
  11. summary: "Price sheet for inventory parts",
  12. caption: "Example table with simple columns"
  13. }).render("#example");
  14. });
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");
});

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.

  1. YUI().use("datatable-base", function(Y) {
  2. var cols = [
  3. { key: "mfr_parts_database_id", label: "Mfr Part ID", abbr: "ID"},
  4. { key: "mfr_parts_database_name", label: "Mfr Part Name", abbr: "Name"},
  5. { key: "mfr_parts_database_price", label: "Wholesale Price", abbr: "Price"}
  6. ],
  7. data = [
  8. { mfr_parts_database_id: "ga_3475", mfr_parts_database_name: "gadget", mfr_parts_database_price: "$6.99"},
  9. { mfr_parts_database_id: "sp_9980", mfr_parts_database_name: "sprocket", mfr_parts_database_price: "$3.75"},
  10. { mfr_parts_database_id: "wi_0650", mfr_parts_database_name: "widget", mfr_parts_database_price: "$4.25"}
  11. ],
  12. dt = new Y.DataTable.Base({
  13. columnset: cols,
  14. recordset: data,
  15. summary: "Price sheet for inventory parts",
  16. caption: "These columns have labels and abbrs"
  17. }).render("#example");
  18. });
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");
});

Copyright © 2011 Yahoo! Inc. All rights reserved.

Privacy Policy - Terms of Service - Copyright Policy - Job Openings