codeigniter:在视图的div内加载视图 [英] codeigniter :load view inside div of a view
问题描述
让我说这个视图(主)
< body&
lorem epsim
< div table>< / div>
lorem epsim
< / body>
在控制器control1.php中执行
$ this-> load-> view('header');
$ this-> load-> view('main',$ data);
$ this-> load-> view('footer');
现在我需要从另一个视图(tbl.php)加载div = table的内容,这是从另一个控制器调用
control2.php
function load_table {
$ data ['x'] = 1;
$ this-> load-> view('tbl.php',$ data);
}
tbl.php检视
< ul> $ x< / ul>
我该如何做?
我尝试从控制器1加载控制器2,并将函数load_table分配给变量,并将其传递给主视图,但它没有工作cuz load->视图执行,而不是保存输出到变量..
原因:
i需要这样做是因为tbl.php视图是一个复杂的表,我需要刷新和加载通过ajax调用,所以我需要它只要在不同的视图
所以可以有人解释我怎么可以工作这出来?解决方案从另一个单独的控制器调用一个控制器方法。然而,您可以获取表视图的输出并使用:
// main.php
< ; body>
lorem epsim
< div table><?php echo $ table_content; ?>< / div>
lorem epsim
< / body>
。
// control1.php
$ table_data ['x'] = 1;
$ data ['table_content'] = $ this-> load-> view('tbl.php',$ table_data,TRUE);
$ this-> load-> view('header');
$ this-> load-> view('main',$ data);
$ this-> load-> view('footer');
所以,你得到的数据传递给tbl.php视图, >视图方法 - 也将
TRUE
作为第三个参数 - 它将以字符串形式返回该视图的内容(而不是将其输出到浏览器)。现在,你有一个$ data变量传递到main
视图,包含表html,你可以在主视图中回显它。
如何从视图获取
$ data ['table_content']
数据取决于你。您可以创建另一个控制器方法在 control1.php中,您可以创建一个帮助文件,可以将视图加载到字符串中并返回等。lets say that i hv this view (main)
<body> lorem epsim <div table></div> lorem epsim </body>
in controller control1.php i do
$this->load->view('header'); $this->load->view('main',$data); $this->load->view('footer');
Now i need to load content of div=table from another view (tbl.php),which is called from another controller
control2.php
function load_table(){ $data['x']=1; $this->load->view('tbl.php',$data); }
tbl.php view
<ul>$x</ul>
how can i do that ?
i tried to load controler 2 from controller 1 and assign the function load_table to variable and pass that to main view, but it didnt work cuz load->view is executed instead of saving output to variable..
Reason: i need to do this is that tbl.php view is a complex table that i need to refresh and load via ajax calls, so i need it to be on different view alone so can some one explain to me how can i work this out ?
解决方案You can't call one controller method from another, separate controller. You can, however, get the output of the table view and use that:
// main.php <body> lorem epsim <div table><?php echo $table_content; ?></div> lorem epsim </body>
.
// control1.php $table_data['x'] = 1; $data['table_content'] = $this->load->view('tbl.php', $table_data, TRUE); $this->load->view('header'); $this->load->view('main',$data); $this->load->view('footer');
So, you get the data to pass to the tbl.php view and pass that to the load->view method - also passing
TRUE
as the third parameter - which will return the contents of that view as a string (instead of outputting that to the browser). Now, you have a $data variable to pass to themain
view with the table html included and you can just echo that out in the main view.How you get the
$data['table_content']
data from the view is up to you. You can create another controller method inside control1.php, you can create a helper file that can load the view into a string and return that, etc.这篇关于codeigniter:在视图的div内加载视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!