2012-03-07 101 views
1

我有一个页面,我想保持静态与两个div,我想在用户导航过程中加载不同的html视图。我希望内容在不刷新整个页面的情况下进行更改,只刷新这两个div。如何加载视图内的div - codeigniter

如何将特定的视图加载到这些div中? 我已经有菜单导航,确定要加载到这些div和控制器功能的html视图到菜单选项,我得到使用JavaScript,然后在主页的控制器中使用它,我只是不知道如何加载内容。

回答

4

我认为你需要使用jQuery和笨

这将是你的jQuery代码

$('nav').click(function(){//element to be click to load the page in the div 
    $(your_div_element).load('controller/method'); 

}); 

这将是你笨

function method(){ 

    $data['result'] = ''//you can use this if you need to pass some data to the view 


    print $this->load->view('your_view',$data,true);//This will load your view page to the div element 

} 

古德勒克!

+0

泰克斯,它的工作。 – zesilva 2012-03-07 16:06:35

+0

您的欢迎兄弟 – 2012-03-08 03:37:42

+0

如何传递值/变量? – Jobayer 2015-12-18 08:35:04

5

您可以使用AJAX加载一些Javascript的视图。

使用jQuery:

$('a.nav-button').click(function(e) { 
    // prevent the default action when a nav button link is clicked 
    e.preventDefault(); 

    // ajax query to retrieve the HTML view without refreshing the page. 
    $.ajax({ 
    type: 'get', 
    url: '/path/to/your/controller/method', 
    dataType: 'html', 
    success: function (html) { 
     // success callback -- replace the div's innerHTML with 
     // the response from the server. 
     $('#yourDiv').html(html); 
    } 
    }); 
}); 
2

为了把内容而无需刷新,你应该使用AJAX,可以说你有一个目录(的WebParts)下的一个文件中的div,你要添加它的内容到div叫my_div;首先创建一个呈现刚视图(DIV)控制器上的方法,你希望把这样的事情

function part($part = false) 
{ 
    $data = array(); // you can add data to your div, if you need to. 
    $this->load->view("webparts/$part", $data); 
} 

然后用JavaScript(jQuery的在这种情况下),你可以调用

$('#my_div').load('controller/mypart'); 

注意:我喜欢用“负荷”,因为它可以让我选择即时请求视图的某些部分,使用这样

$('#my_div').load('controller/mypart #certainsection'); 

东西会加载从ID =“certainsection”的元素我部分观点