自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

使用jQuery設(shè)計(jì)數(shù)據(jù)表格之設(shè)計(jì)表格基類

開發(fā) 前端
在本文中,將選取PHP中的著名的開發(fā)框架Codeigniter(下簡稱CI)配合jQuery去設(shè)計(jì)一個(gè)日常常見的datagrid數(shù)據(jù)表格。其中充分利用了jQuery及CI框架的特性,打造一個(gè)無刷新的數(shù)據(jù)表格。本文的閱讀對象為已經(jīng)具備一定jQuery基礎(chǔ)知識及CI框架基礎(chǔ)知識的用戶。

當(dāng)前在Web開發(fā)中,jQuery和PHP無疑是***的配合。其中PHP由于其簡單易用,深得開發(fā)者的喜愛,而jQuery則由于在前端開發(fā)中的靈活和簡單,功能強(qiáng)大,可以做出很多很眩目的效果。在本文中,將選取PHP中的著名的開發(fā)框架Codeigniter(下簡稱CI)配合jQuery去設(shè)計(jì)一個(gè)日常常見的datagrid數(shù)據(jù)表格。其中充分利用了jQuery及CI框架的特性,打造一個(gè)無刷新的數(shù)據(jù)表格。本文的閱讀對象為已經(jīng)具備一定jQuery基礎(chǔ)知識及CI框架基礎(chǔ)知識的用戶。

步驟1 設(shè)計(jì)生成表格的基類

我們希望設(shè)計(jì)一個(gè)生成表格的基類,它可以針對任意數(shù)據(jù)庫中的任意表,都能自動生成對應(yīng)的數(shù)據(jù)表格,比如我們只需要傳入數(shù)據(jù)庫中的表名或者表的索引字段即可生成表格。本文中的大部分時(shí)間都會圍繞這個(gè)基類展開代碼編寫,下面是代碼的片斷定義:

  1. class Datagrid{  
  2.   private $hide_pk_col = true;  
  3.   private $hide_cols = array();  
  4.   private $tbl_name = '';  
  5.   private $pk_col = '';  
  6.   private $headings = array();  
  7.   private $tbl_fields = array();  
  8. }  
  9. ?> 

這里先行定義了一些屬性變量,比如是否隱藏主鍵的列,表的名稱$tbl_name,表頭列$headings,表的字段數(shù)組$tbl_fields。這里,我們把這個(gè)基類定義為CI中的helper幫助類,因?yàn)槎x為CI中的library的話,則不容易向其構(gòu)造函數(shù)傳遞參數(shù)。

接下來,編寫其構(gòu)造函數(shù)為代碼如:

  1. public function __construct($tbl_name, $pk_col = 'id'){  
  2.   $this->CI =& get_instance();  
  3.   $this->CI->load->database();  
  4.   $this->tbl_fields = $this->CI->db->list_fields($tbl_name);  
  5.   if(!in_array($pk_col,$this->tbl_fields)){  
  6.   throw new Exception("Primary key column '$pk_col' not found in table '$tbl_name'");  
  7.   }  
  8.   $this->tbl_name = $tbl_name;  
  9.   $this->pk_col = $pk_col;  
  10.   $this->CI->load->library('table');  
  11.   }  

在上面的代碼的構(gòu)造函數(shù)中,接收了兩個(gè)參數(shù),分別是數(shù)據(jù)庫表的名稱和主鍵(默認(rèn)這里為$id)。然后初始化實(shí)例化了CI對象,然后調(diào)用其加載數(shù)據(jù)庫及加載表的幫助方法,得出的$this->tbl_fields則是其數(shù)據(jù)庫的字段了。然后判斷主鍵$pk_col是否在數(shù)據(jù)表中,如果不存在的話拋出異常,如果存在的話,使用成員變量tbl_name和pk_col分別保存數(shù)據(jù)的表名和主鍵,這樣下次就不用再訪問數(shù)據(jù)庫了。***,使用$this->CI->load->library('table')的幫助表格類,將數(shù)據(jù)庫的字段生成一個(gè)HTML的簡單表格。

而為了自定義列的標(biāo)題,也有一個(gè)方法如下:

  1. public function setHeadings(array $headings){  
  2.   $this->headings = array_merge($this->headings, $headings);  
  3.   } 

比如,我們可以將原來表格的列重新自定義要顯示的名稱,比如把regdate字段改為“Registration Date”。而具體的代碼在下文中還會講解。

在數(shù)據(jù)的呈現(xiàn)過程中,有的時(shí)候不是所有的列都需要顯示,要根據(jù)實(shí)際情況進(jìn)行隱藏或顯示,這個(gè)時(shí)候可以編寫相關(guān)的方法實(shí)現(xiàn),代碼如下:

  1. public function ignoreFields(array $fields){  
  2.   foreach($fields as $f){  
  3.   if($f!=$this->pk_col)  
  4.   $this->hide_cols[] = $f;  
  5.   }  
  6.   } 

其中,$fields是需要被隱藏的列的名稱數(shù)組。代碼中還對主鍵進(jìn)行了判斷,因?yàn)橹麈I是必須從數(shù)據(jù)庫中獲取的。而假如不希望主鍵顯示在用戶界面上的話,可以通過以下方法設(shè)置:

  1. public function hidePkCol($bool){  
  2.   $this->hide_pk_col = (bool)$bool;  
  3.   } 

這里傳入的$bool是一個(gè)布爾值,代表是否需要在界面中顯示主鍵。

接下來,再看一個(gè)方法,代碼如下:

  1. private function _selectFields(){  
  2.   foreach($this->tbl_fields as $field){  
  3.   if(!in_array($field,$this->hide_cols)){  
  4.   $this->CI->db->select($field);  
  5.   //判斷是否隱藏了主鍵 if($field==$this->pk_col && $this->hide_pk_col) continue;  
  6.   $headings[]= isset($this->headings[$field]) ? $this->headings[$field] : ucfirst($field);  
  7.   }  
  8.   }  
  9.   if(!empty($headings)){  
  10.   array_unshift($headings,"");  
  11.   $this->CI->table->set_heading($headings);  
  12.   }  
  13.   } 

這里是一個(gè)helper的幫助類方法,注意CI中的helper類,命名是private的,以下劃線+方法名的方法命名。這個(gè)方法是在下文中的generate()中用到的,其中主要的功能是,循環(huán)查找$this->tbl_fields中的每個(gè)字段,是否屬于隱藏顯示的字段,如果不屬于隱藏字段,則通過$this->CI->db->select($field);將相關(guān)字段取出。另外要注意的是

  1.  array_unshift($headings,""); 

這句代碼中,實(shí)際的作用是,在數(shù)據(jù)表格的***列中,加一項(xiàng)代表“全選/反選”功能的checkbox,這個(gè)功能可以用在對數(shù)據(jù)進(jìn)行選擇或刪除的時(shí)候。

接下來,是生成數(shù)據(jù)表格的generate()方法,代碼如下:

  1. public function generate(){  
  2.   $this->_selectFields();  
  3.   $rows = $this->CI->db  
  4.   ->from($this->tbl_name)  
  5.   ->get()  
  6.   ->result_array();  
  7.   foreach($rows as &$row){  
  8.   $id = $row[$this->pk_col];  
  9.   array_unshift($row, "");  
  10.   if($this->hide_pk_col){  
  11.   unset($row[$this->pk_col]);  
  12.   }  
  13.   }  
  14.   return $this->CI->table->generate($rows);  
  15.   }  

在這個(gè)方法中,首先是調(diào)用了上文中的 $this->_selectFields();

方法,以決定顯示數(shù)據(jù)庫指定表中的哪些字段。然后使用CI中的獲得數(shù)據(jù)表記錄的方法獲得數(shù)據(jù)集($rows)。然后在循環(huán)中,為每一條記錄前都生成一個(gè)checkbox(array_unshift一句)。***,判斷是否需要屏蔽顯示主鍵,如果是的話,則屏蔽顯示(unset一句)。

接下來,為數(shù)據(jù)表格增加表單提交按鈕。為了通用起見,我們期望可以根據(jù)用戶的要求,指定生成什么類型的按鈕。比如,在這個(gè)例子中,期望生成一個(gè)刪除的按鈕,所以我們編寫如下的一個(gè)生成按鈕的方法:

  1. public static function createButton($action_name, $label){  
  2.   return "";  
  3.   } 

在這個(gè)靜態(tài)方法中,$action_name為要生成的方法名,比如我們要生成的是Delete方法,則傳入的$action_name參數(shù)為delete,而label則為按鈕的標(biāo)簽名。

而如果得知這個(gè)按鈕被用戶點(diǎn)擊并提交呢?則可以用如下方法判斷

  1. public static function getPostAction(){  
  2.   if(isset($_POST['dg_action'])){  
  3.   return key($_POST['dg_action']);  
  4.   }  
  5.   }  

如果用戶選擇了數(shù)據(jù)表格中的多行并提交的話,可以使用如下方法去獲得

  1. public static function getPostItems(){  
  2.   if(!empty($_POST['dg_item'])){  
  3.   return $_POST['dg_item'];  
  4.   }  
  5.   return array();  
  6.   } 

返回的是一個(gè)表示了用戶選擇多少個(gè)記錄的數(shù)組。本例子中涉及的是刪除按鈕的功能,所以編寫一個(gè)方法,用于將用戶選擇的數(shù)據(jù)刪除,代碼如下:

  1. public function deletePostSelection(){  
  2.   if(!empty($_POST['dg_item']))  
  3.   return $this->CI->db  
  4.   ->from($this->tbl_name)  
  5.   ->where_in($this->pk_col,$_POST['dg_item'])  
  6.   ->delete();  
  7.   } 

比如用戶在表格中選擇了若干條記錄,點(diǎn)delete按鈕提交,則deletePostSelection方法會等價(jià)于執(zhí)行如下的SQL語句:

  1. DELETE FROM my_table WHERE id IN (1,5,7,3,etc...)。 

***,我們綜合整理一下完整的數(shù)據(jù)表格生成類,如下代碼:

  1. class Datagrid{  
  2.   private $hide_pk_col = true;  
  3.   private $hide_cols = array();  
  4.   private $tbl_name = '';  
  5.   private $pk_col = '';  
  6.   private $headings = array();  
  7.   private $tbl_fields = array();  
  8.   function __construct($tbl_name, $pk_col = 'id'){  
  9.   $this->CI =& get_instance();  
  10.   $this->CI->load->database();  
  11.   $this->tbl_fields = $this->CI->db->list_fields($tbl_name);  
  12.   if(!in_array($pk_col,$this->tbl_fields)){  
  13.   throw new Exception("Primary key column '$pk_col' not found in table '$tbl_name'");  
  14.   }  
  15.   $this->tbl_name = $tbl_name;  
  16.   $this->pk_col = $pk_col;  
  17.   $this->CI->load->library('table');  
  18.   }  
  19.   public function setHeadings(array $headings){  
  20.   $this->headings = array_merge($this->headings, $headings);  
  21.   }  
  22.   public function hidePkCol($bool){  
  23.   $this->hide_pk_col = (bool)$bool;  
  24.   }  
  25.   public function ignoreFields(array $fields){  
  26.   foreach($fields as $f){  
  27.   if($f!=$this->pk_col)  
  28.   $this->hide_cols[] = $f;  
  29.   }  
  30.   }  
  31.   private function _selectFields(){  
  32.   foreach($this->tbl_fields as $field){  
  33.   if(!in_array($field,$this->hide_cols)){  
  34.   $this->CI->db->select($field);  
  35.   if($field==$this->pk_col && $this->hide_pk_col) continue;  
  36.   $headings[]= isset($this->headings[$field]) ? $this->headings[$field] : ucfirst($field);  
  37.   }  
  38.   }  
  39.   if(!empty($headings)){  
  40.   array_unshift($headings,"");  
  41.   $this->CI->table->set_heading($headings);  
  42.   }  
  43.   }  
  44.   public function generate(){  
  45.   $this->_selectFields();  
  46.   $rows = $this->CI->db  
  47.   ->from($this->tbl_name)  
  48.   ->get()  
  49.   ->result_array();  
  50.   foreach($rows as &$row){  
  51.   $id = $row[$this->pk_col];  
  52.   array_unshift($row, "");  
  53.   if($this->hide_pk_col){  
  54.   unset($row[$this->pk_col]);  
  55.   }  
  56.   }  
  57.   return $this->CI->table->generate($rows);  
  58.   }  
  59.   public static function createButton($action_name, $label){  
  60.   return "";  
  61.   }  
  62.   public static function getPostAction(){  
  63.   if(isset($_POST['dg_action'])){  
  64.   return key($_POST['dg_action']);  
  65.   }  
  66.   }  
  67.   public static function getPostItems(){  
  68.   if(!empty($_POST['dg_item'])){  
  69.   return $_POST['dg_item'];  
  70.   }  
  71.   return array();  
  72.   }  
  73.   public function deletePostSelection(){  
  74.   if(!empty($_POST['dg_item']))  
  75.   return $this->CI->db  
  76.   ->from($this->tbl_name)  
  77.   ->where_in($this->pk_col,$_POST['dg_item'])  
  78.   ->delete();  
  79.   }  
  80.   } 

我們把這個(gè)類保存為datagrid_helper.php,保存在application/helper目錄下。

原文:http://tech.it168.com/a2011/1024/1262/000001262979_all.shtml

【編輯推薦】

  1. 10月10款有趣強(qiáng)大的jQuery插件推薦
  2. 當(dāng)jQuery遭遇CoffeeScript——妙不可言
  3. 10個(gè)新鮮的Ajax相關(guān)的jQuery插件
  4. 20個(gè)教你得到酷炫效果的jQuery教程
  5. jQuery插件開發(fā)實(shí)戰(zhàn)場
責(zé)任編輯:陳貽新 來源: it168
相關(guān)推薦

2011-10-28 14:01:30

jQuery

2017-08-10 13:43:00

大數(shù)據(jù)數(shù)據(jù)表格優(yōu)化設(shè)計(jì)

2011-05-19 11:01:14

ERWin數(shù)據(jù)庫設(shè)計(jì)

2010-12-13 10:36:45

CSS表格

2022-01-14 10:34:59

B端表格設(shè)計(jì)APP

2011-01-26 08:59:11

jQueryjavascriptweb

2013-09-03 09:55:42

iOS無線客戶端框架設(shè)計(jì)

2015-09-08 11:06:46

設(shè)計(jì)編輯窗體

2010-07-05 16:23:39

UML類圖

2022-06-29 08:28:58

數(shù)據(jù)可視化數(shù)據(jù)可視化平臺

2011-06-30 16:38:07

Qt QTableWidg

2023-07-26 12:38:42

PyGWalker數(shù)據(jù)類型

2013-03-20 11:25:47

數(shù)據(jù)庫數(shù)據(jù)庫設(shè)計(jì)

2013-03-20 11:33:31

2013-03-20 13:25:53

數(shù)據(jù)庫數(shù)據(jù)庫設(shè)計(jì)

2013-03-20 13:35:12

數(shù)據(jù)庫數(shù)據(jù)庫設(shè)計(jì)

2011-07-26 15:30:32

jQuery

2012-04-28 10:07:43

數(shù)據(jù)庫數(shù)據(jù)庫設(shè)計(jì)

2021-11-09 08:15:18

Grafana 數(shù)據(jù)可視化運(yùn)維

2019-09-29 09:08:41

Python數(shù)據(jù)庫Google
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號