0 Comments

通用表格类(1)

发布于:2012-11-26  |   作者:广州网站建设  |   已聚集:人围观
通用表格类(1)

当程序在处理大量数据时,往往会用到表格,在本章要实现的购物网站中,使用表格的地方很多,例如产品列表、购物清单列表、订单列表等。程序根据各种列表的要求,创建一个通用的表格类,来完成购物网站中关于表格输出的要求。广州网站设计

要创建一个可以用于多种情况下的类,要先考虑类需要完成的功能。本小节需要实现的表格类,需要完成以下功能。

记录列表显示:根据参数,以表格的形式,列出记录内容。广州网站建设

列表头:根据参数,创建一个表头,以帮助用户理解数据。

表单:使用一个表单,来管理表格中的表单控制。

与列表相关的表单控件:将表格记录与表单控件进行绑定,在提交表单时,用来获取记录的相关数据。

与表单提交相关的表单控制:当表单提交时,根据这个控制来判断需要调用的      代码。

工具栏:根据参数创建一行工具栏,生成的工具栏按钮,可以控制表单提交的动作等内容。

JavaScript支持:为了能够更加灵活地控制表单控件,加入jQuery框架支持,完成其中与表单相关的JavaScript代码操作。

注意:jQuery是一个快速、简洁的JavaScript库,使用户能更方便地处理HTML documents、events、实现动画效果,并且方便地为网站提供AJAX交互。

在规划好需要的功能后,就可以开始创建通用表格类的代码了。通用表格类的代码保存在"./class"目录下,文件名为table.class.php。详细代码如下所示。

广州网站设计


  1. <?php 
  2. class table {  
  3.     public $_jspath = "templates/js/";          //保存JavaScript文件的目录  
  4.     public $_name = "";                         //保存名称  
  5.     public $_action = "";                       //保存动作变量  
  6.     function table($name = "tableForm") {  
  7.         $this->_name = $name;  
  8.     }  
  9.     function normal($header, $data = NULL, $control = false, $toolbar = NULL,   $width = '100%') {  
  10.         $html = $this->js ( $toolbar );     //加载JavaScript文件  
  11.         $html .'<form name="' . $this->_name . '" id="' . $this->_name .      '"method="post" action="' . $this->_action . '">';  
  12.         if ($toolbar != NULL) {  
  13.             foreach ( $toolbar as $k => $v ) {  //遍历工具栏数组,创建工具栏  
  14.                 $html .'<input type="button" name="' . $k . '" id="' . $k .               '" value="' . $v ["value"] . '">';  
  15.             }  
  16.         }  
  17.         $html .'<table class="tableList" style="width:' . $width . '">';  
  18.         $html .'<tr>';  
  19.         if ($control == true) {             //根据参数,决定是否显示表单控制  
  20.             $html .'<th><input type="checkbox" name="toggle" id="toggle"          value="" onClick="' . $this->_name . '_checkAll(' . (count              ( $data )). ');" /></th>';  
  21.         }  
  22.         foreach ( $header as $v ) {  
  23.             $html ."<th>" . $v . "</th>"; //输出表头内容  
  24.         }  
  25.         $html ."</tr>";  
  26.         $rs = 0;  
  27.         $cb = 0;  
  28.         if ($data != NULL) {  
  29.             if (is_object ( $data )) {  
  30.                 $data = ( array ) $data;        //转换数据为数组  
  31.             }  
  32.             foreach ( $data as $k => $v ) {  
  33.                 //兼容性处理开始  
  34.                 if (is_object ( $v )) {  
  35.                     $v = ( array ) $v;  //如果传入的数据是对象,将其转换为数组  
  36.                 }  
  37.                 //如果要处理的数据不是数组,跳过此次循环  
  38.                 if (gettype ( $v ) != "array") {  
  39.                     continue;  
  40.                 }  
  41.                 //使用array_values将关联数组转换为数字为键名的数组  
  42.                 $v = array_values ( $v );  
  43.                 //兼容性处理结束  
  44.                 if ($rs == 2) {  
  45.                     $rs = 0;  
  46.                 }  
  47.                 $html .'<tr class="row' . $rs . '" id="' . $k . '">';  
  48.                 if ($control == true) { //允许显示表格控制时,输出表格控制内容  
  49.                     $html .'<td><input type="checkbox" id="cb' . $cb . '"   
  50.                     name="cid[]" value="' . $v [0] . '" onclick="' .   
  51.                     $this->_name . '_isChecked(this.checked);" /></td>';  
  52.                 }  
  53.                 for($i = 0; $i < count ( $v ); $i ++) {  
  54.                     $html ."<td>" . $v [$i] . "</td>";  
  55.                 }  
  56.                 $html ."</tr>";  
  57.                 $rs ++;  
  58.                 $cb ++;  
  59.             }  
  60.         }  
  61.         $html .'</table>';  
  62.         $html .'<input type="hidden" name="boxchecked" id="' . $this->_  
  63.         name . '_boxchecked" value="0"><input type="hidden" name="do" id="' .   
  64.         $this->_name . '_do" value=""></form>';  
  65.         return $html;  
  66.     }  
  67.     //JS判断函数  
  68.     function js($toolbar = NULL) {  
  69.         //加载jQuery文件  
  70.         $js = "<script type='text/JavaScript' src='" . $this->_jspath .   
  71.         "jquery-1.2.6.js'></script>";  
  72.         $js .'<script type="text/JavaScript">';  
  73.         if ($toolbar != "") {  
  74.             $js .'$(document).ready(function(){';  
  75.             foreach ( $toolbar as $k => $v ) {      //循环表格标签变量  
  76.                 $js .'$("#' . $k . '").click(function(){  
  77.                     $("#' . $this->_name . '").attr("action","' . $v   
  78.                     ["action"] . '");  
  79.                     $("#' . $this->_name . '_do").val("' . $k . '");  
  80.                     if($("#' . $this->_name . '_boxchecked").val()==0){  
  81.                         alert("请选择要处理的记录!");  
  82.                     }else{  
  83.                         $("#' . $this->_name . '").submit();    //发送请求  
  84.                     }  
  85.                 });';  
  86.             }  
  87.             $js ."});";  
  88.         }  
  89.         //创建与工具栏有关的JavaScript代码  
  90.         $js .= "  
  91.         function " . $this->_name . "_checkAll( n, fldName ) {  
  92.             if (!fldName) {  
  93.                fldName = 'cb';  
  94.             }  
  95.             var f = document." . $this->_name . ";  
  96.             var c = f.toggle.checked;  
  97.             var n2 = 0;  
  98.             for (i=0; i < n; i++) {  
  99.                 cb = eval( 'f.' + fldName + '' + i );  
  100.                 if (cb) {  
  101.                     ccb.checked = c;  
  102.                     n2++;  
  103.                 }  
  104.             }  
  105.             if (c) {  
  106.                 document." . $this->_name . ".boxchecked.value = n2;  
  107.             } else {  
  108.                 document." . $this->_name . ".boxchecked.value = 0;  
  109.             }  
  110.         }  
  111.         function " . $this->_name . "_isChecked(isitchecked){  
  112.             if (isitchecked == true){  
  113.                 document." . $this->_name . ".boxchecked.value++;  
  114.             }  
  115.             else {  
  116.                 document." . $this->_name . ".boxchecked.value--;  
  117.             }  
  118.             if(document." . $this->_name . ".boxchecked.value == 0){  
  119.                 document." . $this->_name . ".toggle.checked = false;     
  120.             }  
  121.         }  
  122.         </script>";  
  123.         return $js;  
  124.     }  
  125. }  
  126. ?> 

广州网站建设

标签:
飞机