YUI Library Home

Site Search (YDN & YUIBlog):

# YUI Library Examples: DataTable Control: Adding, Updating, and Deleting Rows

# DataTable Control: Adding, Updating, and Deleting Rows

Adding, updating, and deleting row data dynamically.

View example in new window.

row(s) at index

## Sample Code for this Example

CSS:

/* custom styles for this example */ .modform {margin-bottom: 1em;} .index {width:5em;}

Markup:

<form class="modform"> <select id="mode"> <option value="add">Add</option> <option value="update">Update</option> <option value="deletestandard">Delete top-to-bottom</option> <option value="deletereverse">Delete bottom-up</option> </select> <select id="count"> <option value=1>1</option> <option value=5>5</option> <option value=10>10</option> <option value=25>25</option> <option value=100>100</option> </select> row(s) at index <input id="index" type="text" value="0" class="index"> <span id="go" class="yui-button yui-push-button"> <span class="first-child"> <button type="button">Go!</button> </span> </span> </form> <div id="container"></div>

JavaScript:

YAHOO.util.Event.addListener(window, "load", function() { YAHOO.example.RowDataMod = function() { var myColumnDefs = [ {key:"row", label:"row counter", resizeable:true,sortable:true}, {key:"one",resizeable:true}, {key:"two",resizeable:true}, {key:"three",resizeable:true} ]; var myDataSource = new YAHOO.util.DataSource([]); myDataSource.responseType = YAHOO.util.DataSource.TYPE_JSARRAY; myDataSource.responseSchema = { fields: ["one","two","three"] }; var myDataTable = new YAHOO.widget.DataTable("container", myColumnDefs, myDataSource, {}); var i=1, bReverseSorted = false; // Track when Column is reverse-sorted, since new data will come in out of order var trackReverseSorts = function(oArg) { bReverseSorted = (oArg.dir === YAHOO.widget.DataTable.CLASS_DESC); }; var globalDataCount = -1, getData = function(count) { if(count) { var allData = []; for(var i=0; i<count; i++) { globalDataCount++; allData.push({row:globalDataCount, one:"one", two:"two", three:"three"}); } return allData; } else { globalDataCount++; return {row:globalDataCount, one:"one", two:"two", three:"three"}; } }; // Add/update/delete rows as indicated var handleClick = function() { // Reset sort myDataTable.set("sortedBy", null); var mode = YAHOO.util.Dom.get("mode").value, count = parseInt(YAHOO.util.Dom.get("count").value), index = parseInt(YAHOO.util.Dom.get("index").value); if(YAHOO.lang.isNumber(index)) { switch(mode) { case "add": if(count === 1) { myDataTable.addRow(getData(), index); } else { myDataTable.addRows(getData(count), index); } return; case "update": if(count === 1) { myDataTable.updateRow(index, getData()); } else { myDataTable.updateRows(index, getData(count)); } return; case "deletestandard": if(count === 1) { myDataTable.deleteRow(index); } else { myDataTable.deleteRows(index, count); } return; case "deletereverse": if(count === 1) { myDataTable.deleteRow(index, -1); } else { myDataTable.deleteRows(index, count*-1); } return; default: break; } } YAHOO.log("Could not continue due to invalid index."); } var btn = new YAHOO.widget.Button("go"); btn.on("click", handleClick); return { ds: myDataSource, dt: myDataTable }; }(); });

## Configuration for This Example

You can load the necessary JavaScript and CSS for this example from Yahoo's servers. Click here to load the YUI Dependency Configurator with all of this example's dependencies preconfigured.

### YUI Logger Output:

Note: Logging and debugging is currently turned off for this example.

Reload with logging
and debugging enabled.

### DataTable Control Examples:

- Basic Example
- Progressive Enhancement
- Custom Cell Formatting
- Conditional row coloring
- Nested Headers
- JSON Data Over XHR
- XML Data Over XHR With POST
- XML Data with XPath
- Textual Data Over XHR
- Polling the DataSource
- Adding, Updating, and Deleting Rows
- Client-side Pagination
- Client-side Sorting
- Server-side Pagination and Sorting for Dynamic Data
- Integrating Browser History Manager with Server-side Pagination and Sorting
- XY-scrolling, Y-scrolling, and X-scrolling
- Row Selection
- Cell Selection
- Inline Cell Editing
- Showing, Hiding, and Reordering Columns.
- Highlighting Cells, Rows, or Columns
- Reorder Rows with Drag and Drop
- Row Expansion
- Context Menu Integration
- Configuring the Paginator (included with examples for Paginator)
- TabView Integration
- Complex Example of Multiple Features
- Client-side Filtering of Local Data
- Filtering of Dynamic Data
- Datatable with Autocomplete
- Skinning Model
- Ratings (included with examples for ProgressBar Control)
- YQLDataSource (included with examples for DataSource Utility)
- Complex Application (included with examples for Layout Manager)

### More DataTable Control Resources:

- User's Guide (external)
- API Documentation
- Cheat Sheet PDF (external)

Copyright © 2010 Yahoo! Inc. All rights reserved.

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