当程序在处理大量数据时,往往会用到表格,在本章要实现的购物网站中,使用表格的地方很多,例如产品列表、购物清单列表、订单列表等。程序根据各种列表的要求,创建一个通用的表格类,来完成购物网站中关于表格输出的要求。广州网站设计
要创建一个可以用于多种情况下的类,要先考虑类需要完成的功能。本小节需要实现的表格类,需要完成以下功能。
记录列表显示:根据参数,以表格的形式,列出记录内容。广州网站建设
列表头:根据参数,创建一个表头,以帮助用户理解数据。
表单:使用一个表单,来管理表格中的表单控制。
与列表相关的表单控件:将表格记录与表单控件进行绑定,在提交表单时,用来获取记录的相关数据。
与表单提交相关的表单控制:当表单提交时,根据这个控制来判断需要调用的 代码。
工具栏:根据参数创建一行工具栏,生成的工具栏按钮,可以控制表单提交的动作等内容。
JavaScript支持:为了能够更加灵活地控制表单控件,加入jQuery框架支持,完成其中与表单相关的JavaScript代码操作。
注意:jQuery是一个快速、简洁的JavaScript库,使用户能更方便地处理HTML documents、events、实现动画效果,并且方便地为网站提供AJAX交互。
在规划好需要的功能后,就可以开始创建通用表格类的代码了。通用表格类的代码保存在"./class"目录下,文件名为table.class.php。详细代码如下所示。
广州网站设计
- <?php
- class table {
- public $_jspath = "templates/js/"; //保存JavaScript文件的目录
- public $_name = ""; //保存名称
- public $_action = ""; //保存动作变量
- function table($name = "tableForm") {
- $this->_name = $name;
- }
- function normal($header, $data = NULL, $control = false, $toolbar = NULL, $width = '100%') {
- $html = $this->js ( $toolbar ); //加载JavaScript文件
- $html .= '<form name="' . $this->_name . '" id="' . $this->_name . '"method="post" action="' . $this->_action . '">';
- if ($toolbar != NULL) {
- foreach ( $toolbar as $k => $v ) { //遍历工具栏数组,创建工具栏
- $html .= '<input type="button" name="' . $k . '" id="' . $k . '" value="' . $v ["value"] . '">';
- }
- }
- $html .= '<table class="tableList" style="width:' . $width . '">';
- $html .= '<tr>';
- if ($control == true) { //根据参数,决定是否显示表单控制
- $html .= '<th><input type="checkbox" name="toggle" id="toggle" value="" onClick="' . $this->_name . '_checkAll(' . (count ( $data )). ');" /></th>';
- }
- foreach ( $header as $v ) {
- $html .= "<th>" . $v . "</th>"; //输出表头内容
- }
- $html .= "</tr>";
- $rs = 0;
- $cb = 0;
- if ($data != NULL) {
- if (is_object ( $data )) {
- $data = ( array ) $data; //转换数据为数组
- }
- foreach ( $data as $k => $v ) {
- //兼容性处理开始
- if (is_object ( $v )) {
- $v = ( array ) $v; //如果传入的数据是对象,将其转换为数组
- }
- //如果要处理的数据不是数组,跳过此次循环
- if (gettype ( $v ) != "array") {
- continue;
- }
- //使用array_values将关联数组转换为数字为键名的数组
- $v = array_values ( $v );
- //兼容性处理结束
- if ($rs == 2) {
- $rs = 0;
- }
- $html .= '<tr class="row' . $rs . '" id="' . $k . '">';
- if ($control == true) { //允许显示表格控制时,输出表格控制内容
- $html .= '<td><input type="checkbox" id="cb' . $cb . '"
- name="cid[]" value="' . $v [0] . '" onclick="' .
- $this->_name . '_isChecked(this.checked);" /></td>';
- }
- for($i = 0; $i < count ( $v ); $i ++) {
- $html .= "<td>" . $v [$i] . "</td>";
- }
- $html .= "</tr>";
- $rs ++;
- $cb ++;
- }
- }
- $html .= '</table>';
- $html .= '<input type="hidden" name="boxchecked" id="' . $this->_
- name . '_boxchecked" value="0"><input type="hidden" name="do" id="' .
- $this->_name . '_do" value=""></form>';
- return $html;
- }
- //JS判断函数
- function js($toolbar = NULL) {
- //加载jQuery文件
- $js = "<script type='text/JavaScript' src='" . $this->_jspath .
- "jquery-1.2.6.js'></script>";
- $js .= '<script type="text/JavaScript">';
- if ($toolbar != "") {
- $js .= '$(document).ready(function(){';
- foreach ( $toolbar as $k => $v ) { //循环表格标签变量
- $js .= '$("#' . $k . '").click(function(){
- $("#' . $this->_name . '").attr("action","' . $v
- ["action"] . '");
- $("#' . $this->_name . '_do").val("' . $k . '");
- if($("#' . $this->_name . '_boxchecked").val()==0){
- alert("请选择要处理的记录!");
- }else{
- $("#' . $this->_name . '").submit(); //发送请求
- }
- });';
- }
- $js .= "});";
- }
- //创建与工具栏有关的JavaScript代码
- $js .= "
- function " . $this->_name . "_checkAll( n, fldName ) {
- if (!fldName) {
- fldName = 'cb';
- }
- var f = document." . $this->_name . ";
- var c = f.toggle.checked;
- var n2 = 0;
- for (i=0; i < n; i++) {
- cb = eval( 'f.' + fldName + '' + i );
- if (cb) {
- ccb.checked = c;
- n2++;
- }
- }
- if (c) {
- document." . $this->_name . ".boxchecked.value = n2;
- } else {
- document." . $this->_name . ".boxchecked.value = 0;
- }
- }
- function " . $this->_name . "_isChecked(isitchecked){
- if (isitchecked == true){
- document." . $this->_name . ".boxchecked.value++;
- }
- else {
- document." . $this->_name . ".boxchecked.value--;
- }
- if(document." . $this->_name . ".boxchecked.value == 0){
- document." . $this->_name . ".toggle.checked = false;
- }
- }
- </script>";
- return $js;
- }
- }
- ?>
广州网站建设



