PHPGRID3.0用户手册

目录

PHPGRID3.0用户手册... 1

1简介... 1

2 PHPGRID3.0新特性... 1

3概览/原理... 1

3安装... 3

4 PHPGRID的类型... 3

5 定制Datagrid. 3

6 简单的使用示例... 4

6.1. 只需要指定一条SQL. 4

6.2 只需要指定表名和字段名... 4

6.3 查询... 5

6.4 显示控制... 5

6.5隐藏行功能... 5

6.6使用表单... 6

6.7权限控制... 6

6.8启用缓存... 6

7.复杂功能的例子... 6

7.1指定表单字段类型... 6

7.2指定表单显示几列... 7

7.3给表单字段附加属性... 8

7.4设置表单验证... 9

7.5使用在线编辑器... 10

7.6显示多个grid. 12

7.7更换皮肤... 13

8.高级应用... 15

8.1实现字段值的替换... 15

8.2重写表单... 17

 

 

1简介

PHPGRID是一个用于快速开发MIS系统(ERP,CRM,HR),网站后台,演示程序的PHP类库,不仅具有数据

添加编辑删除的基本功能,还具有表单自动生成,排序,分页,查询,权限设定,缓存的功能,而且这些功能是可

定制的。对于特殊的需求,您只需编写少量的代码来继承于PHPGRID PHPGRID适合那些懒惰而又聪明的PHP

程序员,PHPGRID将使你从无味重复的工作中解放出来。

 

2 PHPGRID3.0新特性

 

3概览/原理

       PHPGRID由如下模块组成:

      

 

注意,上图并不是一个标准的UML图,该图只是为了大家明白PHPGRID的结构。其中Datagrid类是核心类,它

将根据提供的参数构造SQL并从数据库中取得数据,然后并接成类似如下的HTML,最后返回。

生成HTML的例子:

 

       <form action="" method="get" name="grid_search"><input

    type="hidden" name="datagrid_action" value="search" /> <input

    type="hidden" name="datagrid_page" value="1" />

 

<table>

    <tr>

       <td colspan="3"><span>查询条件</span></td>

    </tr>

    <tr>

       <td>武将名称</td>

       <td><select name="ge_name_exp">

           <option value="=">=</option>

           <option value="!=">!=</option>

           <option value="LIKE">LIKE</option>

           <option value="LIKE %...%">LIKE %...%</option>

           <option value="NOT LIKE">NOT LIKE</option>

           <option value="IS NULL">IS NULL</option>

           <option value="IS NOT NULL">IS NOT NULL</option>

       </select></td>

       <td><input type="text" name="ge_name_value" size="25"

           class="textfield" id="ge_name_value" /></td>

    </tr>

 

    <tr>

       <td colspan="6">

       <div align="center"><input type="submit" name="Submit"

           value="提交"> &nbsp;&nbsp;<input type="reset" name="Submit2"

           value="重置"></div>

       </td>

    </tr>

</table>

<br>

</form>

<table summary=" table of Datagrid">

    <caption>武将列表</caption>

    <thead>

       <tr>

           <td scope="col"><a href="?datagrid_order=ge_userid">用户ID</a></td>

           <td scope="col"><a href="?datagrid_order=ge_cityid">城市ID</a></td>

           <td scope="col"><a href="?datagrid_order=ge_name">武将名称</a></td>

           <td scope="col"><a href="?datagrid_order=ge_equipment">装备信息</a></td>

           <td width="10%" align="center" scope="col"><a href=#>操作</a></td>

       <tr>

    </thead>

    <tbody>

       <tr>

           <td>2</td>

           <td>2222222222222</td>

           <td>Name</td>

           <td></td>

           <td align="center"><a title="body_control"

              href="?datagrid_action=edit&ge_id=1&">编辑</a> <a href="#"

              title="body_control"

              onClick="if(isconfirm()) window.location.href='?datagrid_action=delete&ge_id=1&'">删除</a></td>

       </tr>

    </tbody>

    <tfoot>

       <tr>

           <td colspan="21">总数:30 &nbsp; 分页</td>

       </tr>

    </tfoot>

</table>    

 

 

文件目录结构:

      

   phpgrid3.0beta

   |

   |-...

   |

   +--cache                         存放缓存目录                    

   +--js                       Javascript目录        

   +--css                            Css样式目录,用于控制界面的皮肤                     

   +--examples                   一些典型的例子                          

   +--images                     

   +--lang                          语言包,支持多国语言

   +--uploadfiles                 默认的文件上传目录,你可以在代码指定另外一个目录                    

   |-...

   |-config.inc.php              全局配置文件,主要设置数据库连接,时区和语言

   |-Dataview.php                Phpgrid3.0的基类文件,该类只是显示一个表格,类似ASP.NETdataview

   |-Datagrid.php                Phpgrid3.0的核心文件,继承于Dataview,具有Dataview全部和附件特性

   |-Mysql.php                    连接MYSQL的类库,支持PDOMYSQL函数,如果安装了PDO扩展那么将不使用MYSQL函数

3安装

Phpgrid3.0压缩包释放到你的PHP程序目录,在你需要的地方引入Datagrid.php就可以使用了。

如何运行examples中例子?

步骤如下:

1.MYSQL数据库中建立任意一个数据库,

2.然后在该数据库下执行example_sql.txt中的SQL语句,将数据导入到该库,

3.接着修改config.inc.php的数据库连接项,

4.最后在浏览器中访问example下的PHP文件。

4 PHPGRID的类型

选择类型

如果你想生成拥有添加删除修改甚至表单的页面,那么你需要实例化于Datagrid类,而如果你只想显示数据那么你只需要实例化于Dataview

最简单的Dtagrid例子:

 

<?php 

 

include_once('../Datagrid.php');

$expamle=new Datagrid();

$expamle->sql="SELECT * FROM `customer`";

$html=$expamle->display();

echo $html;

?>

 

也可以在这个页面中加入其他任意代码,如css样式表,菜单等。

示例:

 

<?php 

include_once('../Datagrid.php');

$expamle=new Datagrid();

$expamle->sql="SELECT * FROM `customer`";

$html=$expamle->display();

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

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

<link type="text/css" rel="stylesheet" href="../css/bluedream.css" />

<title>Customers</title>

 <body  >

    <table >

      <tr>

        <td>

        <a href="?" >back</a>,&nbsp;&nbsp;Add form: 

        <a href="?datagrid_action=new" >here</a>

        </td>

      </tr>

    </table>

    <?php

    echo $html;

    ?>

</body>

</html>

 

5 定制Datagrid

你可以非常方便的设定Datagrid的显示,排序,权限,查询,缓存。有如下设定:

 

    //基本参数

public  $is_use_cache=false; //是否启用缓存,默认不启用

    public  $role="v,u,a,d";     //v,u,a,d分表表示具有查看,更新,添加,删除的权限

    public $pageRow   = 10;      //每页显示几行

 

    //显示设置  注意下面设置与前面的权限设置不同,下面只是显示的设置不是权限

    public $displaySearch       =true; //是否显示表标题的HTML

    public $displayTableTitle   =true; //是否显示表标题的HTML

    public $displayTableHeader  =true; //是否显示表头的HTML

    public $displayTableBody     =true; //是否显示表内容的HTML

    public $displayTableFooter  =true; //是否显示表底部的HTML

    public $displayFooter       =true; //是否显示底部的HTML

    public $displayControl      =true; //是否显示操作

    public $displayOrder         =true; //是否可以排序

    public $displayHideField     =false;//显示隐藏列

 

    //数据设置

    public $rows=array();     //Datagrid从数据库中得到数据,这里可以不设置

    public $sql="";            //通过设置该值,取得特定数据

    public $key="";            //SQL中主键字段,如果为空Datagrid自动取得主键值    

   public $tables="";         //SQL查询中表名称,可以是单表,多表,或者连接表,如果已经设置前面sql,这里可以不需要设置

    public $where="";          //设置查询的条件,可以动态的构建条件,用于取得你所需要数据

    public $fields           =array();//要取得$tables字段格式类似如下:array(’id’,’name’,’pic as picture’),可以使用as 别名

    public $fieldDisplays    =array();//指定$fields中要显示的字段,格式如下:array(’name’=>’名称’,’ picture’=>’照片’),数组键值为字段名,而数组值为显示的字符串

    public $hideField        ="";     //设置隐藏列值的字段,如果显示设置允许显示,该项才会生效

 

    //查询的字段,下标为字段名,值为显示的名称

    public $searchFields     =array();//设置查询的字段,格式类似如:array(’name’=>’名称’,’ picture’=>’照片’),$fieldDisplays 相似

 

    //表单设定

    public $formTable        ="";     //设置表单对应的表名,只可以设置一个表,如果你要使用表单,必须给该项设置值

    public $formFields       =array();//设置表单的项,这些表单项为$formTable中的字段,格式如下:array(’name’=>’名称’,’ picture’=>’照片’),$fieldDisplays 相似

   public $formTypes        =array();//为表单项指定类型,改项目是可选的,可以指定:label,input,date,text,textarea,select,list,hidden,simple_editor,fckeditor.格式如下:array(’name’=>’text’,

’pic’=>’file’,

’memo’=>’simple_editor’,

’sex’=>array(’select’,array(1,2)

);

    public $formPrimkey      ="";   //指定$formTable中的主键,如果你要使用表单,必须给该项设置值

    public $formDefaults     =array();//指定表单项中对应的默认值,格式如下array(’name’=>’请输入姓名’);

    public $formFieldsValidates =array();//设置表单项中的js验证,使用了livevalidation1.3 javascript库,格式如:array("name"=>"Presence","user_email"=>"Email","cus_phone"=>"Numericality, {minimum: 11}"), 关于livevalidation1的详细使用可以访问官方网站文档:http://livevalidation.com/

    public $formColsNum         =2;//表单将会显示几列

    public $formAttributes      =array();//自定义表单项中的HTML属性,格式举例:array("name"=>'style="background:#FFFF00" ',"user_email"=>'readonly="true" style="background:#CCCCCC"'),数组的键值为表单项的名称,数组元素值为其属性

   

    public $formUploadDir       ="uploadfiles/";//文件上传的默认存放路径,你可以更改此路径

    public $formUploadFiloeType=array("jpg","gif","png","pdf","rar","zip","doc","xls");//允许上传文件的格式

    public $formUploadMaxSize   =1048576;//文件上传的大小限制

 

 

 

6 简单的使用示例

6.1. 只需要指定一条SQL

 

<?php 

 

include_once('../Datagrid.php');

$expamle=new Datagrid();

$expamle->sql="SELECT * FROM `customer`";

$html=$expamle->display();

echo $html;

?>

 

6.2 只需要指定表名和字段名

 

<?php 

include_once('../Datagrid.php');

$expamle=new Datagrid();

$expamle->tables="`customer`";

//aim to-> SELECT `cus_id` ,`cus_name`,`cus_address`,`cus_phone`  FROM `customer` 

$expamle->fields=array("cus_id","cus_name","cus_address","cus_phone");

$expamle->fieldDisplays=array("cus_name"=>"Customer's name","cus_address"=>"Address","cus_phone"=>"Phone");

$expamle->title="List of customer";

$html=$expamle->display();

echo $html;

?>

 

6.3 查询

 

<?php 

include_once('../Datagrid.php');

$expamle=new Datagrid();

$expamle->tables="`customer`";

//aim to-> SELECT `cus_id` ,`cus_name`,`cus_address`,`cus_phone`  FROM `customer` 

$expamle->fields=array("cus_id","cus_name","cus_address","cus_phone");

$expamle->fieldDisplays=array("cus_name"=>"Customer's name","cus_address"=>"Address","cus_phone"=>"Phone");

$expamle->title="List of customer";

 

//define search fields

$expamle->searchFields=array("cus_name"=>"Customer's name", "cus_address"=>"Address");

 

$html=$expamle->display();

echo $html;

?>

 

 

6.4 显示控制

 

<?php 

 

include_once('../Datagrid.php');

$expamle=new Datagrid();

$expamle->tables="`customer`";

$expamle->fields=array("cus_id","cus_name","cus_address","cus_phone");

$expamle->fieldDisplays=array("cus_name"=>"Customer's name","cus_address"=>"Address","cus_phone"=>"Phone");

$expamle->title="List of customer";

 

$expamle->displayTableTitle=false; //no display Table Title

$expamle->displayOrder=false;      //no display order

$expamle->displayTableHeader=false;    //no display Table Header

$expamle->displaySearch=false;     //no display Search part

$expamle->displayControl=false;    //no display Control colums

$expamle->displayTableFooter=false;    //no display Footer

 

$html=$expamle->display();

echo $html;

?>

 

 

6.5隐藏行功能

 

<?php 

 

include_once('../Datagrid.php');

$expamle=new Datagrid();

$expamle->tables="`customer`";

//aim to-> SELECT `cus_id` ,`cus_name`,`cus_address`,`cus_phone`  FROM `customer` 

$expamle->fields=array("cus_id","cus_name","cus_address","cus_phone","cus_memo");

$expamle->fieldDisplays=array("cus_name"=>"Customer's name","cus_address"=>"Address","cus_phone"=>"Phone");

$expamle->title="List of customer";

$expamle->displayHideField=true;//先设置显示隐藏行

$expamle->hideField="cus_memo";/指定隐藏行的字段

 

$html=$expamle->display();

echo $html;

?>

 

 

6.6使用表单

 

<?php 

 

include_once('../Datagrid.php');

$expamle=new Datagrid();

$expamle->tables="`customer`";

//aim to-> SELECT `cus_id` ,`cus_name`,`cus_address`,`cus_phone`  FROM `customer` 

$expamle->fields=array("cus_id","cus_name","cus_address","cus_phone");

$expamle->fieldDisplays=array("cus_name"=>"Customer's name","cus_address"=>"Address","cus_phone"=>"Phone");

$expamle->title="List of customer";

 

// 设置表单属性

$expamle->formTable="`customer`";

$expamle->formFields=array("cus_name"=>"Customer's name","cus_address"=>"Address","cus_phone"=>"Phone");

 

$html=$expamle->display();

echo $html;

?>

 

 

6.7权限控制

 

<?php 

 

include_once('../Datagrid.php');

$expamle=new Datagrid();

$expamle->tables="`customer`";

//aim to-> SELECT `cus_id` ,`cus_name`,`cus_address`,`cus_phone`  FROM `customer` 

$expamle->fields=array("cus_id","cus_name","cus_address","cus_phone");

$expamle->fieldDisplays=array("cus_name"=>"Customer's name","cus_address"=>"Address","cus_phone"=>"Phone");

$expamle->title="List of customer";

$expamle->role="v,d,a";//设置此属性来设定权限

 // define form

$expamle->formTable="`customer`";

$expamle->formFields=array("cus_name"=>"Customer's name","cus_address"=>"Address","cus_phone"=>"Phone");

 

$html=$expamle->display();

echo $html;

?>

 

 

6.8启用缓存

启用缓存,需要对cache具有读写权限

 

<?php 

 

include_once('../Datagrid.php');

$expamle=new Datagrid();

$expamle->is_use_cache=true; //启用缓存

$expamle->tables="`customer`";

//aim to-> SELECT `cus_id` ,`cus_name`,`cus_address`,`cus_phone`  FROM `customer` 

$expamle->fields=array("cus_id","cus_name","cus_address","cus_phone");

$expamle->fieldDisplays=array("cus_name"=>"Customer's name","cus_address"=>"Address","cus_phone"=>"Phone");

$expamle->title="List of customer";

$html=$expamle->display();

echo $html;

?>

 

 

7.复杂功能的例子

7.1指定表单字段类型

      

<?php 

 

include_once('../Datagrid.php');

$expamle=new Datagrid();

$expamle->tables="`customer`";

//aim to-> SELECT `cus_id` ,`cus_name`,`cus_address`,`cus_phone`  FROM `customer` 

$expamle->fields=array("cus_id","cus_userid","cus_name","cus_email","cus_dept","cus_address","cus_phone","cus_pic");

$expamle->fieldDisplays=array("cus_name"=>"Customer's name","cus_userid"=>"User","cus_email"=>"Email","cus_dept"=>"Department","cus_address"=>"Address","cus_phone"=>"Phone","cus_pic"=>"Pictrue");

$expamle->title="List of customer";

 

$expamle->formTable="`customer`";

$expamle->formFields=$expamle->fieldDisplays;

 

$user_list=array(1=>"Mark",2=>"Jockson",3=>"Tom",4=>"Sun");

$expamle->formTypes=array('cus_name' =>'text',

                       'cus_email' => 'text',

                       'cus_address' => 'text',

                       'cus_pic'=>'file',

                       'cus_phone'=>'text',

                       'cus_userid' => array('select',$user_list),

                       'cus_dept' => array ('list',array (1=>'HR',2=>'Information',3=>'BI'))

             );

 

 

//use livevalidation1.3 javascript libary

//how to use? See http://livevalidation.com/

$expamle->formFieldsValidates=array("cus_name"=>"Presence",

                                "cus_userid"=>"Presence",

                                "cus_email"=>"Email",

                                "cus_phone"=>"Numericality, {minimum: 11}");

 

$html=$expamle->display();

echo $html;

?>

 

      

结果如下:

      

      

7.2指定表单显示几列

      

<?php 

include_once('../Datagrid.php');

$expamle=new Datagrid();

$expamle->tables="`customer`";

//aim to-> SELECT `cus_id` ,`cus_name`,`cus_address`,`cus_phone`  FROM `customer` 

$expamle->fields=array("cus_id","cus_name","cus_address","cus_phone");

$expamle->fieldDisplays=array("cus_name"=>"Customer's name","cus_address"=>"Address","cus_phone"=>"Phone");

$expamle->title="List of customer";

 

$expamle->formTable="`customer`";

$expamle->formFields=$expamle->fieldDisplays;

$expamle->formColsNum=3;//指定表单显示3

$html=$expamle->display();

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

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

<link type="text/css" rel="stylesheet" href="../css/bluedream.css" />

<title>Customers</title>

 <body  >

 <table >

  <tr>

    <td><a href="?" >back</a>,&nbsp;&nbsp;Add form:  <a href="?datagrid_action=new" >here</a></td>

  </tr>

</table>

<?php

echo $html;

?>

 

</body>

</html>

 

       结果:

      

      

 

7.3给表单字段附加属性

      

<?php 

 

include_once('../Datagrid.php');

$expamle=new Datagrid();

$expamle->tables="`customer`";

//aim to-> SELECT `cus_id` ,`cus_name`,`cus_address`,`cus_phone`  FROM `customer` 

$expamle->fields=array("cus_id","cus_name","cus_email","cus_address","cus_phone");

$expamle->fieldDisplays=array("cus_name"=>"Customer's name","cus_email"=>"Email","cus_address"=>"Address","cus_phone"=>"Phone");

$expamle->title="List of customer";

 

$expamle->formTable="`customer`";

$expamle->formFields=array("cus_name"=>"Customer's name",

                        "cus_email"=>"Email",

                        "cus_address"=>"Address",

                        "cus_phone"=>"Phone");

 

//此处设置了表单中一些字段的属性

$expamle->formAttributes=array("cus_name"=>'style="background:#FFFF00" ',

                                "cus_email"=>'readonly="true" style="background:#CCCCCC"');

 

$html=$expamle->display();

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

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

<link type="text/css" rel="stylesheet" href="../css/bluedream.css" />

<title>Customers</title>

 <body  >

 <table >

  <tr>

    <td><a href="?" >back</a>,&nbsp;&nbsp;Add form:  <a href="?datagrid_action=new" >here</a></td>

  </tr>

</table>

<?php

echo $html;

?>

 

</body>

</html>

 

       结果,注意cus_namecus_email表单字段的背景颜色改变了:

      

      

7.4设置表单验证

Datagrid设置表单验证非常的简单

      

<?php 

 

include_once('../Datagrid.php');

$expamle=new Datagrid();

$expamle->tables="`customer`";

//aim to-> SELECT `cus_id` ,`cus_name`,`cus_address`,`cus_phone`  FROM `customer` 

$expamle->fields=array("cus_id","cus_name","cus_email","cus_address","cus_phone");

$expamle->fieldDisplays=array("cus_name"=>"Customer's name","cus_email"=>"Email","cus_address"=>"Address","cus_phone"=>"Phone");

$expamle->title="List of customer";

 

$expamle->formTable="`customer`";

$expamle->formFields=array("cus_name"=>"Customer's name",

                        "cus_email"=>"Email",

                        "cus_address"=>"Address",

                        "cus_phone"=>"Phone");

//使用了 livevalidation1.3 javascript 验证类库

//详细使用请参考 http://livevalidation.com/

$expamle->formFieldsValidates=array("cus_name"=>"Presence",

                                "cus_email"=>"Email",

                                "cus_phone"=>"Numericality, {minimum: 11}");

 

$html=$expamle->display();

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

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

<link type="text/css" rel="stylesheet" href="../css/bluedream.css" />

<title>Customers</title>

 <body  >

 <table >

  <tr>

    <td><a href="?" >back</a>,&nbsp;&nbsp;Add form:  <a href="?datagrid_action=new" >here</a></td>

  </tr>

</table>

<?php

echo $html;

?>

 

</body>

</html>

       结果

      

 

7.5使用在线编辑器

      

<?php 

 

include_once('../Datagrid.php');

$expamle=new Datagrid();

$expamle->tables="`customer`";

//aim to-> SELECT `cus_id` ,`cus_name`,`cus_address`,`cus_phone`  FROM `customer` 

$expamle->fields=array("cus_id","cus_userid","cus_name","cus_email","cus_dept","cus_address","cus_phone","cus_pic","cus_memo");

$expamle->fieldDisplays=array("cus_name"=>"Customer's name",

                           "cus_userid"=>"User",

                           "cus_email"=>"Email",

                           "cus_dept"=>"Department",

                           "cus_address"=>"Address",

                           "cus_phone"=>"Phone",

                           "cus_pic"=>"Pictrue",

                           "cus_memo"=>"Memo");

$expamle->title="List of customer";

 

$expamle->formTable="`customer`";

$expamle->formFields=$expamle->fieldDisplays;

$expamle->formColsNum=1; //Set the form for a few columns

 

$user_list=array(1=>"Mark",2=>"Jockson",3=>"Tom",4=>"Sun");

$expamle->formTypes=array('cus_name' =>'text',

                       'cus_email' => 'text',

                       'cus_address' => 'simple_editor',

                       'cus_memo' => 'fckeditor',

                       'cus_pic'=>'file',

                       'cus_phone'=>'text',

                       'cus_userid' => array('select',$user_list),

                       'cus_dept' => array ('list',array (1=>'HR',2=>'Information',3=>'BI'))

             );

 

 

//use livevalidation1.3 javascript libary

//how to use? See http://livevalidation.com/

$expamle->formFieldsValidates=array("cus_name"=>"Presence",

                                "cus_userid"=>"Presence",

                                "cus_email"=>"Email",

                                "cus_phone"=>"Numericality, {minimum: 11}");

 

$html=$expamle->display();

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

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

<link type="text/css" rel="stylesheet" href="../css/bluedream.css" />

<title>Customers</title>

 <body  >

 <table >

  <tr>

    <td><a href="?" >back</a>,&nbsp;&nbsp;Add form:  <a href="?datagrid_action=new" >here</a></td>

  </tr>

</table>

<?php

echo $html;

?>

 

</body>

</html>

 

       结果:

 

7.6显示多个grid

      

<?php 

//first

include_once('../Datagrid.php');

$gridObj=new Datagrid();

$gridObj->tables="`customer`";

//aim to-> SELECT `cus_id` ,`cus_name`,`cus_address`,`cus_phone`  FROM `customer` 

$gridObj->fields=array("cus_id","cus_name","cus_address","cus_phone");

$gridObj->fieldDisplays=array("cus_name"=>"Customer's name","cus_address"=>"Address","cus_phone"=>"Phone");

$gridObj->title="List of customer";

$gridHtml=$gridObj->display();

 

//second

$viewObj=new Dataview();

$viewObj->sql="SELECT * FROM `customer` left join `user` on user_id=cus_userid";

$viewObj->fieldDisplays=array("cus_name"=>"Customer","user_name"=>"User","cus_pic"=>"Pictrue","cus_address"=>"Address","cus_phone"=>"Phone");

$viewHtml=$viewObj->display();

 

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

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

<link type="text/css" rel="stylesheet" href="../css/bluedream.css" />

<title>Customers</title>

 <body  >

<?php

echo $gridHtml;

?>

<p><p>

 

<?php

echo $viewHtml;

?>

</body>

</html>

       结果:

  

 

7.7更换皮肤

Datagrid内置了许多表格CSS样式通过更换这些样式可以更换你所需要的皮肤

 

<?php 

//first

include_once('../Datagrid.php');

$gridObj=new Datagrid();

$gridObj->tables="`customer`";

//aim to-> SELECT `cus_id` ,`cus_name`,`cus_address`,`cus_phone`  FROM `customer` 

$gridObj->fields=array("cus_id","cus_name","cus_address","cus_phone");

$gridObj->fieldDisplays=array("cus_name"=>"Customer's name","cus_address"=>"Address","cus_phone"=>"Phone");

$gridObj->title="List of customer";

$gridHtml=$gridObj->display();

 

//second

$viewObj=new Dataview();

$viewObj->sql="SELECT * FROM `customer` left join `user` on user_id=cus_userid";

$viewObj->fieldDisplays=array("cus_name"=>"Customer","user_name"=>"User","cus_pic"=>"Pictrue","cus_address"=>"Address","cus_phone"=>"Phone");

$viewHtml=$viewObj->display();

 

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

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

<title>Customers</title>

<script type="text/javascript">

    if(getCookie('style_css')!=null){

       document.write('<link type="text/css" rel="stylesheet" href="../css/'+getCookie('style_css')+'.css" />');

    }else{

       document.write('<link type="text/css" rel="stylesheet" href="../css/oranges-in-the-sky.css" />');

    }

    //cookies函数

    function SetCookie(name,value)

    {

        var Days = 30; 

        var exp  = new Date();    //new Date("December 31, 9998");

        exp.setTime(exp.getTime() + Days*24*60*60*1000);

        document.cookie = name + "="+ escape (value) + ";expires=" + exp.toGMTString();

    }

    function getCookie(name)

    {

        var arr = document.cookie.match(new RegExp("(^| )"+name+"=([^;]*)(;|$)"));

         if(arr != null) return unescape(arr[2]); return null;

 

    }

</script>

 

 <body  >

 

<table      >

  <tr>

    <td>当前位置:<a href="?" >数据列表</a>,&nbsp;&nbsp;添加请点  <a href="?datagrid_action=new" >这里</a>&nbsp;&nbsp;&nbsp;更换皮肤

          <select name="style_cass" id="style_css" onChange="SetCookie('style_css',this.value);window.location.reload();" >

             <option  >请选择</option>

              <option value="oranges-in-the-sky">oranges-in-the-sky.css</option>

              <option value="bluedream">bluedream.css</option>

              <option value="classic_somehow">classic_somehow.css</option>

              <option value="itunes">itunes.css</option>

              <option value="mintgreen">mintgreen.css</option>

              <option value="seaglass">seaglass.css</option>

              <option value="pure_blue">pure_blue.css</option>

              <option value="dartblue">dartblue.css</option> 

        </select>

 </td>

  </tr>

</table>

<?php

echo $gridHtml;

?>

<p><p>

 

<?php

echo $viewHtml;

?>

</body>

</html>

结果:

 

8.高级应用

在遇到比较复杂的业务需求时,基本设置里已经不能满足运行的需要了 ,由于Phpgrid  采用面向对象技术,因此可以对DatagridDataview类进行继承和扩展重写.

使用这种方法,可以满足任何特殊的需求,而且这样无需要修改基类的代码。

当你继承于DatagridDataview类后你可以重写其中任意public方法。

下面是一些扩展的示例。

 

8.1实现字段值的替换

通过重写Datagrid类的repalceValue函数可以对每一个字段值进行替换,例如加上 颜色或者超链接,代码:

  

<?php 

 

include_once('../Datagrid.php');

 

$GLOBALS['user_list']=array(1=>"Mark",2=>"Jockson",3=>"Tom",4=>"Sun");

 

 

/**

 * 继承了 Datagrid 并重写了repalceValue函数

 *

 * @author cdwei

 *

 */

class myclass extends Datagrid{

   

    //rewrite this function to custom   table body

    function repalceValue($row,$field,$value) {

       if($field=="cus_userid"){

           return $GLOBALS['user_list'][$value];

       }

       if($field=="cus_pic"){

           return '<img src="../'.$value.'"  width="30%" height="30%" />';

       }

       return $value;

    }

}

//实例化继承后的myclass

$expamle=new myclass();

$expamle->tables="`customer`";

//aim to-> SELECT `cus_id` ,`cus_name`,`cus_address`,`cus_phone`  FROM `customer` 

$expamle->fields=array("cus_id","cus_name","cus_userid","cus_pic","cus_address","cus_phone");

$expamle->fieldDisplays=array("cus_name"=>"Customer's name","cus_userid"=>"User","cus_pic"=>"Pictrue","cus_address"=>"Address","cus_phone"=>"Phone");

$expamle->title="List of customer";

 

$expamle->formTable="`customer`";

$expamle->formFields=$expamle->fieldDisplays;

 

 

$expamle->formTypes=array('cus_name' =>'text',

                       'cus_email' => 'text',

                       'cus_address' => 'text',

                       'cus_pic'=>'file',

                       'cus_phone'=>'text',

                       'cus_userid' => array('select',$GLOBALS['user_list'])

             );

 

$html=$expamle->display();

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

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

<link type="text/css" rel="stylesheet" href="../css/bluedream.css" />

<title>Customers</title>

 <body  >

 <table >

  <tr>

    <td><a href="?" >back</a>,&nbsp;&nbsp;Add form:  <a href="?datagrid_action=new" >here</a></td>

  </tr>

</table>

<?php

echo $html;

?>

 

</body>

</html>

 

replaceValue函数的$row为数据中每一行的值,$field为当前的字段名,$value为当前值

结果:

 

8.2重写表单

当您需要更复杂的表单时, 需要扩展Datagrid类的formAdd formEdit函数,例如:

 

 

<?php 

 

include_once('../Datagrid.php');

 

$GLOBALS['user_list']=array(1=>"Mark",2=>"Jockson",3=>"Tom",4=>"Sun");

 

 

/**

 * extend Datagrid ,rewrite the public of function

 *

 * @author cdwei

 *

 */

class myclass extends Datagrid{

   

    /**

     *重写生成新增表单

     */

    public function setNewFormHtml() {

       $html='

           <table   id="formHtml" >

                  <tr>

                   <td  colspan="4"  ><span >自己定义添加表单</span></td>

                 </tr>

                

                      <td  >Customer  name</td>

                       <td  ><input type="text" id="cus_name" name="cus_name"   value=""    > </td>

                      

                      <td  >User</td>

                       <td  ><select name="cus_userid"   id="cus_userid">

                         <option value=1>Mark</option>

                         <option value=2>Jockson</option>

                         <option value=3>Tom</option>

                         <option value=4>Sun</option>

                         </select> </td>

                       </tr>

                      <td  >Pictrue</td>

                       <td  >文件路径:<a href="../"  target="_blank"></a><br>

                      <input name="cus_pic" id="cus_pic" type="file"   />  </td>

                      

                      <td  >Address</td>

                       <td  ><input type="text" id="cus_address" name="cus_address"   value=""    > </td>

                       </tr>

                      <td  >Phone</td>

                       <td  ><input type="text" id="cus_phone" name="cus_phone"   value=""    > </td>

                      

                      <td  ></td>

                       <td  > </td>

                       </tr><tr >

                   <td colspan="4">

                     <div align="center">

                  <input type="submit" name="Submit" value="提交">

                   &nbsp;&nbsp;<input type="reset" name="Submit2" value="重置">

                     </div></td>

                </tr>     </table>

       ';

      

       return $html;

    }  

 

    /**

     *重写生成编辑表单

     */

    public function setEditFormHtml() {

       return $this->makeFormHtml('edit');

    }

   

}

//实例化继承后的myclass

$expamle=new myclass();

$expamle->tables="`customer`";

//aim to-> SELECT `cus_id` ,`cus_name`,`cus_address`,`cus_phone`  FROM `customer` 

$expamle->fields=array("cus_id","cus_name","cus_userid","cus_pic","cus_address","cus_phone");

$expamle->fieldDisplays=array("cus_name"=>"Customer's name","cus_userid"=>"User","cus_pic"=>"Pictrue","cus_address"=>"Address","cus_phone"=>"Phone");

$expamle->title="List of customer";

 

$expamle->formTable="`customer`";

$expamle->formFields=$expamle->fieldDisplays;

 

 

$html=$expamle->display();

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

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

<link type="text/css" rel="stylesheet" href="../css/bluedream.css" />

<title>Customers</title>

 <body  >

 <table >

  <tr>

    <td><a href="?" >back</a>,&nbsp;&nbsp;Add form:  <a href="?datagrid_action=new" >here</a></td>

  </tr>

</table>

<?php

echo $html;

?>

 

</body>

</html>

 

 

运行结果: