JavaScript DHTML/YUI Library/DataTable

Материал из Web эксперт
Перейти к: навигация, поиск

Содержание

Adding, Updating, and Deleting Rows

   <source lang="html4strict">


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head>

   <meta http-equiv="content-type" content="text/html; charset=utf-8">

<title>Adding, Updating, and Deleting Rows</title> <style type="text/css"> /*margin and padding on body element

 can introduce errors in determining
 element position and are not recommended;
 we turn them off as a foundation for YUI
 CSS treatments. */

body {

 margin:0;
 padding:0;

} </style> <link rel="stylesheet" type="text/css" href="yui_2.7.0b-lib/fonts/fonts-min.css" /> <link rel="stylesheet" type="text/css" href="yui_2.7.0b-lib/paginator/assets/skins/sam/paginator.css" /> <link rel="stylesheet" type="text/css" href="yui_2.7.0b-lib/button/assets/skins/sam/button.css" /> <link rel="stylesheet" type="text/css" href="yui_2.7.0b-lib/datatable/assets/skins/sam/datatable.css" /> <script type="text/javascript" src="yui_2.7.0b-lib/yahoo-dom-event/yahoo-dom-event.js"></script> <script type="text/javascript" src="yui_2.7.0b-lib/element/element-min.js"></script> <script type="text/javascript" src="yui_2.7.0b-lib/paginator/paginator-min.js"></script> <script type="text/javascript" src="yui_2.7.0b-lib/button/button-min.js"></script> <script type="text/javascript" src="yui_2.7.0b-lib/dragdrop/dragdrop-min.js"></script> <script type="text/javascript" src="yui_2.7.0b-lib/datasource/datasource-min.js"></script> <script type="text/javascript" src="yui_2.7.0b-lib/datatable/datatable-min.js"></script>


<style type="text/css"> /* custom styles for this example */ .modform {margin-bottom: 1em;} .index {width:5em;} </style>

</head> <body class=" yui-skin-sam">

Adding, Updating, and Deleting Rows

Adding, updating, and deleting row data dynamically.

<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">
  
   
       
           <button type="button">Go!</button>
       
   

</form>

<script type="text/javascript" src="yui_2.7.0b-assets/datatable-assets/js/data.js"></script> <script type="text/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
       };
   }();

}); </script>

</body> </html>


 </source>
   
  

<A href="http://www.wbex.ru/Code/JavaScriptDownload/yui_2.7.0b.zip">yui_2.7.0b.zip( 4,431 k)</a>


Basic inline cell editing features, input validation and click-to-save interactions.

   <source lang="html4strict">

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head>

   <meta http-equiv="content-type" content="text/html; charset=utf-8">

<title>Inline Cell Editing</title> <style type="text/css"> /*margin and padding on body element

 can introduce errors in determining
 element position and are not recommended;
 we turn them off as a foundation for YUI
 CSS treatments. */

body {

 margin:0;
 padding:0;

} </style> <link rel="stylesheet" type="text/css" href="yui_2.7.0b-lib/fonts/fonts-min.css" /> <link rel="stylesheet" type="text/css" href="yui_2.7.0b-lib/calendar/assets/skins/sam/calendar.css" /> <link rel="stylesheet" type="text/css" href="yui_2.7.0b-lib/datatable/assets/skins/sam/datatable.css" /> <script type="text/javascript" src="yui_2.7.0b-lib/yahoo-dom-event/yahoo-dom-event.js"></script> <script type="text/javascript" src="yui_2.7.0b-lib/calendar/calendar-min.js"></script> <script type="text/javascript" src="yui_2.7.0b-lib/element/element-min.js"></script> <script type="text/javascript" src="yui_2.7.0b-lib/datasource/datasource-min.js"></script> <script type="text/javascript" src="yui_2.7.0b-lib/datatable/datatable-min.js"></script>


<style type="text/css"> /* custom styles for this example */ .yui-skin-sam .yui-dt-col-address pre { font-family:arial;font-size:100%; } /* Use PRE in first col to preserve linebreaks*/ </style>

</head> <body class=" yui-skin-sam">

Inline Cell Editing

This example demonstrates basic inline cell editing features, as well as more complex customizations, such as input validation and click-to-save interactions.

<script type="text/javascript" src="yui_2.7.0b-assets/datatable-assets/js/data.js"></script> <script type="text/javascript"> YAHOO.util.Event.addListener(window, "load", function() {

   YAHOO.example.InlineCellEditing = function() {
       // Custom formatter for "address" column to preserve line breaks
       var formatAddress = function(elCell, oRecord, oColumn, oData) {
elCell.innerHTML = "
" + oData + "
";
       };
       var myColumnDefs = [
           {key:"uneditable"},
           {key:"address", formatter:formatAddress, editor: new YAHOO.widget.TextareaCellEditor()},
           {key:"city", editor: new YAHOO.widget.TextboxCellEditor({disableBtns:true})},
           {key:"state", editor: new YAHOO.widget.DropdownCellEditor({dropdownOptions:YAHOO.example.Data.stateAbbrs,disableBtns:true})},
           {key:"amount", editor: new YAHOO.widget.TextboxCellEditor({validator:YAHOO.widget.DataTable.validateNumber})},
           {key:"active", editor: new YAHOO.widget.RadioCellEditor({radioOptions:["yes","no","maybe"],disableBtns:true})},
           {key:"colors", editor: new YAHOO.widget.CheckboxCellEditor({checkboxOptions:["red","yellow","blue"]})},
           {key:"last_login", formatter:YAHOO.widget.DataTable.formatDate, editor: new YAHOO.widget.DateCellEditor()}
       ];
       var myDataSource = new YAHOO.util.DataSource(YAHOO.example.Data.addresses);
       myDataSource.responseType = YAHOO.util.DataSource.TYPE_JSARRAY;
       myDataSource.responseSchema = {
           fields: ["address","city","state","amount","active","colors",{key:"last_login",parser:"date"}]
       };
       var myDataTable = new YAHOO.widget.DataTable("cellediting", myColumnDefs, myDataSource, {});
       // Set up editing flow
       var highlightEditableCell = function(oArgs) {
           var elCell = oArgs.target;
           if(YAHOO.util.Dom.hasClass(elCell, "yui-dt-editable")) {
               this.highlightCell(elCell);
           }
       };
       myDataTable.subscribe("cellMouseoverEvent", highlightEditableCell);
       myDataTable.subscribe("cellMouseoutEvent", myDataTable.onEventUnhighlightCell);
       myDataTable.subscribe("cellClickEvent", myDataTable.onEventShowCellEditor);
       
       return {
           oDS: myDataSource,
           oDT: myDataTable
       };
   }();

}); </script>

</body> </html>


 </source>
   
  

<A href="http://www.wbex.ru/Code/JavaScriptDownload/yui_2.7.0b.zip">yui_2.7.0b.zip( 4,431 k)</a>


Cell-Block Selection Mode with Support for Modifier Keys

   <source lang="html4strict">

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head>

   <meta http-equiv="content-type" content="text/html; charset=utf-8">

<title>Cell Selection</title> <style type="text/css"> /*margin and padding on body element

 can introduce errors in determining
 element position and are not recommended;
 we turn them off as a foundation for YUI
 CSS treatments. */

body {

 margin:0;
 padding:0;

} </style> <link rel="stylesheet" type="text/css" href="yui_2.7.0b-lib/fonts/fonts-min.css" /> <link rel="stylesheet" type="text/css" href="yui_2.7.0b-lib/datatable/assets/skins/sam/datatable.css" /> <script type="text/javascript" src="yui_2.7.0b-lib/yahoo-dom-event/yahoo-dom-event.js"></script> <script type="text/javascript" src="yui_2.7.0b-lib/element/element-min.js"></script> <script type="text/javascript" src="yui_2.7.0b-lib/datasource/datasource-min.js"></script> <script type="text/javascript" src="yui_2.7.0b-lib/datatable/datatable-min.js"></script>


<style type="text/css"> /* custom styles for this example */ .yui-skin-sam .yui-dt-body { cursor:pointer; } /* when cells are selectable */

  1. cellrange, #singlecell { margin-top:2em; }

</style>

</head> <body class=" yui-skin-sam">

Cell Selection

These examples demonstrate "cellblock", "cellrange", and "singlecell" selection modes.

<script type="text/javascript" src="yui_2.7.0b-assets/datatable-assets/js/data.js"></script> <script type="text/javascript"> YAHOO.util.Event.addListener(window, "load", function() {

   YAHOO.example.CellSelection = function() {
       var myColumnDefs = [
           {key:"col1", sortable:true},
           {key:"col2", sortable:true},
           {key:"col3", sortable:true},
           {key:"col4", sortable:true}
       ];
       var myDataSource = new YAHOO.util.DataSource(YAHOO.example.Data.webstats);
       myDataSource.responseType = YAHOO.util.DataSource.TYPE_JSARRAY;
       myDataSource.responseSchema = {
           fields: ["col0","col1","col2","col3","col4"]
       };
       var cellBlockSelectDataTable = new YAHOO.widget.DataTable("cellblock",
               myColumnDefs, myDataSource, {
                   caption:"Cell-Block Selection Mode with Support for Modifier Keys",
                   selectionMode:"cellblock"
               });
       // Subscribe to events for cell selection
       cellBlockSelectDataTable.subscribe("cellMouseoverEvent", cellBlockSelectDataTable.onEventHighlightCell);
       cellBlockSelectDataTable.subscribe("cellMouseoutEvent", cellBlockSelectDataTable.onEventUnhighlightCell);
       cellBlockSelectDataTable.subscribe("cellClickEvent", cellBlockSelectDataTable.onEventSelectCell);
       cellBlockSelectDataTable.subscribe("cellSelectEvent", cellBlockSelectDataTable.clearTextSelection);
       var cellRangeSelectDataTable = new YAHOO.widget.DataTable("cellrange",
               myColumnDefs, myDataSource, {
                   caption:"Example: Cell-Range Selection Mode Support for Modifier Keys",
                   selectionMode:"cellrange"
               });
       // Subscribe to events for cell selection
       cellRangeSelectDataTable.subscribe("cellMouseoverEvent", cellRangeSelectDataTable.onEventHighlightCell);
       cellRangeSelectDataTable.subscribe("cellMouseoutEvent", cellRangeSelectDataTable.onEventUnhighlightCell);
       cellRangeSelectDataTable.subscribe("cellClickEvent", cellRangeSelectDataTable.onEventSelectCell);
       cellRangeSelectDataTable.subscribe("cellSelectEvent", cellRangeSelectDataTable.clearTextSelection);
       var singleCellSelectDataTable = new YAHOO.widget.DataTable("singlecell",
               myColumnDefs, myDataSource, {
                   caption:"Single-Cell Selection Mode with Modifier Keys Disabled",
                   selectionMode:"singlecell"
               });
       // Subscribe to events for cell selection
       singleCellSelectDataTable.subscribe("cellMouseoverEvent", singleCellSelectDataTable.onEventHighlightCell);
       singleCellSelectDataTable.subscribe("cellMouseoutEvent", singleCellSelectDataTable.onEventUnhighlightCell);
       singleCellSelectDataTable.subscribe("cellClickEvent", singleCellSelectDataTable.onEventSelectCell);
       singleCellSelectDataTable.subscribe("cellSelectEvent", singleCellSelectDataTable.clearTextSelection);
       
       return {
           oDS: myDataSource,
           oDTCellBlockSelect: cellBlockSelectDataTable,
           oDTCellRangeSelect: cellRangeSelectDataTable,
           oDTSingleCellSelect: singleCellSelectDataTable
       };
   }();

}); </script>

</body> </html>


 </source>
   
  

<A href="http://www.wbex.ru/Code/JavaScriptDownload/yui_2.7.0b.zip">yui_2.7.0b.zip( 4,431 k)</a>


Client-side Pagination

   <source lang="html4strict">

<head>

   <meta http-equiv="content-type" content="text/html; charset=utf-8">

<title>Client-side Pagination</title> <style type="text/css"> /*margin and padding on body element

 can introduce errors in determining
 element position and are not recommended;
 we turn them off as a foundation for YUI
 CSS treatments. */

body {

 margin:0;
 padding:0;

} </style> <link rel="stylesheet" type="text/css" href="yui_2.7.0b-lib/fonts/fonts-min.css" /> <link rel="stylesheet" type="text/css" href="yui_2.7.0b-lib/paginator/assets/skins/sam/paginator.css" /> <link rel="stylesheet" type="text/css" href="yui_2.7.0b-lib/datatable/assets/skins/sam/datatable.css" /> <script type="text/javascript" src="yui_2.7.0b-lib/yahoo-dom-event/yahoo-dom-event.js"></script> <script type="text/javascript" src="yui_2.7.0b-lib/connection/connection-min.js"></script> <script type="text/javascript" src="yui_2.7.0b-lib/json/json-min.js"></script> <script type="text/javascript" src="yui_2.7.0b-lib/element/element-min.js"></script> <script type="text/javascript" src="yui_2.7.0b-lib/paginator/paginator-min.js"></script> <script type="text/javascript" src="yui_2.7.0b-lib/datasource/datasource-min.js"></script> <script type="text/javascript" src="yui_2.7.0b-lib/datatable/datatable-min.js"></script>


<style type="text/css">

   #paginated {
       text-align: center;
   }
   #paginated table {
       margin-left:auto; margin-right:auto;
   }
   #paginated, #paginated .yui-dt-loading {
       text-align: center; background-color: transparent;
   }

</style>

</head> <body class=" yui-skin-sam">

Client-side Pagination

This example retrieves a large data set in JSON format from a server script, then loads the data into a DataTable with client side pagination enabled.

<script type="text/javascript"> YAHOO.util.Event.onDOMReady(function() {

   YAHOO.example.ClientPagination = function() {
       var myColumnDefs = [
           {key:"id", label:"ID"},
           {key:"name", label:"Name"},
           {key:"date", label:"Date"},
           {key:"price", label:"Price"},
           {key:"number", label:"Number"}
       ];
       var myDataSource = new YAHOO.util.DataSource("yui_2.7.0b-assets/datatable-assets/php/json_proxy.php?");
       myDataSource.responseType = YAHOO.util.DataSource.TYPE_JSON;
       myDataSource.responseSchema = {
           resultsList: "records",
           fields: ["id","name","date","price","number"]
       };
       var oConfigs = {
               paginator: new YAHOO.widget.Paginator({
                   rowsPerPage: 15
               }),
               initialRequest: "results=504"
       };
       var myDataTable = new YAHOO.widget.DataTable("paginated", myColumnDefs,
               myDataSource, oConfigs);
               
       return {
           oDS: myDataSource,
           oDT: myDataTable
       };
   }();

}); </script> </body> </html>

</body>

 </source>
   
  

<A href="http://www.wbex.ru/Code/JavaScriptDownload/yui_2.7.0b.zip">yui_2.7.0b.zip( 4,431 k)</a>


Client-side Sorting

   <source lang="html4strict">


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head>

   <meta http-equiv="content-type" content="text/html; charset=utf-8">

<title>Client-side Sorting</title> <style type="text/css"> /*margin and padding on body element

 can introduce errors in determining
 element position and are not recommended;
 we turn them off as a foundation for YUI
 CSS treatments. */

body {

 margin:0;
 padding:0;

} </style> <link rel="stylesheet" type="text/css" href="yui_2.7.0b-lib/fonts/fonts-min.css" /> <link rel="stylesheet" type="text/css" href="yui_2.7.0b-lib/datatable/assets/skins/sam/datatable.css" /> <script type="text/javascript" src="yui_2.7.0b-lib/yahoo-dom-event/yahoo-dom-event.js"></script> <script type="text/javascript" src="yui_2.7.0b-lib/element/element-min.js"></script> <script type="text/javascript" src="yui_2.7.0b-lib/datasource/datasource-min.js"></script> <script type="text/javascript" src="yui_2.7.0b-lib/datatable/datatable-min.js"></script>

</head> <body class=" yui-skin-sam">

Client-side Sorting

A custom sort handler has been defined in this example to enable custom nested sorting, such that clicking on the "States" Column will sort by states, and then by area code.

<script type="text/javascript" src="yui_2.7.0b-assets/datatable-assets/js/data.js"></script> <script type="text/javascript"> YAHOO.util.Event.addListener(window, "load", function() {

   YAHOO.example.CustomSort = function() {
       // Custom sort handler to sort by state and then by areacode
       // where a and b are Record instances to compare
       var sortStates = function(a, b, desc) {
           // Deal with empty values
           if(!YAHOO.lang.isValue(a)) {
               return (!YAHOO.lang.isValue(b)) ? 0 : 1;
           }
           else if(!YAHOO.lang.isValue(b)) {
               return -1;
           }
           // First compare by state
           var comp = YAHOO.util.Sort.rupare;
           var compState = comp(a.getData("state"), b.getData("state"), desc);
           // If states are equal, then compare by areacode
           return (compState !== 0) ? compState : comp(a.getData("areacode"), b.getData("areacode"), desc);
       };
       var myColumnDefs = [
           {key:"areacode",label:"Area Codes",sortable:true},
           {key:"state",label:"States",sortable:true,sortOptions:{sortFunction:sortStates}}
       ];
       var myDataSource = new YAHOO.util.DataSource(YAHOO.example.Data.areacodes.slice(0,25));
       myDataSource.responseType = YAHOO.util.DataSource.TYPE_JSARRAY;
       myDataSource.responseSchema = {
           fields: ["areacode","state"]
       };
       var myDataTable = new YAHOO.widget.DataTable("sort", myColumnDefs,
               myDataSource, {sortedBy:{key:"areacode", dir:"asc"}});
               
       return {
           oDS: myDataSource,
           oDT: myDataTable
       };
   }();

}); </script>

</body> </html>


 </source>
   
  

<A href="http://www.wbex.ru/Code/JavaScriptDownload/yui_2.7.0b.zip">yui_2.7.0b.zip( 4,431 k)</a>


Conditional row coloring

   <source lang="html4strict">


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head>

   <meta http-equiv="content-type" content="text/html; charset=utf-8">

<title>Conditional row coloring</title> <style type="text/css"> /*margin and padding on body element

 can introduce errors in determining
 element position and are not recommended;
 we turn them off as a foundation for YUI
 CSS treatments. */

body {

 margin:0;
 padding:0;

} </style> <link rel="stylesheet" type="text/css" href="yui_2.7.0b-lib/fonts/fonts-min.css" /> <link rel="stylesheet" type="text/css" href="yui_2.7.0b-lib/datatable/assets/skins/sam/datatable.css" /> <script type="text/javascript" src="yui_2.7.0b-lib/yahoo-dom-event/yahoo-dom-event.js"></script> <script type="text/javascript" src="yui_2.7.0b-lib/element/element-min.js"></script> <script type="text/javascript" src="yui_2.7.0b-lib/datasource/datasource-min.js"></script> <script type="text/javascript" src="yui_2.7.0b-lib/datatable/datatable-min.js"></script>


<style type="text/css"> /* Remove row striping, column borders, and sort highlighting */ .yui-skin-sam tr.yui-dt-odd, .yui-skin-sam tr.yui-dt-odd td.yui-dt-asc, .yui-skin-sam tr.yui-dt-odd td.yui-dt-desc, .yui-skin-sam tr.yui-dt-even td.yui-dt-asc, .yui-skin-sam tr.yui-dt-even td.yui-dt-desc {

   background-color: #fff;

} .yui-skin-sam .yui-dt tbody td {

   border-bottom: 1px solid #ddd;

} .yui-skin-sam .yui-dt thead th {

   border-bottom: 1px solid #7f7f7f;

} .yui-skin-sam .yui-dt tr.yui-dt-last td, .yui-skin-sam .yui-dt th, .yui-skin-sam .yui-dt td {

   border: none;

} /* Class for marked rows */ .yui-skin-sam .yui-dt tr.mark, .yui-skin-sam .yui-dt tr.mark td.yui-dt-asc, .yui-skin-sam .yui-dt tr.mark td.yui-dt-desc, .yui-skin-sam .yui-dt tr.mark td.yui-dt-asc, .yui-skin-sam .yui-dt tr.mark td.yui-dt-desc {

   background-color: #a33;
   color: #fff;

} </style>

</head> <body class=" yui-skin-sam">

Conditional row coloring

This example demonstrates how to color DataTable rows based on data in a Record. In this case, rows with Quantity less than 40 are highlighted.

<script type="text/javascript" src="yui_2.7.0b-assets/datatable-assets/js/data.js"></script> <script type="text/javascript"> YAHOO.util.Event.onDOMReady(function () { // Create a shortcut var Dom = YAHOO.util.Dom; // Contain our code under the YAHOO.example namespace var Ex = YAHOO.example; // Create the DataSource Ex.dataSource = new YAHOO.util.DataSource(Ex.Data.inventory); Ex.dataSource.responseType = YAHOO.util.DataSource.TYPE_JSARRAY; Ex.dataSource.responseSchema = {

   fields : ["SKU","Quantity","Item","Description"]

}; // Define a custom row formatter function var myRowFormatter = function(elTr, oRecord) {

   if (oRecord.getData("Quantity") < 40) {
       Dom.addClass(elTr, "mark");
   }
   return true;

}; // Instantiate the DataTable. Ex.dataTable = new YAHOO.widget.DataTable("tbl",

               [ {key:"SKU",sortable: true},
                 {key:"Item",sortable: true},
                 {key:"Quantity",sortable: true},
                 {key:"Description",sortable: true}
               ],
               Ex.dataSource,
               {formatRow: myRowFormatter}); // Enable the row formatter

}); </script>

</body> </html>


 </source>
   
  

<A href="http://www.wbex.ru/Code/JavaScriptDownload/yui_2.7.0b.zip">yui_2.7.0b.zip( 4,431 k)</a>


Create a DataTable instance based on markup that already exists on the page. By progressively enhancing markup with higher order functionality.

   <source lang="html4strict">

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head>

   <meta http-equiv="content-type" content="text/html; charset=utf-8">

<title>Progressive Enhancement</title> <style type="text/css"> /*margin and padding on body element

 can introduce errors in determining
 element position and are not recommended;
 we turn them off as a foundation for YUI
 CSS treatments. */

body {

 margin:0;
 padding:0;

} </style> <link rel="stylesheet" type="text/css" href="yui_2.7.0b-lib/fonts/fonts-min.css" /> <link rel="stylesheet" type="text/css" href="yui_2.7.0b-lib/datatable/assets/skins/sam/datatable.css" /> <script type="text/javascript" src="yui_2.7.0b-lib/yahoo-dom-event/yahoo-dom-event.js"></script> <script type="text/javascript" src="yui_2.7.0b-lib/element/element-min.js"></script> <script type="text/javascript" src="yui_2.7.0b-lib/datasource/datasource-min.js"></script> <script type="text/javascript" src="yui_2.7.0b-lib/datatable/datatable-min.js"></script>

</head> <body class=" yui-skin-sam">

Progressive Enhancement

This example creates a DataTable instance based on markup that already exists on the page. By progressively enhancing markup with higher order functionality, users who do not have JavaScript enabled are still able to view the page"s content and experience core functionality.

In this example"s code, note that we listen for the window "load" event before calling our function to be sure that the original table markup is fully rendered and available as a DataSource source for our DataTable instance.

<thead> </thead> <tbody> </tbody>
Due Date Account Number Quantity Amount Due
1/23/1999 29e8548592d8c82 12 $150.00
5/19/1999 83849 8 $60.00
8/9/1999 11348 1 $34.99
1/23/2000 29e8548592d8c82 10 $1.00
4/28/2000 37892857482836437378273 123 $33.32
1/23/2001 83849 5 $15.00
9/30/2001 224747 14 $56.78

<script type="text/javascript"> YAHOO.util.Event.addListener(window, "load", function() {

   YAHOO.example.EnhanceFromMarkup = function() {
       var myColumnDefs = [
           {key:"due",label:"Due Date",formatter:YAHOO.widget.DataTable.formatDate,sortable:true},
           {key:"account",label:"Account Number", sortable:true},
           {key:"quantity",label:"Quantity",formatter:YAHOO.widget.DataTable.formatNumber,sortable:true},
           {key:"amount",label:"Amount Due",formatter:YAHOO.widget.DataTable.formatCurrency,sortable:true}
       ];
       var parseNumberFromCurrency = function(sString) {
           // Remove dollar sign and make it a float
           return parseFloat(sString.substring(1));
       };
       var myDataSource = new YAHOO.util.DataSource(YAHOO.util.Dom.get("accounts"));
       myDataSource.responseType = YAHOO.util.DataSource.TYPE_HTMLTABLE;
       myDataSource.responseSchema = {
           fields: [{key:"due", parser:"date"},
                   {key:"account"},
                   {key:"quantity", parser:"number"},
                   {key:"amount", parser:parseNumberFromCurrency} // point to a custom parser
           ]
       };
       var myDataTable = new YAHOO.widget.DataTable("markup", myColumnDefs, myDataSource,
               {caption:"Example: Progressively Enhanced Table from Markup",
               sortedBy:{key:"due",dir:"desc"}}
       );
       
       return {
           oDS: myDataSource,
           oDT: myDataTable
       };
   }();

}); </script>

</body> </html>


 </source>
   
  

<A href="http://www.wbex.ru/Code/JavaScriptDownload/yui_2.7.0b.zip">yui_2.7.0b.zip( 4,431 k)</a>


Custom Cell Formatting

   <source lang="html4strict">


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head>

   <style type="text/css">
       .yui-skin-sam .yui-dt tbody td.up {
           background-color: #efe;
       }
       .yui-skin-sam .yui-dt tbody td.down {
           background-color: #fee;
       }
   </style>

</head> <body class="yui-skin-sam">

Custom Cell Formatting

<script type="text/javascript" src="yui_2.7.0b-lib/yuiloader/yuiloader.js"></script> <script type="text/javascript" src="yui_2.7.0b-assets/datatable-assets/js/data.js"></script> <script type="text/javascript"> var loader = new YAHOO.util.YUILoader(); loader.insert({

   require: ["fonts", "datatable", "json"],
   filter: "debug",
   base: "yui_2.7.0b-lib/",
   onSuccess: function() {
       YAHOO.example.CustomFormatting = function() {
           // Define a custom formatter for the Column labeled "flag"
           // draws an up icon and adds class "up" to the cell liner to affect
           // a green background color if value of field3 is greater than 100.
           // Otherwise renders a down icon and assigns class "down", setting
           // the background color to red.
           var myCustomFormatter = function(elCell, oRecord, oColumn, oData) {
               if(oRecord.getData("field3") > 100) {
                   YAHOO.util.Dom.replaceClass(elCell.parentNode, "down", "up");
                   elCell.innerHTML = " <img src="yui_2.7.0b-lib/datatable/assets/skins/sam/dt-arrow-up.png">";
               }
               else {
                   YAHOO.util.Dom.replaceClass(elCell.parentNode, "up","down");
                   elCell.innerHTML = " <img src="yui_2.7.0b-lib/datatable/assets/skins/sam/dt-arrow-dn.png">";
               }
           };
           
           // Add the custom formatter to the shortcuts
           YAHOO.widget.DataTable.Formatter.myCustom = myCustomFormatter;
           // Override the built-in formatter
           YAHOO.widget.DataTable.formatEmail = function(elCell, oRecord, oColumn, oData) {
               var user = oData;
               elCell.innerHTML = "<a href=\"mailto:" + user + "@mycompany.ru\">" + user + "</a>";
           };
           
           var myColumnDefs = [
               {key:"flag", formatter:"myCustom"}, // use custom shortcut
               {key:"radio", formatter:"radio"}, // use the built-in radio formatter
               {key:"check", formatter:"checkbox"}, // use the built-in checkbox formatter (shortcut)
               {key:"button", label:"Show record data", formatter:YAHOO.widget.DataTable.formatButton}, // use the built-in button formatter
               {key:"field1", formatter:"dropdown", dropdownOptions:["apples","bananas","cherries"],sortable:true},
               {key:"field2", sortable:true, formatter:"date"}, // use the built-in date formatter
               {key:"field3", sortable:true, formatter:"number"},
               {key:"field4", sortable:true, formatter:"currency"}, // use the built-in currency formatter
               {key:"field5", sortable:true, formatter:YAHOO.widget.DataTable.formatEmail}, // use the overridden email formatter
               {key:"field6", sortable:true, formatter:YAHOO.widget.DataTable.formatLink} // use the built-in link formatter
           ];
           var myDataSource = new YAHOO.util.DataSource(YAHOO.example.Data.multitypes);
           myDataSource.responseType = YAHOO.util.DataSource.TYPE_JSON;
           myDataSource.responseSchema = {
               resultsList: "items",
               // Use the parse methods to populate the RecordSet with the right data types
               fields: [
                   {key:"field1", parser:"string"}, // point to the string parser
                   {key:"field2", parser:"date"}, // point to the date parser
                   {key:"field3", parser:"number"}, // point to the number parser
                   {key:"field4", parser:"number"}, // point to the number parser
                   {key:"field5"}, // this is already string data so no parser needed
                   {key:"field6"} // this is already string data so no parser needed
               ]
           };
           var myDataTable = new YAHOO.widget.DataTable("formatting", myColumnDefs, myDataSource);
           var lastSelectedRadioRecord = null;
           myDataTable.subscribe("radioClickEvent", function(oArgs){
               if(lastSelectedRadioRecord) {
                   lastSelectedRadioRecord.setData("radio",false);
               }
               var elRadio = oArgs.target;
               var oRecord = this.getRecord(elRadio);
               oRecord.setData("radio",true);
               lastSelectedRadioRecord = oRecord;
               var name = oRecord.getData("field5");
           });
           myDataTable.subscribe("checkboxClickEvent", function(oArgs){
               var elCheckbox = oArgs.target;
               var oRecord = this.getRecord(elCheckbox);
               oRecord.setData("check",elCheckbox.checked);
           });
           myDataTable.subscribe("buttonClickEvent", function(oArgs){
               var oRecord = this.getRecord(oArgs.target);
               alert(YAHOO.lang.dump(oRecord.getData()));
           });
           myDataTable.subscribe("dropdownChangeEvent", function(oArgs){
               var elDropdown = oArgs.target;
               var oRecord = this.getRecord(elDropdown);
               oRecord.setData("field1",elDropdown.options[elDropdown.selectedIndex].value);
           });
           
           return {
               oDS: myDataSource,
               oDT: myDataTable
           };
       }();
   }

}); </script> </body> </html>


 </source>
   
  

<A href="http://www.wbex.ru/Code/JavaScriptDownload/yui_2.7.0b.zip">yui_2.7.0b.zip( 4,431 k)</a>


DataTable"s basic feature set.

   <source lang="html4strict">


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head>

   <meta http-equiv="content-type" content="text/html; charset=utf-8">

<title>Basic Example</title> <style type="text/css"> /*margin and padding on body element

 can introduce errors in determining
 element position and are not recommended;
 we turn them off as a foundation for YUI
 CSS treatments. */

body {

 margin:0;
 padding:0;

} </style> <link rel="stylesheet" type="text/css" href="yui_2.7.0b-lib/fonts/fonts-min.css" /> <link rel="stylesheet" type="text/css" href="yui_2.7.0b-lib/datatable/assets/skins/sam/datatable.css" /> <script type="text/javascript" src="yui_2.7.0b-lib/yahoo-dom-event/yahoo-dom-event.js"></script> <script type="text/javascript" src="yui_2.7.0b-lib/dragdrop/dragdrop-min.js"></script> <script type="text/javascript" src="yui_2.7.0b-lib/element/element-min.js"></script> <script type="text/javascript" src="yui_2.7.0b-lib/datasource/datasource-min.js"></script> <script type="text/javascript" src="yui_2.7.0b-lib/datatable/datatable-min.js"></script>


<style type="text/css"> /* custom styles for this example */ .yui-skin-sam .yui-dt-liner { white-space:nowrap; } </style>

</head> <body class=" yui-skin-sam">

Basic Example

A demonstration of the DataTable"s basic feature set.

<script type="text/javascript" src="yui_2.7.0b-assets/datatable-assets/js/data.js"></script> <script type="text/javascript"> YAHOO.util.Event.addListener(window, "load", function() {

   YAHOO.example.Basic = function() {
       var myColumnDefs = [
           {key:"id", sortable:true, resizeable:true},
           {key:"date", formatter:YAHOO.widget.DataTable.formatDate, sortable:true, sortOptions:{defaultDir:YAHOO.widget.DataTable.CLASS_DESC},resizeable:true},
           {key:"quantity", formatter:YAHOO.widget.DataTable.formatNumber, sortable:true, resizeable:true},
           {key:"amount", formatter:YAHOO.widget.DataTable.formatCurrency, sortable:true, resizeable:true},
           {key:"title", sortable:true, resizeable:true}
       ];
       var myDataSource = new YAHOO.util.DataSource(YAHOO.example.Data.bookorders);
       myDataSource.responseType = YAHOO.util.DataSource.TYPE_JSARRAY;
       myDataSource.responseSchema = {
           fields: ["id","date","quantity","amount","title"]
       };
       var myDataTable = new YAHOO.widget.DataTable("basic",
               myColumnDefs, myDataSource, {caption:"DataTable Caption"});
               
       return {
           oDS: myDataSource,
           oDT: myDataTable
       };
   }();

}); </script>

</body> </html>


 </source>
   
  

<A href="http://www.wbex.ru/Code/JavaScriptDownload/yui_2.7.0b.zip">yui_2.7.0b.zip( 4,431 k)</a>


DataTable scroll: XY-scrolling, Y-scrolling, and X-scrolling

   <source lang="html4strict">


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head>

   <meta http-equiv="content-type" content="text/html; charset=utf-8">

<title>XY-scrolling, Y-scrolling, and X-scrolling</title> <style type="text/css"> /*margin and padding on body element

 can introduce errors in determining
 element position and are not recommended;
 we turn them off as a foundation for YUI
 CSS treatments. */

body {

 margin:0;
 padding:0;

} </style> <link rel="stylesheet" type="text/css" href="yui_2.7.0b-lib/fonts/fonts-min.css" /> <link rel="stylesheet" type="text/css" href="yui_2.7.0b-lib/button/assets/skins/sam/button.css" /> <link rel="stylesheet" type="text/css" href="yui_2.7.0b-lib/datatable/assets/skins/sam/datatable.css" /> <script type="text/javascript" src="yui_2.7.0b-lib/yahoo-dom-event/yahoo-dom-event.js"></script> <script type="text/javascript" src="yui_2.7.0b-lib/element/element-min.js"></script> <script type="text/javascript" src="yui_2.7.0b-lib/button/button-min.js"></script> <script type="text/javascript" src="yui_2.7.0b-lib/datasource/datasource-min.js"></script> <script type="text/javascript" src="yui_2.7.0b-lib/datatable/datatable-min.js"></script>

</head> <body class=" yui-skin-sam">

XY-scrolling, Y-scrolling, and X-scrolling

Scrolling can be enabled along the x, y, or xy-axes. Call the YAHOO.widget.ScrollingDataTable constructor and pass in width and/and or height string values as configurations. Since scrolling functionality has been moved to the ScrollingDataTable subclass, please note that you cannot start with a YAHOO.widget.DataTable instance and add scrolling to it dynamically at runtime.

XY Scrolling

Y Scrolling

X Scrolling

<script type="text/javascript" src="yui_2.7.0b-assets/datatable-assets/js/data.js"></script> <script type="text/javascript"> YAHOO.util.Event.addListener(window, "load", function() {

   YAHOO.example.Scrolling = function() {
       var myColumnDefs = [
               {key:"field1", width:50},
               {key:"field2", width:100, formatter:"date"},
               {key:"field3", width:50},
               {key:"field4", width:50},
               {key:"field5", width:50},
               {key:"field6", width:150}
           ];
           
       var myColumnDefsY = [
               {key:"field1", width:50},
               {key:"field2", width:100, formatter:"date"},
               {key:"field3", width:50},
               {key:"field4", width:50},
               {key:"field5", width:50},
               {key:"field6", width:150}
           ];
       var myColumnDefsX = [
               {key:"field1", width:50},
               {key:"field2", width:100, formatter:"date"},
               {key:"field3", width:50},
               {key:"field4", width:50},
               {key:"field5", width:50},
               {key:"field6", width:150}
           ];
       var myDataSource = new YAHOO.util.DataSource(YAHOO.example.Data.multitypes);
       myDataSource.responseType = YAHOO.util.DataSource.TYPE_JSON;
       myDataSource.responseSchema = {
           resultsList: "items",
           fields: [
               {key:"field1"},
               {key:"field2", formatter:"date"},
               {key:"field3"},
               {key:"field4"},
               {key:"field5"},
               {key:"field6"}
           ]
       };
       // Set width and height as string values
       var myDataTableXY = new YAHOO.widget.ScrollingDataTable("xyscrolling", myColumnDefs,
               myDataSource, {width:"30em", height:"10em"});
       // Set height as a string value
       var myDataTableY = new YAHOO.widget.ScrollingDataTable("yscrolling", myColumnDefsY,
               myDataSource, {height:"10em"});
       // Set width as a string value
       var myDataTableX = new YAHOO.widget.ScrollingDataTable("xscrolling", myColumnDefsX,
               myDataSource, {width:"30em"});
               
       return {
           oDS: myDataSource,
           oDTXY: myDataTableXY,
           oDTY: myDataTableY,
           oDTX: myDataTableX
       };
   }();

}); </script>

</body> </html>


 </source>
   
  

<A href="http://www.wbex.ru/Code/JavaScriptDownload/yui_2.7.0b.zip">yui_2.7.0b.zip( 4,431 k)</a>


Datatable with Autocomplete

   <source lang="html4strict">


<head>

   <meta http-equiv="content-type" content="text/html; charset=utf-8">

<title>Datatable with Autocomplete</title> <style type="text/css"> /*margin and padding on body element

 can introduce errors in determining
 element position and are not recommended;
 we turn them off as a foundation for YUI
 CSS treatments. */

body {

 margin:0;
 padding:0;

} </style> <link rel="stylesheet" type="text/css" href="yui_2.7.0b-lib/fonts/fonts-min.css" /> <link rel="stylesheet" type="text/css" href="yui_2.7.0b-lib/autocomplete/assets/skins/sam/autocomplete.css" /> <link rel="stylesheet" type="text/css" href="yui_2.7.0b-lib/datatable/assets/skins/sam/datatable.css" /> <script type="text/javascript" src="yui_2.7.0b-lib/yahoo-dom-event/yahoo-dom-event.js"></script> <script type="text/javascript" src="yui_2.7.0b-lib/connection/connection-min.js"></script> <script type="text/javascript" src="yui_2.7.0b-lib/json/json-min.js"></script> <script type="text/javascript" src="yui_2.7.0b-lib/datasource/datasource-min.js"></script> <script type="text/javascript" src="yui_2.7.0b-lib/autocomplete/autocomplete-min.js"></script> <script type="text/javascript" src="yui_2.7.0b-lib/element/element-min.js"></script> <script type="text/javascript" src="yui_2.7.0b-lib/datatable/datatable-min.js"></script>

</head> <body class=" yui-skin-sam">

Datatable with Autocomplete

This example uses two <a href="http://developer.yahoo.ru/yui/autocomplete/">AutoComplete Controls</a> to populate a DataTable with data received via XHR from the Yahoo! Local webservice.

<style type="text/css">

   #autocomplete, #autocomplete_zip {
       height: 25px;
   }
   #dt_input, #dt_input_zip {
       position: static;
       width: 300px;
   }
   #dt_input_zip {
       width: 60px;
   }
   #dt_ac_container, #dt_ac_zip_container {
       display: none;
   }

</style>

   <label for="dt_input">Search Term: </label><input id="dt_input" type="text" value="pizza">
   <label for="dt_input_zip">Zip Code: </label><input id="dt_input_zip" type="text" value="94089">

<script type="text/javascript"> (function() {

   var Dom = YAHOO.util.Dom,
   Event = YAHOO.util.Event,
   queryString = "&results=20&output=json",
   zip = null,
   myDataSource = null,
   myDataTable = null;
   var getZip = function(query) {
       query = parseInt(query, 10);
       if (!YAHOO.lang.isNumber(query)) {
           query = zip;
           Dom.get("dt_input_zip").value = zip;
           YAHOO.log("Invalid zip code, must be a number", "warn", "example");
       }
       myDataSource.sendRequest("datatable=yes&zip=" + query + "&query=" + Dom.get("dt_input").value + queryString,
           myDataTable.onDataReturnInitializeTable, myDataTable);        
   };
   var getTerms = function(query) {
       myDataSource.sendRequest("datatable=yes&query=" + query + "&zip=" + Dom.get("dt_input_zip").value + queryString,
       myDataTable.onDataReturnInitializeTable, myDataTable);
   };
   Event.onDOMReady(function() {
       zip = Dom.get("dt_input_zip").value;
       
       var oACDS = new YAHOO.util.FunctionDataSource(getTerms);
       oACDS.queryMatchContains = true;
       var oAutoComp = new YAHOO.widget.AutoComplete("dt_input","dt_ac_container", oACDS);
       
       var oACDSZip = new YAHOO.util.FunctionDataSource(getZip);
       oACDSZip.queryMatchContains = true;
       var oAutoCompZip = new YAHOO.widget.AutoComplete("dt_input_zip","dt_ac_zip_container", oACDSZip);
       //Don"t query until we have 5 numbers for the zip code
       oAutoCompZip.minQueryLength = 5;
       var formatUrl = function(elCell, oRecord, oColumn, sData) {
           elCell.innerHTML = "<a href="" + oRecord.getData("ClickUrl") + "" target="_blank">" + sData + "</a>";
       };
       var myColumnDefs = [
           { key:"Title",
               label:"Name",
               sortable:true,
               formatter: formatUrl
           },
           { key:"Phone" },
           { key:"City" },
           { key:"Rating.AverageRating",
               label:"Rating",
               formatter:YAHOO.widget.DataTable.formatNumber, 
               sortable:true
           }
       ];
       myDataSource = new YAHOO.util.DataSource("yui_2.7.0b-assets/datatable-assets/php/ylocal_proxy.php?");
       myDataSource.responseType = YAHOO.util.DataSource.TYPE_JSON;
       myDataSource.connXhrMode = "queueRequests";
       myDataSource.responseSchema = {
           resultsList: "ResultSet.Result",
           fields: [
               "Title",
               "Phone",
               "City",
               {
                   key: "Rating.AverageRating",
                   parser:"number"
               },
               "ClickUrl"
           ]
       };
       myDataTable = new YAHOO.widget.DataTable("json", myColumnDefs,
           myDataSource, {initialRequest: "datatable=yes&query=" + Dom.get("dt_input").value + "&zip=" + Dom.get("dt_input_zip").value + queryString });
   });

})(); </script>

</body>


 </source>
   
  

<A href="http://www.wbex.ru/Code/JavaScriptDownload/yui_2.7.0b.zip">yui_2.7.0b.zip( 4,431 k)</a>


Double click to popup the cell editor

   <source lang="html4strict">

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head>

   <meta http-equiv="content-type" content="text/html; charset=utf-8">

<title>Complex Example of Multiple Features</title> <style type="text/css"> /*margin and padding on body element

 can introduce errors in determining
 element position and are not recommended;
 we turn them off as a foundation for YUI
 CSS treatments. */

body {

 margin:0;
 padding:0;

} </style> <link rel="stylesheet" type="text/css" href="yui_2.7.0b-lib/fonts/fonts-min.css" /> <link rel="stylesheet" type="text/css" href="yui_2.7.0b-lib/paginator/assets/skins/sam/paginator.css" /> <link rel="stylesheet" type="text/css" href="yui_2.7.0b-lib/datatable/assets/skins/sam/datatable.css" /> <script type="text/javascript" src="yui_2.7.0b-lib/yahoo-dom-event/yahoo-dom-event.js"></script> <script type="text/javascript" src="yui_2.7.0b-lib/dragdrop/dragdrop-min.js"></script> <script type="text/javascript" src="yui_2.7.0b-lib/animation/animation-min.js"></script> <script type="text/javascript" src="yui_2.7.0b-lib/element/element-min.js"></script> <script type="text/javascript" src="yui_2.7.0b-lib/paginator/paginator-min.js"></script> <script type="text/javascript" src="yui_2.7.0b-lib/datasource/datasource-min.js"></script> <script type="text/javascript" src="yui_2.7.0b-lib/datatable/datatable-min.js"></script>


</head> <body class=" yui-skin-sam">

Complex Example of Multiple Features

A demonstration of several DataTable features combined in one instance. The features implemented in this example require the Drag and Drop and Animation utilities.

<script type="text/javascript" src="yui_2.7.0b-assets/datatable-assets/js/data.js"></script> <script type="text/javascript"> YAHOO.util.Event.addListener(window, "load", function() {

   YAHOO.example.MultipleFeatures = function() {
       // Custom sort handler to sort by state and then by areacode
       // where a and b are Record instances to compare
       var sortStates = function(a, b, desc) {
           // Deal with empty values
           if(!YAHOO.lang.isValue(a)) {
               return (!YAHOO.lang.isValue(b)) ? 0 : 1;
           }
           else if(!YAHOO.lang.isValue(b)) {
               return -1;
           }
           // First compare by state
           var comp = YAHOO.util.Sort.rupare;
           var compState = comp(a.getData("state"), b.getData("state"), desc);
           // If states are equal, then compare by areacode
           return (compState !== 0) ? compState : comp(a.getData("areacode"), b.getData("areacode"), desc);
       };
       var myColumnDefs = [
           {key:"areacode",label:"Area Codes",width:100,resizeable:true,sortable:true},
           {key:"state",label:"States",width:250,resizeable:true,sortable:true,
                   sortOptions:{sortFunction:sortStates}},
           {key:"notes",label:"Notes (editable)",editor:"textbox",resizeable:true,sortable:true}
       ];
       var myDataSource = new YAHOO.util.DataSource(YAHOO.example.Data.areacodes);
       myDataSource.responseType = YAHOO.util.DataSource.TYPE_JSARRAY;
       myDataSource.responseSchema = {
           fields: ["areacode","state"]
       };
       var myConfigs = {
           sortedBy:{key:"areacode",dir:"asc"},
           paginator: new YAHOO.widget.Paginator({
               rowsPerPage: 25,
               template: YAHOO.widget.Paginator.TEMPLATE_ROWS_PER_PAGE,
               rowsPerPageOptions: [10,25,50,100],
               pageLinks: 5
           }),
           draggableColumns:true
       }
       var myDataTable = new YAHOO.widget.DataTable("complex", myColumnDefs, myDataSource, myConfigs);
       myDataTable.subscribe("rowClickEvent",myDataTable.onEventSelectRow);
       myDataTable.subscribe("cellDblclickEvent",myDataTable.onEventShowCellEditor);
       myDataTable.subscribe("editorBlurEvent", myDataTable.onEventSaveCellEditor);
       // When cell is edited, pulse the color of the row yellow
       var onCellEdit = function(oArgs) {
           var elCell = oArgs.editor.getTdEl();
           var oOldData = oArgs.oldData;
           var oNewData = oArgs.newData;
           // Grab the row el and the 2 colors
           var elRow = this.getTrEl(elCell);
           var origColor = YAHOO.util.Dom.getStyle(elRow.cells[0], "backgroundColor");
           var pulseColor = "#ff0";
           // Create a temp anim instance that nulls out when anim is complete
           var rowColorAnim = new YAHOO.util.ColorAnim(elRow.cells, {
                   backgroundColor:{to:origColor, from:pulseColor}, duration:2});
           var onComplete = function() {
               rowColorAnim = null;
               YAHOO.util.Dom.setStyle(elRow.cells, "backgroundColor", "");
           }
           rowColorAnim.onComplete.subscribe(onComplete);
           rowColorAnim.animate();
       }
       myDataTable.subscribe("editorSaveEvent", onCellEdit);
       
       return {
           oDS: myDataSource,
           oDT: myDataTable
       };
   }();

}); </script>

</body> </html>


 </source>
   
  

<A href="http://www.wbex.ru/Code/JavaScriptDownload/yui_2.7.0b.zip">yui_2.7.0b.zip( 4,431 k)</a>


Example: Cell-Range Selection Mode Support for Modifier Keys

   <source lang="html4strict">


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head>

   <meta http-equiv="content-type" content="text/html; charset=utf-8">

<title>Cell Selection</title> <style type="text/css"> /*margin and padding on body element

 can introduce errors in determining
 element position and are not recommended;
 we turn them off as a foundation for YUI
 CSS treatments. */

body {

 margin:0;
 padding:0;

} </style> <link rel="stylesheet" type="text/css" href="yui_2.7.0b-lib/fonts/fonts-min.css" /> <link rel="stylesheet" type="text/css" href="yui_2.7.0b-lib/datatable/assets/skins/sam/datatable.css" /> <script type="text/javascript" src="yui_2.7.0b-lib/yahoo-dom-event/yahoo-dom-event.js"></script> <script type="text/javascript" src="yui_2.7.0b-lib/element/element-min.js"></script> <script type="text/javascript" src="yui_2.7.0b-lib/datasource/datasource-min.js"></script> <script type="text/javascript" src="yui_2.7.0b-lib/datatable/datatable-min.js"></script>


<style type="text/css"> /* custom styles for this example */ .yui-skin-sam .yui-dt-body { cursor:pointer; } /* when cells are selectable */

  1. cellrange, #singlecell { margin-top:2em; }

</style>

</head> <body class=" yui-skin-sam">

Cell Selection

These examples demonstrate "cellblock", "cellrange", and "singlecell" selection modes.

<script type="text/javascript" src="yui_2.7.0b-assets/datatable-assets/js/data.js"></script> <script type="text/javascript"> YAHOO.util.Event.addListener(window, "load", function() {

   YAHOO.example.CellSelection = function() {
       var myColumnDefs = [
           {key:"col1", sortable:true},
           {key:"col2", sortable:true},
           {key:"col3", sortable:true},
           {key:"col4", sortable:true}
       ];
       var myDataSource = new YAHOO.util.DataSource(YAHOO.example.Data.webstats);
       myDataSource.responseType = YAHOO.util.DataSource.TYPE_JSARRAY;
       myDataSource.responseSchema = {
           fields: ["col0","col1","col2","col3","col4"]
       };
       var cellBlockSelectDataTable = new YAHOO.widget.DataTable("cellblock",
               myColumnDefs, myDataSource, {
                   caption:"Cell-Block Selection Mode with Support for Modifier Keys",
                   selectionMode:"cellblock"
               });
       // Subscribe to events for cell selection
       cellBlockSelectDataTable.subscribe("cellMouseoverEvent", cellBlockSelectDataTable.onEventHighlightCell);
       cellBlockSelectDataTable.subscribe("cellMouseoutEvent", cellBlockSelectDataTable.onEventUnhighlightCell);
       cellBlockSelectDataTable.subscribe("cellClickEvent", cellBlockSelectDataTable.onEventSelectCell);
       cellBlockSelectDataTable.subscribe("cellSelectEvent", cellBlockSelectDataTable.clearTextSelection);
       var cellRangeSelectDataTable = new YAHOO.widget.DataTable("cellrange",
               myColumnDefs, myDataSource, {
                   caption:"Example: Cell-Range Selection Mode Support for Modifier Keys",
                   selectionMode:"cellrange"
               });
       // Subscribe to events for cell selection
       cellRangeSelectDataTable.subscribe("cellMouseoverEvent", cellRangeSelectDataTable.onEventHighlightCell);
       cellRangeSelectDataTable.subscribe("cellMouseoutEvent", cellRangeSelectDataTable.onEventUnhighlightCell);
       cellRangeSelectDataTable.subscribe("cellClickEvent", cellRangeSelectDataTable.onEventSelectCell);
       cellRangeSelectDataTable.subscribe("cellSelectEvent", cellRangeSelectDataTable.clearTextSelection);
       var singleCellSelectDataTable = new YAHOO.widget.DataTable("singlecell",
               myColumnDefs, myDataSource, {
                   caption:"Single-Cell Selection Mode with Modifier Keys Disabled",
                   selectionMode:"singlecell"
               });
       // Subscribe to events for cell selection
       singleCellSelectDataTable.subscribe("cellMouseoverEvent", singleCellSelectDataTable.onEventHighlightCell);
       singleCellSelectDataTable.subscribe("cellMouseoutEvent", singleCellSelectDataTable.onEventUnhighlightCell);
       singleCellSelectDataTable.subscribe("cellClickEvent", singleCellSelectDataTable.onEventSelectCell);
       singleCellSelectDataTable.subscribe("cellSelectEvent", singleCellSelectDataTable.clearTextSelection);
       
       return {
           oDS: myDataSource,
           oDTCellBlockSelect: cellBlockSelectDataTable,
           oDTCellRangeSelect: cellRangeSelectDataTable,
           oDTSingleCellSelect: singleCellSelectDataTable
       };
   }();

}); </script>

</body> </html>


 </source>
   
  

<A href="http://www.wbex.ru/Code/JavaScriptDownload/yui_2.7.0b.zip">yui_2.7.0b.zip( 4,431 k)</a>


Highlighting Cells, Rows, or Columns

   <source lang="html4strict">

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head>

   <meta http-equiv="content-type" content="text/html; charset=utf-8">

<title>Highlighting Cells, Rows, or Columns</title> <style type="text/css"> /*margin and padding on body element

 can introduce errors in determining
 element position and are not recommended;
 we turn them off as a foundation for YUI
 CSS treatments. */

body {

 margin:0;
 padding:0;

} </style> <link rel="stylesheet" type="text/css" href="yui_2.7.0b-lib/fonts/fonts-min.css" /> <link rel="stylesheet" type="text/css" href="yui_2.7.0b-lib/datatable/assets/skins/sam/datatable.css" /> <script type="text/javascript" src="yui_2.7.0b-lib/yahoo-dom-event/yahoo-dom-event.js"></script> <script type="text/javascript" src="yui_2.7.0b-lib/element/element-min.js"></script> <script type="text/javascript" src="yui_2.7.0b-lib/datasource/datasource-min.js"></script> <script type="text/javascript" src="yui_2.7.0b-lib/datatable/datatable-min.js"></script>

</head> <body class=" yui-skin-sam">

Highlighting Cells, Rows, or Columns

<script type="text/javascript" src="yui_2.7.0b-assets/datatable-assets/js/data.js"></script> <script type="text/javascript"> YAHOO.util.Event.addListener(window, "load", function() {

   YAHOO.example.Highlighting = function() {
       var myColumnDefs = [
           {key:"Page"},
           {key:"VisitsThisMonth"},
           {key:"VisitsThisYear"},
           {key:"ViewsThisMonth"},
           {key:"ViewsThisYear"}
       ];
       var myDataSource = new YAHOO.util.DataSource(YAHOO.example.Data.webstats);
       myDataSource.responseType = YAHOO.util.DataSource.TYPE_JSARRAY;
       myDataSource.responseSchema = {
           fields: ["Page","VisitsThisMonth","VisitsThisYear","ViewsThisMonth","ViewsThisYear"]
       };
       var cellHighlightDataTable = new YAHOO.widget.DataTable("cell",
               myColumnDefs, myDataSource, {
                   caption:"Example: Cell Highlighting"
               });
       // Enable cell highlighting
       cellHighlightDataTable.subscribe("cellMouseoverEvent", cellHighlightDataTable.onEventHighlightCell);
       cellHighlightDataTable.subscribe("cellMouseoutEvent", cellHighlightDataTable.onEventUnhighlightCell);
       var rowHighlightDataTable = new YAHOO.widget.DataTable("row",
               myColumnDefs, myDataSource, {
                   caption:"Example: Row Highlighting"
               });
       // Enable row highlighting
       rowHighlightDataTable.subscribe("rowMouseoverEvent", rowHighlightDataTable.onEventHighlightRow);
       rowHighlightDataTable.subscribe("rowMouseoutEvent", rowHighlightDataTable.onEventUnhighlightRow);
       var colHighlightDataTable = new YAHOO.widget.DataTable("column",
               myColumnDefs, myDataSource, {
                   caption:"Example: Column Highlighting"
               });
       // Enable Column highlighting
       colHighlightDataTable.subscribe("theadCellMouseoverEvent", colHighlightDataTable.onEventHighlightColumn);
       colHighlightDataTable.subscribe("theadCellMouseoutEvent", colHighlightDataTable.onEventUnhighlightColumn);
       
       return {
           oDS: myDataSource,
           oDTCellHighlight: cellHighlightDataTable,
           oDTRowHighlight: rowHighlightDataTable,
           oDTColHighlight: colHighlightDataTable
       };
   }();

}); </script>

</body> </html>


 </source>
   
  

<A href="http://www.wbex.ru/Code/JavaScriptDownload/yui_2.7.0b.zip">yui_2.7.0b.zip( 4,431 k)</a>


Nested Headers

   <source lang="html4strict">


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head>

   <meta http-equiv="content-type" content="text/html; charset=utf-8">

<title>Nested Headers</title> <style type="text/css"> /*margin and padding on body element

 can introduce errors in determining
 element position and are not recommended;
 we turn them off as a foundation for YUI
 CSS treatments. */

body {

 margin:0;
 padding:0;

} </style> <link rel="stylesheet" type="text/css" href="yui_2.7.0b-lib/fonts/fonts-min.css" /> <link rel="stylesheet" type="text/css" href="yui_2.7.0b-lib/datatable/assets/skins/sam/datatable.css" /> <script type="text/javascript" src="yui_2.7.0b-lib/yahoo-dom-event/yahoo-dom-event.js"></script> <script type="text/javascript" src="yui_2.7.0b-lib/dragdrop/dragdrop-min.js"></script> <script type="text/javascript" src="yui_2.7.0b-lib/element/element-min.js"></script> <script type="text/javascript" src="yui_2.7.0b-lib/datasource/datasource-min.js"></script> <script type="text/javascript" src="yui_2.7.0b-lib/datatable/datatable-min.js"></script>

</head> <body class=" yui-skin-sam">

Nested Headers

<script type="text/javascript" src="yui_2.7.0b-assets/datatable-assets/js/data.js"></script> <script type="text/javascript"> YAHOO.util.Event.addListener(window, "load", function() {

   YAHOO.example.NestedHeaders = function() {
       var myColumnDefs = [
           {key:"page", label:"Page", sortable:true, resizeable:true},
           {label:"Statistics", formatter:YAHOO.widget.DataTable.formatNumber, children:[
               {label:"Visits",
                   children: [
                       {key:"visitsmonth", label:"This Month",sortable:true, resizeable:true},
                       {key:"visitsytd", label:"YTD", abbr:"Year to Date",sortable:true, resizeable:true}
                   ]
               },
               {label:"Views",
                   children: [
                       {key:"viewsmonth", label:"This Month",sortable:true, resizeable:true},
                       {key:"viewsytd", label:"YTD", abbr:"Year to Date",sortable:true, resizeable:true}
                   ]
               }
           ]}
       ];
       var myDataSource = new YAHOO.util.DataSource(YAHOO.example.Data.webstats);
       myDataSource.responseType = YAHOO.util.DataSource.TYPE_JSARRAY;
       myDataSource.responseSchema = {
           fields: ["page","visitsmonth","visitsytd","viewsmonth","viewsytd"]
       };
       var myDataTable = new YAHOO.widget.DataTable("nested", myColumnDefs, myDataSource);
       
       return {
           oDS: myDataSource,
           oDT: myDataTable
       };
   }();

}); </script>

</body> </html>


 </source>
   
  

<A href="http://www.wbex.ru/Code/JavaScriptDownload/yui_2.7.0b.zip">yui_2.7.0b.zip( 4,431 k)</a>


Pageable DataTable

   <source lang="html4strict">


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head>

   <meta http-equiv="content-type" content="text/html; charset=utf-8">

<title>Complex Example of Multiple Features</title> <style type="text/css"> /*margin and padding on body element

 can introduce errors in determining
 element position and are not recommended;
 we turn them off as a foundation for YUI
 CSS treatments. */

body {

 margin:0;
 padding:0;

} </style> <link rel="stylesheet" type="text/css" href="yui_2.7.0b-lib/fonts/fonts-min.css" /> <link rel="stylesheet" type="text/css" href="yui_2.7.0b-lib/paginator/assets/skins/sam/paginator.css" /> <link rel="stylesheet" type="text/css" href="yui_2.7.0b-lib/datatable/assets/skins/sam/datatable.css" /> <script type="text/javascript" src="yui_2.7.0b-lib/yahoo-dom-event/yahoo-dom-event.js"></script> <script type="text/javascript" src="yui_2.7.0b-lib/dragdrop/dragdrop-min.js"></script> <script type="text/javascript" src="yui_2.7.0b-lib/animation/animation-min.js"></script> <script type="text/javascript" src="yui_2.7.0b-lib/element/element-min.js"></script> <script type="text/javascript" src="yui_2.7.0b-lib/paginator/paginator-min.js"></script> <script type="text/javascript" src="yui_2.7.0b-lib/datasource/datasource-min.js"></script> <script type="text/javascript" src="yui_2.7.0b-lib/datatable/datatable-min.js"></script>


</head> <body class=" yui-skin-sam">

Complex Example of Multiple Features

A demonstration of several DataTable features combined in one instance. The features implemented in this example require the Drag and Drop and Animation utilities.

<script type="text/javascript" src="yui_2.7.0b-assets/datatable-assets/js/data.js"></script> <script type="text/javascript"> YAHOO.util.Event.addListener(window, "load", function() {

   YAHOO.example.MultipleFeatures = function() {
       // Custom sort handler to sort by state and then by areacode
       // where a and b are Record instances to compare
       var sortStates = function(a, b, desc) {
           // Deal with empty values
           if(!YAHOO.lang.isValue(a)) {
               return (!YAHOO.lang.isValue(b)) ? 0 : 1;
           }
           else if(!YAHOO.lang.isValue(b)) {
               return -1;
           }
           // First compare by state
           var comp = YAHOO.util.Sort.rupare;
           var compState = comp(a.getData("state"), b.getData("state"), desc);
           // If states are equal, then compare by areacode
           return (compState !== 0) ? compState : comp(a.getData("areacode"), b.getData("areacode"), desc);
       };
       var myColumnDefs = [
           {key:"areacode",label:"Area Codes",width:100,resizeable:true,sortable:true},
           {key:"state",label:"States",width:250,resizeable:true,sortable:true,
                   sortOptions:{sortFunction:sortStates}},
           {key:"notes",label:"Notes (editable)",editor:"textbox",resizeable:true,sortable:true}
       ];
       var myDataSource = new YAHOO.util.DataSource(YAHOO.example.Data.areacodes);
       myDataSource.responseType = YAHOO.util.DataSource.TYPE_JSARRAY;
       myDataSource.responseSchema = {
           fields: ["areacode","state"]
       };
       var myConfigs = {
           sortedBy:{key:"areacode",dir:"asc"},
           paginator: new YAHOO.widget.Paginator({
               rowsPerPage: 25,
               template: YAHOO.widget.Paginator.TEMPLATE_ROWS_PER_PAGE,
               rowsPerPageOptions: [10,25,50,100],
               pageLinks: 5
           }),
           draggableColumns:true
       }
       var myDataTable = new YAHOO.widget.DataTable("complex", myColumnDefs, myDataSource, myConfigs);
       myDataTable.subscribe("rowClickEvent",myDataTable.onEventSelectRow);
       myDataTable.subscribe("cellDblclickEvent",myDataTable.onEventShowCellEditor);
       myDataTable.subscribe("editorBlurEvent", myDataTable.onEventSaveCellEditor);
       // When cell is edited, pulse the color of the row yellow
       var onCellEdit = function(oArgs) {
           var elCell = oArgs.editor.getTdEl();
           var oOldData = oArgs.oldData;
           var oNewData = oArgs.newData;
           // Grab the row el and the 2 colors
           var elRow = this.getTrEl(elCell);
           var origColor = YAHOO.util.Dom.getStyle(elRow.cells[0], "backgroundColor");
           var pulseColor = "#ff0";
           // Create a temp anim instance that nulls out when anim is complete
           var rowColorAnim = new YAHOO.util.ColorAnim(elRow.cells, {
                   backgroundColor:{to:origColor, from:pulseColor}, duration:2});
           var onComplete = function() {
               rowColorAnim = null;
               YAHOO.util.Dom.setStyle(elRow.cells, "backgroundColor", "");
           }
           rowColorAnim.onComplete.subscribe(onComplete);
           rowColorAnim.animate();
       }
       myDataTable.subscribe("editorSaveEvent", onCellEdit);
       
       return {
           oDS: myDataSource,
           oDT: myDataTable
       };
   }();

}); </script>

</body> </html>


 </source>
   
  

<A href="http://www.wbex.ru/Code/JavaScriptDownload/yui_2.7.0b.zip">yui_2.7.0b.zip( 4,431 k)</a>


Populates a DataTable with data received via XHR from the Yahoo! Local webservice.

   <source lang="html4strict">

<head>

   <meta http-equiv="content-type" content="text/html; charset=utf-8">

<title>JSON Data Over XHR</title> <style type="text/css"> /*margin and padding on body element

 can introduce errors in determining
 element position and are not recommended;
 we turn them off as a foundation for YUI
 CSS treatments. */

body {

 margin:0;
 padding:0;

} </style> <link rel="stylesheet" type="text/css" href="yui_2.7.0b-lib/fonts/fonts-min.css" /> <link rel="stylesheet" type="text/css" href="yui_2.7.0b-lib/datatable/assets/skins/sam/datatable.css" /> <script type="text/javascript" src="yui_2.7.0b-lib/yahoo-dom-event/yahoo-dom-event.js"></script> <script type="text/javascript" src="yui_2.7.0b-lib/connection/connection-min.js"></script> <script type="text/javascript" src="yui_2.7.0b-lib/json/json-min.js"></script> <script type="text/javascript" src="yui_2.7.0b-lib/element/element-min.js"></script> <script type="text/javascript" src="yui_2.7.0b-lib/datasource/datasource-min.js"></script> <script type="text/javascript" src="yui_2.7.0b-lib/datatable/datatable-min.js"></script>

</head> <body class=" yui-skin-sam">

JSON Data Over XHR

This example populates a DataTable with data received via XHR from the Yahoo! Local webservice.

<script type="text/javascript"> YAHOO.util.Event.addListener(window, "load", function() {

   YAHOO.example.XHR_JSON = function() {
       var formatUrl = function(elCell, oRecord, oColumn, sData) {
           elCell.innerHTML = "<a href="" + oRecord.getData("ClickUrl") + "" target="_blank">" + sData + "</a>";
       };
       var myColumnDefs = [
           {key:"Title", label:"Name", sortable:true, formatter:formatUrl},
           {key:"Phone"},
           {key:"City"},
           {key:"Rating.AverageRating", label:"Rating", formatter:YAHOO.widget.DataTable.formatNumber, sortable:true}
       ];
       var myDataSource = new YAHOO.util.DataSource("yui_2.7.0b-assets/datatable-assets/php/ylocal_proxy.php?");
       myDataSource.responseType = YAHOO.util.DataSource.TYPE_JSON;
       myDataSource.connXhrMode = "queueRequests";
       myDataSource.responseSchema = {
           resultsList: "ResultSet.Result",
           fields: ["Title","Phone","City",{key:"Rating.AverageRating",parser:"number"},"ClickUrl"]
       };
       var myDataTable = new YAHOO.widget.DataTable("json", myColumnDefs,
               myDataSource, {initialRequest:"query=pizza&zip=94089&results=10&output=json"});
       var mySuccessHandler = function() {
           this.set("sortedBy", null);
           this.onDataReturnAppendRows.apply(this,arguments);
       };
       var myFailureHandler = function() {
           this.showTableMessage(YAHOO.widget.DataTable.MSG_ERROR, YAHOO.widget.DataTable.CLASS_ERROR);
           this.onDataReturnAppendRows.apply(this,arguments);
       };
       var callbackObj = {
           success : mySuccessHandler,
           failure : myFailureHandler,
           scope : myDataTable
       };
       
       myDataSource.sendRequest("query=mexican&zip=94089&results=10&output=json",
               callbackObj);
       myDataSource.sendRequest("query=chinese&zip=94089&results=10&output=json",
               callbackObj);
               
       return {
           oDS: myDataSource,
           oDT: myDataTable
       };
   }();

}); </script>

</body>

 </source>
   
  

<A href="http://www.wbex.ru/Code/JavaScriptDownload/yui_2.7.0b.zip">yui_2.7.0b.zip( 4,431 k)</a>


Showing, Hiding, and Reordering Columns

   <source lang="html4strict">


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <style type="text/css"> /* custom styles for this example */

  1. dt-example {width:45em;margin:0 auto;}
  2. dt-options {text-align:right;margin:1em 0;}
  3. dt-dlg {visibility:hidden;border:1px solid #808080;background-color:#E3E3E3;}
  4. dt-dlg .hd {font-weight:bold;padding:1em;background:none;background-color:#E3E3E3;border-bottom:0;}
  5. dt-dlg .ft {text-align:right;padding:.5em;background-color:#E3E3E3;}
  6. dt-dlg .bd {height:10em;margin:0 1em;overflow:auto;border:1px solid black;background-color:white;}
  7. dt-dlg .dt-dlg-pickercol {clear:both;padding:.5em 1em 3em;border-bottom:1px solid gray;}
  8. dt-dlg .dt-dlg-pickerkey {float:left;}
  9. dt-dlg .dt-dlg-pickerbtns {float:right;}

/* Container workarounds for Mac Gecko scrollbar issues */ .yui-panel-container.hide-scrollbars #dt-dlg .bd {

   /* Hide scrollbars by default for Gecko on OS X */
   overflow: hidden;

} .yui-panel-container.show-scrollbars #dt-dlg .bd {

   /* Show scrollbars for Gecko on OS X when the Panel is visible  */
   overflow: auto;

}

  1. dt-dlg_c .underlay {overflow:hidden;}

/* rounded corners */

  1. dt-dlg .corner_tr {
   background-image: url( yui_2.7.0b-assets/datatable-assets/img/tr.gif);
   position: absolute;
   background-repeat: no-repeat;
   top: -1px;
   right: -1px;
   height: 4px;
   width: 4px;

}

  1. dt-dlg .corner_tl {
   background-image: url( yui_2.7.0b-assets/datatable-assets/img/tl.gif);
   background-repeat: no-repeat;
   position: absolute;
   top: -1px;
   left: -1px;
   height: 4px;
   width: 4px;

}

  1. dt-dlg .corner_br {
   background-image: url( yui_2.7.0b-assets/datatable-assets/img/br.gif);
   position: absolute;
   background-repeat: no-repeat;
   bottom: -1px;
   right: -1px;
   height: 4px;
   width: 4px;

}

  1. dt-dlg .corner_bl {
   background-image: url( yui_2.7.0b-assets/datatable-assets/img/bl.gif);
   background-repeat: no-repeat;
   position: absolute;
   bottom: -1px;
   left: -1px;
   height: 4px;
   width: 4px;

} .inprogress {position:absolute;} /* transitional progressive enhancement state */ .yui-dt-liner {white-space:nowrap;} </style> </head> <body class="yui-skin-sam">

Showing, Hiding, and Reordering Columns

This example uses the Dialog and Button widgets to interactively show and hide Columns. Columns are also reorderable via built-in integration with the Drag and Drop Utility.

<a id="dt-options-link" href="fallbacklink.html">Table Options</a>
   
   
   
   
       Choose which columns you would like to see:

<script type="text/javascript" src="yui_2.7.0b-lib/yuiloader/yuiloader.js"></script> <script type="text/javascript" src="./yui_2.7.0b-assets/datatable-assets/js/data.js"></script> <script type="text/javascript"> var loader = new YAHOO.util.YUILoader(); loader.insert({

   require: ["fonts", "dragdrop", "container", "button", "datatable"],
   base: "yui_2.7.0b-lib/",
   filter: "debug",
   allowRollup: false,
   onSuccess: function() {
       YAHOO.example.ColumnShowHide = function() {
           // Define Columns
           var myColumnDefs = [
               {key:"address"},
               {key:"city"},
               {key:"state"},
               {key:"amount"},
               {key:"active"},
               {key:"colors"},
               {key:"last_login", formatter:YAHOO.widget.DataTable.formatDate}
           ];
   
           // Create DataSource
           var myDataSource = new YAHOO.util.DataSource(YAHOO.example.Data.addresses);
           myDataSource.responseType = YAHOO.util.DataSource.TYPE_JSARRAY;
           myDataSource.responseSchema = {
               fields: ["address","city","state","amount","active","colors",{key:"last_login",parser:"date"}]
           };
   
           // Create DataTable
           var myDataTable = new YAHOO.widget.DataTable("columnshowhide", myColumnDefs, myDataSource, {draggableColumns:true});
                       
           // Shows dialog, creating one when necessary
           var newCols = true;
           var showDlg = function(e) {
               YAHOO.util.Event.stopEvent(e);
               if(newCols) {
                   // Populate Dialog
                   // Using a template to create elements for the SimpleDialog
                   var allColumns = myDataTable.getColumnSet().keys;
                   var elPicker = YAHOO.util.Dom.get("dt-dlg-picker");
                   var elTemplateCol = document.createElement("div");
                   YAHOO.util.Dom.addClass(elTemplateCol, "dt-dlg-pickercol");
                   var elTemplateKey = elTemplateCol.appendChild(document.createElement("span"));
                   YAHOO.util.Dom.addClass(elTemplateKey, "dt-dlg-pickerkey");
                   var elTemplateBtns = elTemplateCol.appendChild(document.createElement("span"));
                   YAHOO.util.Dom.addClass(elTemplateBtns, "dt-dlg-pickerbtns");
                   var onclickObj = {fn:handleButtonClick, obj:this, scope:false };
                   
                   // Create one section in the SimpleDialog for each Column
                   var elColumn, elKey, elButton, oButtonGrp;
                   for(var i=0,l=allColumns.length;i<l;i++) {
                       var oColumn = allColumns[i];
                       
                       // Use the template
                       elColumn = elTemplateCol.cloneNode(true);
                       
                       // Write the Column key
                       elKey = elColumn.firstChild;
                       elKey.innerHTML = oColumn.getKey();
                       
                       // Create a ButtonGroup
                       oButtonGrp = new YAHOO.widget.ButtonGroup({ 
                                       id: "buttongrp"+i, 
                                       name: oColumn.getKey(), 
                                       container: elKey.nextSibling
                       });
                       oButtonGrp.addButtons([
                           { label: "Show", value: "Show", checked: ((!oColumn.hidden)), onclick: onclickObj},
                           { label: "Hide", value: "Hide", checked: ((oColumn.hidden)), onclick: onclickObj}
                       ]);
                                       
                       elPicker.appendChild(elColumn);
                   }
                   newCols = false;
             }
               myDlg.show();
           };
           var hideDlg = function(e) {
               this.hide();
           };
           var handleButtonClick = function(e, oSelf) {
               var sKey = this.get("name");
               if(this.get("value") === "Hide") {
                   // Hides a Column
                   myDataTable.hideColumn(sKey);
               }
               else {
                   // Shows a Column
                   myDataTable.showColumn(sKey);
               }
           };
           
           // Create the SimpleDialog
           YAHOO.util.Dom.removeClass("dt-dlg", "inprogress");
           var myDlg = new YAHOO.widget.SimpleDialog("dt-dlg", {
                   width: "30em",
             visible: false,
             modal: true,
             buttons: [ 
             { text:"Close",  handler:hideDlg }
                   ],
                   fixedcenter: true,
                   constrainToViewport: true
       });
       myDlg.render();
           // Nulls out myDlg to force a new one to be created
           myDataTable.subscribe("columnReorderEvent", function(){
               newCols = true;
               YAHOO.util.Event.purgeElement("dt-dlg-picker", true);
               YAHOO.util.Dom.get("dt-dlg-picker").innerHTML = "";
           }, this, true);
       
       // Hook up the SimpleDialog to the link
       YAHOO.util.Event.addListener("dt-options-link", "click", showDlg, this, true);
       
       return {
         oDS: myDataSource,
         oDT: myDataTable
       };
       }();
   }

}); </script> </body> </html>


 </source>
   
  

<A href="http://www.wbex.ru/Code/JavaScriptDownload/yui_2.7.0b.zip">yui_2.7.0b.zip( 4,431 k)</a>


Single-Cell Selection Mode with Modifier Keys Disabled

   <source lang="html4strict">

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head>

   <meta http-equiv="content-type" content="text/html; charset=utf-8">

<title>Cell Selection</title> <style type="text/css"> /*margin and padding on body element

 can introduce errors in determining
 element position and are not recommended;
 we turn them off as a foundation for YUI
 CSS treatments. */

body {

 margin:0;
 padding:0;

} </style> <link rel="stylesheet" type="text/css" href="yui_2.7.0b-lib/fonts/fonts-min.css" /> <link rel="stylesheet" type="text/css" href="yui_2.7.0b-lib/datatable/assets/skins/sam/datatable.css" /> <script type="text/javascript" src="yui_2.7.0b-lib/yahoo-dom-event/yahoo-dom-event.js"></script> <script type="text/javascript" src="yui_2.7.0b-lib/element/element-min.js"></script> <script type="text/javascript" src="yui_2.7.0b-lib/datasource/datasource-min.js"></script> <script type="text/javascript" src="yui_2.7.0b-lib/datatable/datatable-min.js"></script>


<style type="text/css"> /* custom styles for this example */ .yui-skin-sam .yui-dt-body { cursor:pointer; } /* when cells are selectable */

  1. cellrange, #singlecell { margin-top:2em; }

</style>

</head> <body class=" yui-skin-sam">

Cell Selection

These examples demonstrate "cellblock", "cellrange", and "singlecell" selection modes.

<script type="text/javascript" src="yui_2.7.0b-assets/datatable-assets/js/data.js"></script> <script type="text/javascript"> YAHOO.util.Event.addListener(window, "load", function() {

   YAHOO.example.CellSelection = function() {
       var myColumnDefs = [
           {key:"col1", sortable:true},
           {key:"col2", sortable:true},
           {key:"col3", sortable:true},
           {key:"col4", sortable:true}
       ];
       var myDataSource = new YAHOO.util.DataSource(YAHOO.example.Data.webstats);
       myDataSource.responseType = YAHOO.util.DataSource.TYPE_JSARRAY;
       myDataSource.responseSchema = {
           fields: ["col0","col1","col2","col3","col4"]
       };
       var cellBlockSelectDataTable = new YAHOO.widget.DataTable("cellblock",
               myColumnDefs, myDataSource, {
                   caption:"Cell-Block Selection Mode with Support for Modifier Keys",
                   selectionMode:"cellblock"
               });
       // Subscribe to events for cell selection
       cellBlockSelectDataTable.subscribe("cellMouseoverEvent", cellBlockSelectDataTable.onEventHighlightCell);
       cellBlockSelectDataTable.subscribe("cellMouseoutEvent", cellBlockSelectDataTable.onEventUnhighlightCell);
       cellBlockSelectDataTable.subscribe("cellClickEvent", cellBlockSelectDataTable.onEventSelectCell);
       cellBlockSelectDataTable.subscribe("cellSelectEvent", cellBlockSelectDataTable.clearTextSelection);
       var cellRangeSelectDataTable = new YAHOO.widget.DataTable("cellrange",
               myColumnDefs, myDataSource, {
                   caption:"Example: Cell-Range Selection Mode Support for Modifier Keys",
                   selectionMode:"cellrange"
               });
       // Subscribe to events for cell selection
       cellRangeSelectDataTable.subscribe("cellMouseoverEvent", cellRangeSelectDataTable.onEventHighlightCell);
       cellRangeSelectDataTable.subscribe("cellMouseoutEvent", cellRangeSelectDataTable.onEventUnhighlightCell);
       cellRangeSelectDataTable.subscribe("cellClickEvent", cellRangeSelectDataTable.onEventSelectCell);
       cellRangeSelectDataTable.subscribe("cellSelectEvent", cellRangeSelectDataTable.clearTextSelection);
       var singleCellSelectDataTable = new YAHOO.widget.DataTable("singlecell",
               myColumnDefs, myDataSource, {
                   caption:"Single-Cell Selection Mode with Modifier Keys Disabled",
                   selectionMode:"singlecell"
               });
       // Subscribe to events for cell selection
       singleCellSelectDataTable.subscribe("cellMouseoverEvent", singleCellSelectDataTable.onEventHighlightCell);
       singleCellSelectDataTable.subscribe("cellMouseoutEvent", singleCellSelectDataTable.onEventUnhighlightCell);
       singleCellSelectDataTable.subscribe("cellClickEvent", singleCellSelectDataTable.onEventSelectCell);
       singleCellSelectDataTable.subscribe("cellSelectEvent", singleCellSelectDataTable.clearTextSelection);
       
       return {
           oDS: myDataSource,
           oDTCellBlockSelect: cellBlockSelectDataTable,
           oDTCellRangeSelect: cellRangeSelectDataTable,
           oDTSingleCellSelect: singleCellSelectDataTable
       };
   }();

}); </script>

</body> </html>


 </source>
   
  

<A href="http://www.wbex.ru/Code/JavaScriptDownload/yui_2.7.0b.zip">yui_2.7.0b.zip( 4,431 k)</a>


Single-Row Selection with Modifier Keys Disabled

   <source lang="html4strict">


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head>

   <meta http-equiv="content-type" content="text/html; charset=utf-8">

<title>Row Selection</title> <style type="text/css"> /*margin and padding on body element

 can introduce errors in determining
 element position and are not recommended;
 we turn them off as a foundation for YUI
 CSS treatments. */

body {

 margin:0;
 padding:0;

} </style> <link rel="stylesheet" type="text/css" href="yui_2.7.0b-lib/fonts/fonts-min.css" /> <link rel="stylesheet" type="text/css" href="yui_2.7.0b-lib/datatable/assets/skins/sam/datatable.css" /> <script type="text/javascript" src="yui_2.7.0b-lib/yahoo-dom-event/yahoo-dom-event.js"></script> <script type="text/javascript" src="yui_2.7.0b-lib/element/element-min.js"></script> <script type="text/javascript" src="yui_2.7.0b-lib/datasource/datasource-min.js"></script> <script type="text/javascript" src="yui_2.7.0b-lib/datatable/datatable-min.js"></script>


<style type="text/css"> /* custom styles for this example */ .yui-skin-sam .yui-dt-body { cursor:pointer; } /* when rows are selectable */

  1. single { margin-top:2em; }

</style>

</head> <body class=" yui-skin-sam">

Row Selection

These examples demonstrate "standard" row selection mode and "single" row selection mode.

<script type="text/javascript" src="yui_2.7.0b-assets/datatable-assets/js/data.js"></script> <script type="text/javascript"> YAHOO.util.Event.addListener(window, "load", function() {

   YAHOO.example.RowSelection = function() {
       var myColumnDefs = [
           {key:"Date",formatter:YAHOO.widget.DataTable.formatDate, sortable:true},
           {key:"To", sortable:true},
           {key:"From", sortable:true},
           {key:"Subject", sortable:true}
       ];
       var myDataSource = new YAHOO.util.DataSource(YAHOO.example.Data.emails);
       myDataSource.responseType = YAHOO.util.DataSource.TYPE_JSON;
       myDataSource.responseSchema = {
           resultsList: "messages",
           fields: ["Date","To","From","Subject","XID","Date","Attachment"]
       };
       var standardSelectDataTable = new YAHOO.widget.DataTable("standard",
               myColumnDefs, myDataSource, {
                   caption:"Standard Row Selection with Support for Modifier Keys"
               });
               
       // Subscribe to events for row selection
       standardSelectDataTable.subscribe("rowMouseoverEvent", standardSelectDataTable.onEventHighlightRow);
       standardSelectDataTable.subscribe("rowMouseoutEvent", standardSelectDataTable.onEventUnhighlightRow);
       standardSelectDataTable.subscribe("rowClickEvent", standardSelectDataTable.onEventSelectRow);
       // Programmatically select the first row
       standardSelectDataTable.selectRow(standardSelectDataTable.getTrEl(0));
       // Programmatically bring focus to the instance so arrow selection works immediately
       standardSelectDataTable.focus();
       
       var singleSelectDataTable = new YAHOO.widget.DataTable("single",
               myColumnDefs, myDataSource, {
                   caption:"Single-Row Selection with Modifier Keys Disabled",
                   selectionMode:"single"
               });
               
       // Subscribe to events for row selection
       singleSelectDataTable.subscribe("rowMouseoverEvent", singleSelectDataTable.onEventHighlightRow);
       singleSelectDataTable.subscribe("rowMouseoutEvent", singleSelectDataTable.onEventUnhighlightRow);
       singleSelectDataTable.subscribe("rowClickEvent", singleSelectDataTable.onEventSelectRow);
       
       // Programmatically select the first row
       singleSelectDataTable.selectRow(singleSelectDataTable.getTrEl(0));
       
       return {
           oDS: myDataSource,
           oDTStandardSelect: standardSelectDataTable,
           oDTSingleSelect: singleSelectDataTable
       };
   }();

}); </script>

</body> </html>


 </source>
   
  

<A href="http://www.wbex.ru/Code/JavaScriptDownload/yui_2.7.0b.zip">yui_2.7.0b.zip( 4,431 k)</a>


Standard Row Selection with Support for Modifier Keys

   <source lang="html4strict">

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head>

   <meta http-equiv="content-type" content="text/html; charset=utf-8">

<title>Row Selection</title> <style type="text/css"> /*margin and padding on body element

 can introduce errors in determining
 element position and are not recommended;
 we turn them off as a foundation for YUI
 CSS treatments. */

body {

 margin:0;
 padding:0;

} </style> <link rel="stylesheet" type="text/css" href="yui_2.7.0b-lib/fonts/fonts-min.css" /> <link rel="stylesheet" type="text/css" href="yui_2.7.0b-lib/datatable/assets/skins/sam/datatable.css" /> <script type="text/javascript" src="yui_2.7.0b-lib/yahoo-dom-event/yahoo-dom-event.js"></script> <script type="text/javascript" src="yui_2.7.0b-lib/element/element-min.js"></script> <script type="text/javascript" src="yui_2.7.0b-lib/datasource/datasource-min.js"></script> <script type="text/javascript" src="yui_2.7.0b-lib/datatable/datatable-min.js"></script>


<style type="text/css"> /* custom styles for this example */ .yui-skin-sam .yui-dt-body { cursor:pointer; } /* when rows are selectable */

  1. single { margin-top:2em; }

</style>

</head> <body class=" yui-skin-sam">

Row Selection

These examples demonstrate "standard" row selection mode and "single" row selection mode.

<script type="text/javascript" src="yui_2.7.0b-assets/datatable-assets/js/data.js"></script> <script type="text/javascript"> YAHOO.util.Event.addListener(window, "load", function() {

   YAHOO.example.RowSelection = function() {
       var myColumnDefs = [
           {key:"Date",formatter:YAHOO.widget.DataTable.formatDate, sortable:true},
           {key:"To", sortable:true},
           {key:"From", sortable:true},
           {key:"Subject", sortable:true}
       ];
       var myDataSource = new YAHOO.util.DataSource(YAHOO.example.Data.emails);
       myDataSource.responseType = YAHOO.util.DataSource.TYPE_JSON;
       myDataSource.responseSchema = {
           resultsList: "messages",
           fields: ["Date","To","From","Subject","XID","Date","Attachment"]
       };
       var standardSelectDataTable = new YAHOO.widget.DataTable("standard",
               myColumnDefs, myDataSource, {
                   caption:"Standard Row Selection with Support for Modifier Keys"
               });
               
       // Subscribe to events for row selection
       standardSelectDataTable.subscribe("rowMouseoverEvent", standardSelectDataTable.onEventHighlightRow);
       standardSelectDataTable.subscribe("rowMouseoutEvent", standardSelectDataTable.onEventUnhighlightRow);
       standardSelectDataTable.subscribe("rowClickEvent", standardSelectDataTable.onEventSelectRow);
       // Programmatically select the first row
       standardSelectDataTable.selectRow(standardSelectDataTable.getTrEl(0));
       // Programmatically bring focus to the instance so arrow selection works immediately
       standardSelectDataTable.focus();
       
       var singleSelectDataTable = new YAHOO.widget.DataTable("single",
               myColumnDefs, myDataSource, {
                   caption:"Single-Row Selection with Modifier Keys Disabled",
                   selectionMode:"single"
               });
               
       // Subscribe to events for row selection
       singleSelectDataTable.subscribe("rowMouseoverEvent", singleSelectDataTable.onEventHighlightRow);
       singleSelectDataTable.subscribe("rowMouseoutEvent", singleSelectDataTable.onEventUnhighlightRow);
       singleSelectDataTable.subscribe("rowClickEvent", singleSelectDataTable.onEventSelectRow);
       
       // Programmatically select the first row
       singleSelectDataTable.selectRow(singleSelectDataTable.getTrEl(0));
       
       return {
           oDS: myDataSource,
           oDTStandardSelect: standardSelectDataTable,
           oDTSingleSelect: singleSelectDataTable
       };
   }();

}); </script>

</body> </html>


 </source>
   
  

<A href="http://www.wbex.ru/Code/JavaScriptDownload/yui_2.7.0b.zip">yui_2.7.0b.zip( 4,431 k)</a>


This DataTable polls for data from its DataSource every 5 seconds

   <source lang="html4strict">

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head>

   <meta http-equiv="content-type" content="text/html; charset=utf-8">

<title>Polling the DataSource</title> <style type="text/css"> /*margin and padding on body element

 can introduce errors in determining
 element position and are not recommended;
 we turn them off as a foundation for YUI
 CSS treatments. */

body {

 margin:0;
 padding:0;

} </style> <link rel="stylesheet" type="text/css" href="yui_2.7.0b-lib/fonts/fonts-min.css" /> <link rel="stylesheet" type="text/css" href="yui_2.7.0b-lib/datatable/assets/skins/sam/datatable.css" /> <script type="text/javascript" src="yui_2.7.0b-lib/yahoo-dom-event/yahoo-dom-event.js"></script> <script type="text/javascript" src="yui_2.7.0b-lib/dragdrop/dragdrop-min.js"></script> <script type="text/javascript" src="yui_2.7.0b-lib/element/element-min.js"></script> <script type="text/javascript" src="yui_2.7.0b-lib/datasource/datasource-min.js"></script> <script type="text/javascript" src="yui_2.7.0b-lib/datatable/datatable-min.js"></script>

</head> <body class=" yui-skin-sam">

Polling the DataSource

This DataTable polls for data from its DataSource every 5 seconds. Due to browser limitations, implementers should use this feature to replace existing data, rather than continuously add data to the page.

<script type="text/javascript" src="yui_2.7.0b-assets/datatable-assets/js/data.js"></script> <script type="text/javascript"> YAHOO.util.Event.addListener(window, "load", function() {

   YAHOO.example.Polling = function() {
       var counter = 0;
       var dataIncrementer = function() {
           counter++;
           return [{
               count:counter, 
               description:"At the tone the time will be: ", 
               time: YAHOO.util.Date.format(new Date(), {format:"%I:%M:%S %p"})
           }]
       };
       var myColumnDefs = [
           {key: "count"},
           {key: "description"},
           {key: "time"}
       ];
       var myDataSource = new YAHOO.util.DataSource(dataIncrementer);
       myDataSource.responseType = YAHOO.util.DataSource.TYPE_JSARRAY;
       myDataSource.responseSchema = {
           fields: ["count", "description", "time"]
       };
       var myDataTable = new YAHOO.widget.DataTable("polling",
               myColumnDefs, myDataSource);
               
       // Set up polling
       var myCallback = {
           success: myDataTable.onDataReturnInitializeTable,
           failure: function() {
               YAHOO.log("Polling failure", "error");
           },
           scope: myDataTable
       }
       myDataSource.setInterval(5000, null, myCallback)
               
       return {
           oDS: myDataSource,
           oDT: myDataTable
       };
   }();

}); </script>

</body> </html>


 </source>
   
  

<A href="http://www.wbex.ru/Code/JavaScriptDownload/yui_2.7.0b.zip">yui_2.7.0b.zip( 4,431 k)</a>