2013-03-04 102 views
0

IN codeigniter我反覆使用控制器來加載我的頁面的所有模板.... 我已將頁面劃分爲標題,頂部導航,左側導航以及內容和頁腳。在codeigniter中使用ajax的頁面導航

這是我目前

public function get_started() { 
    if (test_login()) { 
     $this->load->view('includes/header'); 
     $this->load->view('includes/topnav'); 
     $this->load->view('includes/leftbar'); 
     $this->load->view('login_nav/get_started'); 
     $this->load->view('includes/footer'); 
    } else { 
     $this->load->view('errors/needlogin');  
    } 
} 

做是否有笨任何jQuery的AJAX傭工或插件,它可以讓我保持頁眉頁腳和topnavigation靜,讓我加載使用AJAX的具體意見。

在此先感謝..

+0

你正在加載頁眉頁腳左等等,然後你想做什麼?你想通過ajax加載特定的部分?像頁腳頭等?請正確澄清你的問題 – 2013-03-04 04:14:54

+0

如果你使用ajax的原因是因爲模板,你可能想看看這個線程http://stackoverflow.com/questions/3957000/what-c​​odeigniter-template-library-is-best – 2013-03-04 05:06:00

+0

謝謝對於響應...但我試圖使用簡單的jquery .ajax()方法進行頁面導航...我不知道如何在MVC中使用它。 – user1978166 2013-03-04 05:38:39

回答

0

您可以使用構造函數來設置靜態標題:

//in your controller 

public $data; 

function __construct() 
{ 
    $this->data['header'] = 'static_header'; 
    $this->data['static_footer'] = 'static_footer'; 
} 

function get_started(){ 
    if (test_login()) { 
     $this->data['subview'] = 'login_nav/get_started'; 
    } else { 
     $this->data['subview'] = 'errors/needlogin'; 
    } 
     $this->load->view('template',$this->data); 
} 

function get_page(){ 
    $view = $this->load->view('your_dynamic_view','',TRUE); 
    print json_encode(array('success' => TRUE, 'view' => $view); 
} 

// in your template.php 
<div id="header"><?php echo $this->load->view('header');?></div> 
<div id="subview"><?php echo $this->load->view('subview');?></div> 
<div id="footer"><?php echo $this->load->view('footer');?></div> 

// in your script - used to load dynamic view on you subview div 
<script type="text/javascript"> 
    $.get('controller/get_page',{},function(data){ 
     if(data.success){ 
      $('#subview').html(data.view); 
     } 
    },'json') 
</script> 

消息我,如果有我的代碼有問題 快樂編碼--->:d