当前在Web开发中,jQuery和PHP无疑是绝佳的配合。其中PHP由于其简单易用,深得开发者的喜爱,而jQuery则由于在前端开发中的灵活和简单,功能强大,可以做出很多很眩目的效果。在上篇文章中,主要讲述了设计表格基类,本文将主要介绍测试和运行部分,以及加入AJAX功能,整合jQuery。

  测试运行

  现在,我们可以在CI中测试运行下我们所写的数据表格助手类是否有效果,在测试前,先在MYSQL中建立数据表如下:

CREATE DATABASE `dg_test`;

  CREATE TABLE `users` (

  `id`
int ( 11 ) NOT NULL AUTO_INCREMENT,

  `username` varchar(
80 ) NOT NULL ,

  `password` varchar(
32 ) NOT NULL ,

  `email` varchar(
255 ) NOT NULL ,

  UNIQUE KEY `id` (`id`)

  ) ENGINE
= MyISAM DEFAULT CHARSET = latin1 AUTO_INCREMENT = 5 ;

  并插入一些初始数据

INSERT INTO `users` (`id`, `username`, `password`, `email`) VALUES

  (
1 , ' david', '12345', 'david@domain.com'),

  (
2 , ' maria', '464y3y', 'maria@domain.com'),

  (
3 , ' alejandro', 'a42352fawet', 'alejandro@domain.com'),

  (
4 , ' emma', 'f22a3455b2', 'emma@domain.com'

  接下来,编写控制层的测试文件,命名为test.php,保存在application/controller目录下,代码如下:

class Test extends CI_Controller{

  
function __construct(){

  parent::__construct();

  $this
-> load -> helper( array ( ' datagrid','url'));

  $this
-> Datagrid = new Datagrid( ' users','id');

  }

  
function index(){

  $this
-> load -> helper( ' form');

  $this
-> load -> library( ' session');

  $this
-> Datagrid -> hidePkCol( true );

  $this
-> Datagrid -> setHeadings( array ( ' email'=>'E-mail'));

  $this
-> Datagrid -> ignoreFields( array ( ' password'));

  
if ($ error = $this -> session -> flashdata( ' form_error')){

  echo
" $error " ;

  }

  echo form_open(
' test/proc');

  echo $this
-> Datagrid -> generate();

  echo Datagrid::createButton(
' delete','Delete');

  echo form_close();

  }

  
function proc($request_type = ' '){

  $this
-> load -> helper( ' url');

  
if ($action = Datagrid::getPostAction()){

  $
error = "" ;

  switch($action){

  
case ' delete' :

  
if (!$this -> Datagrid -> deletePostSelection()){

  $
error = ' Items could not be deleted';

  }

  break;

  }

  
if ($request_type! = ' ajax'){

  $this
-> load -> library( ' session');

  $this
-> session -> set_flashdata( ' form_error',$error);

  redirect(
' test/index');

  }
else {

  echo json_encode(
array ( ' error' => $error));

  }

  }
else {

  die(
" Bad Request " );

  }

  }

  }

  ?
>