JavaScript DHTML/YUI Library/Paginator

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

Configuring the Paginator

   <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>Configuring the Paginator</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/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">

  1. demo {
   width: 525px;

}

  1. pag {
   display: inline;
   float: left;
   width: 250px;
   margin-top: 0;

}

  1. pag a {
   color: #0000de;

}

  1. pag label {
   display: block;
   margin: 1ex 0;

}

  1. pag p {
   margin: .25ex 0;

} .yui-skin-sam #pag .yui-pg-pages {

   display: block;

} .yui-skin-sam #pag .yui-pg-page {

   display: block;
   background: transparent;
   border: none;
   padding: .5ex 0;
   white-space: normal;

} .yui-skin-sam #pag .yui-pg-current-page {

   padding: .5ex 0;
   background-color: #ffe;
   font-style: italic;

} .yui-skin-sam #pag .yui-pg-current {

   margin: 0;
   white-space: normal;
   font-weight: bold;
   font-size: 113%;

} .yui-skin-sam #demo .yui-dt caption {

   margin: 0.2em 0 0;
   color: #e76300;
   font-weight: bold;

} </style>

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

Configuring the Paginator

In this example we"ll demonstrate the use of all of Paginator"s configuration options, including the options added by the bundled UI Components. Most notably, we"ll use the template and pageLabelBuilder options to render the pagination controls in a custom layout with more descriptive page links. All content in the left column is generated by the Paginator.

<script type="text/javascript" src="yui_2.7.0b-assets/paginator-assets/areacodes.js"></script> <script type="text/javascript"> YAHOO.util.Event.onDOMReady(function () { var Ex = YAHOO.namespace("example"); // Sort our data by state, then area code Ex.data.areacodes.sort(function (a,b) {

   return YAHOO.util.Sort.rupare(a.state,b.state) ||
          YAHOO.util.Sort.rupare(a.areacode,b.areacode);

}); // Custom function we"ll use for the page links Ex.buildPageLabel = function (recs) {

   var data  = Ex.data.areacodes,
       start = recs[0],
       end   = recs[1];
   // Nested function to find the smallest substring
   // to indicate how two strings differ
   var diffNames = function (a,b) {
       var aa = a.state.toLowerCase(),
           bb = b.state.toLowerCase();
       for (var i = 0, len = aa.length; i < len; ++i) {
           if (aa.charAt(i) !== bb.charAt(i)) {
               return a.state.substr(0,i+1);
           }
       }
       return a.state + " ("+a.areacode+")";
   };
   // Build label as "A - C" or "Abc - Def"
   var label = "";
   if (!start) {
       label = data[0].state.substr(0,2) + " - ";
   } else {
       label = diffNames(data[start], data[start-1]) + " - ";
   }
   if (data[end+1]) {
       label += diffNames(data[end], data[end+1]);
   } else {
       label += diffNames(data[end], data[start]);
   }
   return label;

};

// Paginator configurations Ex.config = {

   // REQUIRED
   rowsPerPage : 20,
   // REQUIRED, but DataTable will default if not provided
   containers  : "pag",
   // If not provided, there is no last page or total pages.
   // DataTable will set this in the DataSource callback, so this is
   // redundant.
   totalRecords : Ex.data.areacodes.length,
   // page to activate at load
   initialPage : 3,
   // Class the element(s) that will contain the controls
   containerClass : "yui-pg-container", // default
   // Define the innerHTML of the container(s) using placeholders
   // to identify where the controls will be located
   template :
"

Now showing:

" + "

{CurrentPageReport}

" + "

" + "{FirstPageLink} {PreviousPageLink} " + "{NextPageLink} {LastPageLink}" + "

" +
       "<label>Page size: {RowsPerPageDropdown}</label>" +
"

Directory

" +
       "{PageLinks}",
   // If there is less data than would display on one page, pagination
   // controls can be omitted by setting this to false.
   alwaysVisible : true, // default
   // Override setPage (et al) to immediately update internal values
   // and update the pagination controls in response to user actions.
   // Default is false; requests are delegated through the changeRequest
   // event subscriber.
   updateOnChange : false, // default
   // Options for FirstPageLink component
   firstPageLinkLabel : "<<",
   firstPageLinkClass : "yui-pg-first", // default
   // Options for LastPageLink component
   lastPageLinkLabel : ">>",
   lastPageLinkClass : "yui-pg-last", // default
   // Options for PreviousPageLink component
   previousPageLinkLabel : "< previous",
   previousPageLinkClass : "yui-pg-previous", // default
   // Options for NextPageLink component
   nextPageLinkLabel : "next >", // default
   nextPageLinkClass : "yui-pg-next", // default
   // Options for PageLinks component
   pageLinksContainerClass : "yui-pg-pages",        // default
   pageLinkClass           : "yui-pg-page",         // default
   currentPageClass        : "yui-pg-current-page", // default
   // Display a maximum of X page links.  Use
   // YAHOO.widget.Paginator.VALUE_UNLIMITED to show all page links
   pageLinks               : YAHOO.widget.Paginator.VALUE_UNLIMITED,
   // Create custom page link labels
   pageLabelBuilder        : function (page,paginator) {
       return Ex.buildPageLabel(paginator.getPageRecords(page));
   },
   // Options for RowsPerPageDropdown component
   rowsPerPageDropdownClass : "yui-pg-rpp-options", // default
   rowsPerPageOptions       : [
       { value : 20, text : "small" },
       { value : 40, text : "medium" },
       { value : 100, text : "large" }
   ],
   // Options for CurrentPageReport component
   pageReportClass : "yui-pg-current", // default
   // Provide a key:value map for use by the pageReportTemplate.
   // Unlikely this will need to be customized; see API docs for the
   // template keys made available by the default value generator
   pageReportValueGenerator : function (paginator) {
       var recs  = paginator.getPageRecords();
       return {
           start     : Ex.data.areacodes[recs[0]].state,
           end       : Ex.data.areacodes[recs[1]].state
       };
   },
   // How to render the notification of the Paginator"s current state
   pageReportTemplate : "{start} - {end}"

}; // Create the Paginator for our DataTable to use Ex.paginator = new YAHOO.widget.Paginator(Ex.config);

// Normal DataTable configuration Ex.tableCols = [ {key:"state", label:"State", minWidth: 150},

                {key:"areacode", label:"Code",  width: 30}];

Ex.dataSource = new YAHOO.util.DataSource(Ex.data.areacodes, {

   responseType   : YAHOO.util.DataSource.TYPE_JSARRAY,
   responseSchema : {
       fields : ["state","areacode"]
   }

}); // Pass the Paginator in the DataTable config Ex.tableConfig = {

   paginator : Ex.paginator,
   caption   : "Area Codes by State"

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

   Ex.tableCols, Ex.dataSource, Ex.tableConfig);

}); </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>


Getting started with Paginator

   <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>Getting started with Paginator</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" /> <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>


<style type="text/css">

  1. content {
   background: #fff;
   border: 1px solid #ccc;
   color: #000;
   font-family: Times New Roman, serif;
   padding: 1em 2em;

}

  1. content div {
   display: none;

}

  1. demo .page1 div.page1,
  2. demo .page2 div.page2,
  3. demo .page3 div.page3,
  4. demo .page4 div.page4,
  5. demo .page5 div.page5 {
   display: block;

} </style>

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

Getting started with Paginator

In this example we illustrate the basics of attaching a Paginator to your application. We take a short story by Stephen Crane and divide it up into pages, then use Paginator to display the page navigation.

The Monster

By Stephen Crane

Little Jim was, for the time, engine Number 36, and he was making the run between Syracuse and Rochester. He was fourteen minutes behind time, and the throttle was wide open. In consequence, when he swung around the curve at the flower-bed, a wheel of his cart destroyed a peony. Number 36 slowed down at once and looked guiltily at his father, who was mowing the lawn. The doctor had his back to this accident, and he continued to pace slowly to and fro, pushing the mower.

Jim dropped the tongue of the cart. He looked at his father and at the broken flower. Finally he went to the peony and tried to stand it on its pins, resuscitated, but the spine of it was hurt, and it would only hang limply from his hand. Jim could do no reparation. He looked again toward his father.

He went on to the lawn, very slowly, and kicking wretchedly at the turf. Presently his father came along with the whirring machine, while the sweet, new grass blades spun from the knives. In a low voice, Jim said, �Pa!�

The doctor was shaving this lawn as if it were a priest�s chin. All during the season he had worked at it in the coolness and peace of the evenings after supper. Even in the shadow of the cherry-trees the grass was strong and healthy. Jim raised his voice a trifle. �Pa!�

The doctor paused, and with the howl of the machine no longer occupying the sense, one could hear the robins in the cherry-trees arranging their affairs. Jim�s hands were behind his back, and sometimes his fingers clasped and unclasped. Again he said, �Pa!� The child�s fresh and rosy lip was lowered.

The doctor stared down at his son, thrusting his head forward and frowning attentively. �What is it, Jimmie?�

�Pa!� repeated the child at length. Then he raised his finger and pointed at the flower-bed. �There!�

�What?� said the doctor, frowning more. �What is it, Jim?�

After a period of silence, during which the child may have undergone a severe mental tumult, he raised his finger and repeated his former word��There!� The father had respected this silence with perfect courtesy. Afterward his glance carefully followed the direction indicated by the child�s finger, but he could see nothing which explained to him. �I don�t understand what you mean, Jimmie,� he said.

It seemed that the importance of the whole thing had taken away the boy�s vocabulary. He could only reiterate, �There!�

The doctor mused upon the situation, but he could make nothing of it. At last he said, �Come, show me.�

Together they crossed the lawn toward the flower-bed. At some yards from the broken peony Jimmie began to lag. �There!� The word came almost breathlessly.

�Where?� said the doctor.

Jimmie kicked at the grass. �There!� he replied.

The doctor was obliged to go forward alone. After some trouble he found the subject of the incident, the broken flower. Turning then, he saw the child lurking at the rear and scanning his countenance.

The father reflected. After a time he said, �Jimmie, come here.� With an infinite modesty of demeanour the child came forward. �Jimmie, how did this happen?�

The child answered, �Now—I was playin� train—and—now—I runned over it.�

<script type="text/javascript"> YAHOO.util.Event.onDOMReady(function () { // Set up the application under the YAHOO.example namespace var Ex = YAHOO.namespace("example"); Ex.content = YAHOO.util.Dom.get("content"); Ex.handlePagination = function (state) {

   // Show the appropriate content for the requested page
   Ex.content.className = "page" + state.page;
   
   // Update the Paginator"s state, confirming change
   Ex.paginator.setState(state);

}; // Create the Paginator widget and subscribe to its changeRequest event Ex.paginator = new YAHOO.widget.Paginator({

   rowsPerPage : 1,
   totalRecords : Ex.content.getElementsByTagName("div").length,
   containers : "paging"

}); Ex.paginator.subscribe("changeRequest", Ex.handlePagination); // Render the Paginator into the configured container(s) Ex.paginator.render(); }); </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>


Manually rendering Paginator UI Components

   <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>Manually rendering Paginator UI Components</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" /> <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>


<style type="text/css"> .yui-skin-sam .yui-pg-container { margin: 0; } .yui-skin-sam .yui-pg-current { margin-right: 15px; } .yui-skin-sam .yui-pg-previous {

   float: left;
   padding: 3px 5px;

} .yui-skin-sam .yui-pg-next {

   float: right;
   padding: 3px 5px;

} .yui-skin-sam span.yui-pg-next, .yui-skin-sam span.yui-pg-previous {

   display: none;

}

  1. tbl,
  2. report,
  3. paging {
   width: 400px;
   margin: 0 auto;

}

  1. report {
   color: #fff;
   background: #ccc;
   font-size: 200%;
   margin-bottom: 1em;
   text-align: right;

}

  1. demo table {
   border-collapse: collapse;
   color: #333;
   width: 100%;

}

  1. demo th {
   border-bottom: 4px solid #999;
   color: #444;
   font: normal 125%/100% Trebuchet MS, Arial, sans-serif;
   padding: 0 6px;

}

  1. demo tbody {
   background: #fff;
   border-left: 1px solid #ccc;
   border-right: 1px solid #ccc;

}

  1. demo tbody td {
   border-bottom: 1px solid #eee;
   padding: 5px;

}

  1. demo tfoot td {
   overflow: hidden;

} </style>

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

Manually rendering Paginator UI Components

If you have a UI where it doesn"t make sense to place all controls in a single container (or set of containers), you can place individual UI Components manually outside Paginator"s configured container(s).

For this example, we"ll create a table from a data array and render a few controls into the generated <tfoot> using the Paginator"s template. We"ll also subscribe to the Paginator"s render event with a callback that renders a CurrentPageReport UI Component into a <div> above the table.

<script type="text/javascript" src="yui_2.7.0b-assets/paginator-assets/inventory.js"></script> <script type="text/javascript"> YAHOO.util.Event.onDOMReady(function () { var Ex = YAHOO.namespace("example"),

   d  = document;

/* Convenience functions for building the DOM structure */ Ex.DOM = {

   create : function (el,innerHTML) {
       el = el && el.nodeName ? el : d.createElement(el);
       if (el && innerHTML !== undefined) {
           el.innerHTML = innerHTML;
       }
       return el;
   },
   add : function (par, child, innerHTML) {
       par = par && YAHOO.util.Dom.get(par);
       if (par && par.appendChild) {
           child = Ex.DOM.create(child,innerHTML);
           if (child) {
               par.appendChild(child);
           }
       }
       return child;
   }

}; /* Table generation/maintenance API */ Ex.table = {

   table   : null,
   columns : ["Item","Quantity","Description"],
   pageSize: 5,
   data    : null,
   tbody   : [],
   tfoot   : null,
   load : function (data) {
       if (YAHOO.lang.isArray(data)) {
           this.data = data;
           this.tbody = [];
       }
       return this;
   },
   render : function (container) {
       if (!this.table) {
           container = (container && YAHOO.util.Dom.get(container)) || d.body;
           var thead, tbody, row, cell, i, len;
           this.table = Ex.DOM.create("table");
           thead = Ex.DOM.add(this.table,"thead");
           row   = Ex.DOM.add(thead,"tr");
           for (i=0,len=this.columns.length; i<len; ++i) {
               Ex.DOM.add(row,"th",this.columns[i]);
           }
           this.tfoot = Ex.DOM.add(this.table,"tfoot");
           cell = Ex.DOM.add(Ex.DOM.add(this.tfoot,"tr"),"td");
           cell.colSpan = this.columns.length;
           if (this.data) {
               this.showPage(1);
           } else {
               row  = Ex.DOM.create("tr");
               cell = Ex.DOM.add(row,"td","No Data");
               cell.colSpan = this.columns.length;
               Ex.DOM.add(Ex.DOM.add(this.table,"tbody"),row);
           }
           container.innerHTML = "";
           Ex.DOM.add(container,this.table);
       }
       return this;
   },
   showPage : function (page) {
       var cur, tbody, row, i, j, len, limit;
       if (this.table) {
           cur = this.table.getElementsByTagName("tbody")[0];
           if (YAHOO.lang.isNumber(page)) {
               tbody = this.tbody[page];
               if (!cur || cur !== tbody) {
                   if (!tbody) {
                       tbody = this.tbody[page] = Ex.DOM.create("tbody");
                       i = (page - 1) * this.pageSize;
                       limit  = Math.min(Ex.data.inventory.length,
                                         i + this.pageSize);
                       for (; i < limit; ++i) {
                           row = Ex.DOM.add(tbody,"tr");
                           for (j=0,len=this.columns.length; j<len; ++j) {
                               Ex.DOM.add(row,"td",
                                   this.data[i][this.columns[j]]);
                           }
                       }
                   }
                   if (cur) {
                       this.table.replaceChild(tbody,cur);
                   } else {
                       Ex.DOM.add(this.table,tbody);
                   }
               }
           }
       }
       return this;
   }

};

Ex.handlePagination = function (state) {

   Ex.table.showPage(state.page);
   Ex.paginator.setState(state);

}; Ex.paginator = new YAHOO.widget.Paginator({

   rowsPerPage  : Ex.table.pageSize,
   totalRecords : Ex.data.inventory.length,
   containers   : d.createElement("div"),
   template              : "{PreviousPageLink}{NextPageLink}",
   pageReportTemplate    : "Page {currentPage} of {totalPages}",
   previousPageLinkLabel : "previous",
   nextPageLinkLabel     : "next"

}); Ex.paginator.subscribe("changeRequest", Ex.handlePagination); Ex.paginator.subscribe("render", function () {

   var pageReport, pageReportNode, report;
   report = YAHOO.util.Dom.get("report");
   // Instantiate the UI Component
   pageReport = new YAHOO.widget.Paginator.ui.CurrentPageReport(Ex.paginator);
   // render the UI Component, passing an arbitrary string (the ID of the
   // destination container by convention)
   pageReportNode = pageReport.render("report");
   // Append the generated node into the container
   report.appendChild(pageReportNode);

});

// Render the UI Ex.table.load(Ex.data.inventory).render("tbl"); // Render the Paginator controls into the off DOM div passed as a container // just to illustrate that it is possible to do so. Ex.paginator.render(); // Add the Paginator"s configured container to the table"s tfoot. Ex.DOM.add(Ex.table.tfoot.rows[0].cells[0],Ex.paginator.getContainerNodes()[0]); }); </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>


Partially revealing previous and next items

   <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>Carousel: Partially revealing previous and next items</title> <link rel="stylesheet" type="text/css" href="yui_2.7.0b-lib/fonts/fonts.css"> <link type="text/css" rel="stylesheet" href="yui_2.7.0b-lib/carousel/assets/skins/sam/carousel.css"> <script src="yui_2.7.0b-lib/yahoo-dom-event/yahoo-dom-event.js"></script> <script src="yui_2.7.0b-lib/element/element-min.js"></script> <script src="yui_2.7.0b-lib/carousel/carousel-min.js"></script> </head> <body class="yui-skin-sam">

Partially revealing previous and next items

This example showcases a simple yet powerful feature of the <a href="http://developer.yahoo.ru/yui/carousel/">YUI Carousel Control</a>. In this example, the Carousel displays the previous and next elements partially, giving a sneak peak of the upcoming image to the user. The revealAmount configuration setting accepts the percentage of the width of an item to reveal.

<script>

   (function () {
       var carousel;
               
       YAHOO.util.Event.onDOMReady(function (ev) {
           var carousel    = new YAHOO.widget.Carousel("container", {
                       revealAmount: 25
               });
                       
           carousel.render(); // get ready for rendering the widget
           carousel.show();   // display the widget
       });
   })();

</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>


Rendering controls into multiple containers

   <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>Rendering controls into multiple containers</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" /> <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>


<style type="text/css"> /* override some skin styles */ .yui-skin-sam span.yui-pg-container {

   display: inline;

} .yui-skin-sam .yui-pg-current {

   margin: 0;

} .yui-skin-sam #demo .yui-pg-container a:link, .yui-skin-sam #demo .yui-pg-container a:active, .yui-skin-sam #demo .yui-pg-container a:visited, .yui-skin-sam #demo .yui-pg-container a:hover, .yui-skin-sam #demo .yui-pg-container span.yui-pg-previous, .yui-skin-sam #demo .yui-pg-container span.yui-pg-next {

   background: #fde;
   color: #f3c;
   text-decoration: none;
   border: 3px solid #f9c;
   padding: 0 3px;
   font-size: 130%;
   font-weight: bold;

} .yui-skin-sam #demo .yui-pg-container span.yui-pg-previous, .yui-skin-sam #demo .yui-pg-container span.yui-pg-next {

   background: #eee;
   color: #a6a6a6;
   border: 3px double #ccc;

} .yui-skin-sam #demo .yui-pg-container a:hover {

   background: #f9c;
   color: #fff;

} /* demo specific styles */

  1. demo h2 {
   border: none;
   border-bottom: 1ex solid #aaa;
   color: #333;
   font-size: 1.5em;
   line-height: 65%;
   margin-top: 0;

}

  1. content {
   margin: 0 0 0 4em;
   padding-top: 1em;

}

  1. content li {
   color: #f6c;
   font: bold italic 200%/.5 Arial, sans-serif;
   padding: 1px 0;
   margin: 0;

}

  1. content li p {
   color: #555;
   font: normal 50% Arial, sans-serif;
   margin: 0;
   line-height: 2;

}

  1. p_container {
   text-align: center;

} </style>

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

Rendering controls into multiple containers

In this example, we will add pagination to an ordered list. Some things to note:

  • Pagination controls are added in a <span> as well as in a <p>.
  • All included pagination controls use inline elements, so the containers needn"t be block elements.
  • A custom skin treatment has been applied.
  • State changes made to the Paginator propagate to all controls in all containers.

1987 US Billboard Top 40!

Random content with pagination controls embedded inline. Suspendisse vestibulum dignissim quam. Integer vel augue. Phasellus nulla purus, interdum ac, and here they are. and now back to random content habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas.

<script type="text/javascript" src="yui_2.7.0b-assets/paginator-assets/top40.js"></script> <script type="text/javascript"> YAHOO.util.Event.onDOMReady(function () { // Place code in the YAHOO.example namespace var Ex = YAHOO.namespace("example"); Ex.content = YAHOO.util.Dom.get("content"); Ex.handlePagination = function (state) {

   // Gather the content for the requested page
   var startIndex = state.recordOffset,
       recs = Ex.data.top40.slice(startIndex, startIndex + state.rowsPerPage);
   // Update the content UI
   Ex.content.start = startIndex + 1;
Ex.content.innerHTML = "
  • "+recs.join("

  • ")+"

  • ";
       // Confirm state change with the Paginator
       Ex.paginator.setState(state);
    

    }; Ex.paginator = new YAHOO.widget.Paginator({

       rowsPerPage : 10,
       totalRecords : Ex.data.top40.length,
       containers : ["span_container","p_container"],
       template : "{PreviousPageLink} {CurrentPageReport} {NextPageLink}",
       previousPageLinkLabel : "<",
       nextPageLinkLabel : ">",
       pageReportTemplate : "{startRecord} - {endRecord} of the Top {totalRecords}"
    

    });

    Ex.paginator.subscribe("changeRequest", Ex.handlePagination); Ex.paginator.render(); Ex.handlePagination(Ex.paginator.getState()); }); </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>


    Server-side Pagination and Sorting for Dynamic Data

       <source lang="html4strict">
    
    


    <head>

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

    <title>Server-side Pagination and Sorting for Dynamic Data</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>

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

    Server-side Pagination and Sorting for Dynamic Data

    This example enables server-side sorting and pagination for data that is dynamic in nature.

    <script type="text/javascript"> YAHOO.example.DynamicData = function() {

       // Column definitions
       var myColumnDefs = [ // sortable:true enables sorting
           {key:"id", label:"ID", sortable:true},
           {key:"name", label:"Name", sortable:true},
           {key:"date", label:"Date", sortable:true, formatter:"date"},
           {key:"price", label:"Price", sortable:true},
           {key:"number", label:"Number", sortable:true}
       ];
       // Custom parser
       var stringToDate = function(sData) {
           var array = sData.split("-");
           return new Date(array[1] + " " + array[0] + ", " + array[2]);
       };
       
       // DataSource instance
       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: [
               {key:"id", parser:"number"},
               {key:"name"},
               {key:"date", parser:stringToDate},
               {key:"price",parser:"number"},
               {key:"number",parser:"number"}
           ],
           metaFields: {
               totalRecords: "totalRecords" // Access to value in the server response
           }
       };
       
       // DataTable configuration
       var myConfigs = {
           initialRequest: "sort=id&dir=asc&startIndex=0&results=25", // Initial request for first page of data
           dynamicData: true, // Enables dynamic server-driven data
           sortedBy : {key:"id", dir:YAHOO.widget.DataTable.CLASS_ASC}, // Sets UI initial sort arrow
           paginator: new YAHOO.widget.Paginator({ rowsPerPage:25 }) // Enables pagination 
       };
       
       // DataTable instance
       var myDataTable = new YAHOO.widget.DataTable("dynamicdata", myColumnDefs, myDataSource, myConfigs);
       // Update totalRecords on the fly with value from server
       myDataTable.handleDataReturnPayload = function(oRequest, oResponse, oPayload) {
           oPayload.totalRecords = oResponse.meta.totalRecords;
           return oPayload;
       }
       
       return {
           ds: myDataSource,
           dt: 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>