Ajax 和 XML: 用于表格的 Ajax下


清单 5. index.html

<html>
  <head>
  <script src="prototype.js"></script>
  </head>
  <body>
  <div>
  <a href="javascript:void updateTable(0);">1</a> |
  <a href="javascript:void updateTable(1);">2</a> |
  <a href="javascript:void updateTable(2);">3</a> |
  <a href="javascript:void updateTable(3);">4</a> |
  <a href="javascript:void updateTable(4);">5</a> |
  <a href="javascript:void updateTable(5);">6</a>
  </div>
  <div id="states">
  </div>
  <script>
  function updateTable( start ) {
  new Ajax.Updater( 'states', 'stats.php?start='+(start*10)+'&count=10',
  { method: 'get' } );
  }
  updateTable( 0 );
  </script>
  </body>
  </html>

  大多数页面都位于指向表格的各种页面的链接中。然后,states <div> 标签收到来自 stats.php 页面的表格内容。

  为了运行多页面的示例,我需要一个较大的数据集,因此我找到了一些美国人口统计信息并创建了一个 PHP 函数 getdata,该函数返回一个数组,其中包含这些年来各个州及其人口。清单 6 显示了该函数的一段摘录。

  清单 6. data.php

<?php
  function getdata()
  {
  $population = array();
  $population []=
  array( 'Alabama',4447100,4527166,4596330,4663111,4728915,4800092,4874243 );
  $population []= array( 'Alaska',626932,661110,694109,732544,774421,820881,867674 );
  ...
  $population []= array( 'Wyoming',493782,507268,519886,528005,530948,529031,522979 );
  return $population;
  }
  ?>

  现在,为了获得数据的各个部分,我创建了一个 stats.php 页面,该页面提供了一个开始和结束索引, 返回一个只包含该部分数据的 HTML 表格。清单 7 显示了该文件。

  清单 7. stats.php

 <table>
  <?php
  require 'data.php';
  $data = getdata();
  $start = 0;
  $count = count( $data );
  if ( array_key_exists( 'start', Ajax 和 XML: 用于表格的 Ajax下 - 站长学院

Ajax 和 XML: 用于表格的 Ajax下

2008-10-11 22:41:00查看学习心得
{GetProperty(Content)} 本文作者:
« 
» 

Copyright © 2016 phpStudy | 豫ICP备2021030365号-3