如何在div中加载视图 - codeigniter

时间:2012-03-07 00:37:12

标签: codeigniter view load

我有一个页面,我希望保持静态,有两个div,我想在用户导航期间加载不同的html视图。我希望在不刷新整个页面的情况下更改内容,只刷新这两个div。

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

3 个答案:

答案 0 :(得分: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);
    }
  });
});

答案 1 :(得分:4)

我认为您需要使用jquery和Codeigniter

这将是你的jQuery代码

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

});

这将是您的Codeigniter

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 

}

古德勒克!

答案 2 :(得分:2)

为了放置内容而不刷新你应该使用ajax,假设你在目录(webparts)下的文件中有div,并且你想将它的内容添加到名为my_div的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');

将从mypart视图

加载id =“certainsection”的元素
相关问题