2013-02-26 108 views
0

我想動態地初始化我的控制器方法並收集我的嵌套視圖的輸出數據。我想避免每次查看$ data ['something']的實例在這個視圖和函數中調用適當的視圖php文件。codignieter嵌套視圖

class Admin extends CI_Controller { 

    function __construct() 
    { 
     parent::__construct(); 
    } 

    public function _remap($method, $params = array()) 
    { 
     $this->view($method, $params); 
    } 
    public function view($method, $params) 
    { 
     $param = (implode(",", $params)); 
     $data = array(); 
     if (method_exists($this,$method)){ 
      $param = !empty($param) ? $param : ''; 
      $data = call_user_func_array(array($this, $method), array($param)); 
      // and load the data array in the main view 
      $this->load->view('admin/dashboard', $data); 
     } else { 
      $this->load->view('admin/404', $data); 
     } 
    } 

    public function _one_of_mymethod(){ 
     $data['nested_view'] = 'view1'; 
     //calling models gather $data 
     return $data; 
    } 

} 

比數據數組的功能輸出正確的嵌套視圖。

主視圖

會像

<div class="sidebar"><?php $this->view('admin/sidebar'); ?></div> 
<div id="content"><?php $this->view('/admin/'. $nested_view) ?></div> 

,但這似乎不是在情況下,如果我使用Ajax調用正確的方法。什麼是添加嵌套視圖的最佳方式動態

回答

0

如果您正在使用JQuery,你可以隨時使用JQuery $ .load方法這樣

$('.sidebar').load('<?php site_url('controllername/methodname')?>'); 

這裏是方法名

function methodname(){ 
    $this->view(); 
    // do any stuff you want. 
} 
+0

感謝對於你的反饋,主要是我認爲我的邏輯是我建立的不是最好的 – fefe 2013-02-26 13:09:03

+0

@fefe我建議你閱讀Jamie Rumbelow的My_Controller。它確實是你想要的。他的第一本書是可用的,你可以閱讀和學習許多偉大的技術。 – 2013-02-26 13:11:40