2015-07-13 33 views
0

我想實現一個網站使用datatable和zend框架從mysql數據庫中獲取數據。這裏是我的view.phtmlZend框架使用數據表

<table cellpadding="0" cellspacing="0" border="0" class="display" id="shadow-table" width="100%"> 
     <thead> 
      <tr> 
       <th>ID</th> 
       <th>Name</th> 
       <th>Email</th> 

      </tr> 
     </thead> 

     <tbody bgcolor="#E2E4FF"> 

     </tbody> 

Controller.php這樣

public function getdataAction() { 
      $this->_helper->layout()->disableLayout(); 
     $shadow = new Admin_Model_Shadow(); 
     $result = $shadow->listall(); 
     $this->view->a = $result; 
     json_encode($result); 
     } 

和我model.php

class Admin_Model_Shadow extends Zend_Db_Table_Abstract { 

    protected $_name = "users"; 
    protected $_primary = "user_id"; 

    public function listall() { 
     $db = $this->getDefaultAdapter(); 
     $query = $db->select() 
       ->from('users',array('user_id','name','email'); 
     return $db->fetchAll($query); 
    } 

} 

現在我想通過數據表顯示在view.phtml的$result。我怎樣才能做到這一點?

回答

0

對不起,我會嘗試添加一些更多的細節,如果我有時間這樣做。

這是我在幾年前用於項目的Controller Action。

你必須告訴Controller :: init()你的datatableAction將需要返回JSON而不是渲染的視圖腳本。

/** 
* Initialise Controller 
* 
* @return void 
*/ 
public function init() { 
    /* 
    * Set Request/Response Context (HTML,XML,JSON) for Ajax Request 
    * Add ?format=json to URL e.g. http://test.com/test?format=json 
    */ 
    $this->_helper ->contextSwitch() 
        //add context for datatablesAction() Response is JSON 
        ->addActionContext('datatable', 'json') 
        ->initContext(); 

    parent::init(); 
} 

這是我目前正在使用的Action,如果我得到時間我會修改它以適合您的變量名稱。

/** 
* Get evn records via AJAX call converted to jquery datatable format 
* 
* @link http://www.datatables.net/ 
* 
* @return void 
*/ 
protected function datatableAction() { 
    /* @var $request Zend_Controller_Request_Http */ 
    $request = $this->getRequest(); 
    /* @var $cache Zend_Session_Namespace */ 
    $cache  = new Zend_Session_Namespace("cache"); 

    $array  = $cache->{$module}; 
    $selected = $array["selected"]; 

    /* @var $cdrFactory Evn_Model_RecordsFactory */ 
    $cdrFactory = new Evn_Model_RecordsFactory(); 

    //set request object attributes via magic setter from caches selection array 
    $cdrFactory->account  = $selected["account"]; 
    $cdrFactory->direction = $selected["direction"]; 
    $cdrFactory->date_start = $selected["date_start"]; 
    $cdrFactory->date_end = $selected["date_end"]; 

    //get sEcho from Request Object (default 1) 
    $page = $request->getParam("sEcho", 1); 
    $page = ($page<1) ? 1 : $page; 
    //set view variable (auto json format via contextswitch) 
    $this->view->sEcho = $page; 

    //calculate limit start 
    $from = ($page>1) ? (($page-1) * $selected["entries"]) : 0; 
    //calculate limit end 
    $end = ($from + $selected["entries"])-1; 

    //set records limit start 
    $cdrFactory->limit_begin = $from; 
    //set records limit end 
    $cdrFactory->limit_end = $end; 

    //request records 
    $records = $cdrFactory->fetch(); 
    //get size of retrieved records 
    $total  = $cdrFactory->getSize(); 
    //set view variable (auto json format via contextswitch) 
    $this->view->iTotalRecords = $total; 
    $this->view->iTotalDisplayRecords = $total-(($end-$from)+1); 

    //set view variable (auto json format via contextswitch) 
    $this->view->sColumns = "call_start,a_partynumber,b_partynumber,seconds,direction,price_complete,price_minute,price_call"; 
    //create array in datatables format 
    $aaData  = array(); 
    foreach($records as $record) { 
     //get plain array 0-x 
     $aaData[] = array_values($record); 
    } 
    //set view variable (auto json format via contextswitch) 
    $this->view->aaData = $aaData; 
} 

看看https://www.datatables.net/manual/server-side更多INFORS哪些JSON參數的操作可能或需要返回。

只需將變量添加爲$ this-> view - > [varname],JSON上下文切換將自動處理輸出和標題。

玩得開心!

+0

謝謝你的回覆!但我對此很新,你能否給我一個類似於我的代碼的簡單例子。 – user2986673