日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢(xún)
選擇下列產(chǎn)品馬上在線(xiàn)溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問(wèn)題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷(xiāo)解決方案
使用jQuery設(shè)計(jì)數(shù)據(jù)表格之設(shè)計(jì)表格基類(lèi)

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

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

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

 
 
 
  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. ?>

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

接下來(lái),編寫(xiě)其構(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ù)庫(kù)表的名稱(chēng)和主鍵(默認(rèn)這里為$id)。然后初始化實(shí)例化了CI對(duì)象,然后調(diào)用其加載數(shù)據(jù)庫(kù)及加載表的幫助方法,得出的$this->tbl_fields則是其數(shù)據(jù)庫(kù)的字段了。然后判斷主鍵$pk_col是否在數(shù)據(jù)表中,如果不存在的話(huà)拋出異常,如果存在的話(huà),使用成員變量tbl_name和pk_col分別保存數(shù)據(jù)的表名和主鍵,這樣下次就不用再訪(fǎng)問(wèn)數(shù)據(jù)庫(kù)了。***,使用$this->CI->load->library('table')的幫助表格類(lèi),將數(shù)據(jù)庫(kù)的字段生成一個(gè)HTML的簡(jiǎn)單表格。

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

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

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

在數(shù)據(jù)的呈現(xiàn)過(guò)程中,有的時(shí)候不是所有的列都需要顯示,要根據(jù)實(shí)際情況進(jìn)行隱藏或顯示,這個(gè)時(shí)候可以編寫(xiě)相關(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是需要被隱藏的列的名稱(chēng)數(shù)組。代碼中還對(duì)主鍵進(jìn)行了判斷,因?yàn)橹麈I是必須從數(shù)據(jù)庫(kù)中獲取的。而假如不希望主鍵顯示在用戶(hù)界面上的話(huà),可以通過(guò)以下方法設(shè)置:

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

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

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

 
 
 
  1.  array_unshift($headings,"");

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

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

接下來(lái),為數(shù)據(jù)表格增加表單提交按鈕。為了通用起見(jiàn),我們期望可以根據(jù)用戶(hù)的要求,指定生成什么類(lèi)型的按鈕。比如,在這個(gè)例子中,期望生成一個(gè)刪除的按鈕,所以我們編寫(xiě)如下的一個(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è)按鈕被用戶(hù)點(diǎn)擊并提交呢?則可以用如下方法判斷

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

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

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

返回的是一個(gè)表示了用戶(hù)選擇多少個(gè)記錄的數(shù)組。本例子中涉及的是刪除按鈕的功能,所以編寫(xiě)一個(gè)方法,用于將用戶(hù)選擇的數(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.   }

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

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

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

 
 
 
  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è)類(lèi)保存為datagrid_helper.php,保存在application/helper目錄下。

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


當(dāng)前標(biāo)題:使用jQuery設(shè)計(jì)數(shù)據(jù)表格之設(shè)計(jì)表格基類(lèi)
標(biāo)題路徑:http://www.dlmjj.cn/article/cceched.html