Php 我可以从ajax响应加载CodeIgniter视图并使用$data obj吗?
好的,我加载的视图使用它从控制器接收的数据。然后,我使用jquery.ChangeListener来侦听选择选项列表中的更改。然后,这将触发一个函数运行,实际上,一个ajax请求将被发送到控制器中的一个函数。在该函数中,我需要尝试“重新加载”通过控制器索引加载的数据,但使用基于下拉列表值的新参数。到目前为止,数据的初始页面加载工作正常。change和ajax请求也在工作,但是,控制器函数没有重置页面数据。这是我所拥有的…任何帮助都将不胜感激 jQuery(在my members_home.php视图中):Php 我可以从ajax响应加载CodeIgniter视图并使用$data obj吗?,php,ajax,codeigniter,Php,Ajax,Codeigniter,好的,我加载的视图使用它从控制器接收的数据。然后,我使用jquery.ChangeListener来侦听选择选项列表中的更改。然后,这将触发一个函数运行,实际上,一个ajax请求将被发送到控制器中的一个函数。在该函数中,我需要尝试“重新加载”通过控制器索引加载的数据,但使用基于下拉列表值的新参数。到目前为止,数据的初始页面加载工作正常。change和ajax请求也在工作,但是,控制器函数没有重置页面数据。这是我所拥有的…任何帮助都将不胜感激 jQuery(在my members_home.php
您的控制器方法看起来是正确的。
folderSelector
方法中的$data['things']
使用正确-每个方法调用(来自单独的请求,如ajax请求)都不知道其他方法,因此您确实需要调用模型来设置$data['things']
不过,ajax成功处理程序不会对返回的数据执行任何操作。您应该将loadTables
功能更改为:
function loadTables(str) {
console.log("dfgdfg");
$.ajax({
url: '<?php echo base_url().'account/members_home/folderSelector';?>',
type: 'POST',
data: { folderCat: str },
success: function(output_string){
// here you need to inject the new data in the page
// in place of the old data
$('table').replaceWith(output_string);
}
});
}
函数加载表(str){
console.log(“dfgdfg”);
$.ajax({
url:“”,
键入:“POST”,
数据:{folderCat:str},
成功:函数(输出字符串){
//在这里,您需要在页面中注入新数据
//代替旧数据
$('table').replacetwith(输出字符串);
}
});
}
您的控制器方法看起来是正确的。folderSelector
方法中的$data['things']
使用正确-每个方法调用(来自单独的请求,如ajax请求)都不知道其他方法,因此您确实需要调用模型来设置$data['things']
不过,ajax成功处理程序不会对返回的数据执行任何操作。您应该将loadTables
功能更改为:
function loadTables(str) {
console.log("dfgdfg");
$.ajax({
url: '<?php echo base_url().'account/members_home/folderSelector';?>',
type: 'POST',
data: { folderCat: str },
success: function(output_string){
// here you need to inject the new data in the page
// in place of the old data
$('table').replaceWith(output_string);
}
});
}
函数加载表(str){
console.log(“dfgdfg”);
$.ajax({
url:“”,
键入:“POST”,
数据:{folderCat:str},
成功:函数(输出字符串){
//在这里,您需要在页面中注入新数据
//代替旧数据
$('table').replacetwith(输出字符串);
}
});
}
输出字符串不会是任何内容,因为folderSelector不会返回任何内容。它的唯一目的是用新参数重新加载数据['things'],但它的输出应该是表的HTML-可以尝试这样做:echo$this->load->view('includes/template2',$data,TRUE)
在folderSelector
方法的末尾。$data['things']不是表。。它只是保存视图将使用的变量,但是您将其传递给视图,然后该视图将以以下行输出:$this->load->View('includes/template2',$data')代码>-通过URL调用此方法,将输出带有新$things
数据的template2视图。我知道了!!您非常接近:$('table').replaceWith(输出字符串);需要更改$('table').html(输出字符串);输出字符串不会是任何内容,因为folderSelector不会返回任何内容。它的唯一目的是用新参数重新加载数据['things'],但它的输出应该是表的HTML-可以尝试这样做:echo$this->load->view('includes/template2',$data,TRUE)
在folderSelector
方法的末尾。$data['things']不是表。。它只是保存视图将使用的变量,但是您将其传递给视图,然后该视图将以以下行输出:$this->load->View('includes/template2',$data')代码>-通过URL调用此方法,将输出带有新$things
数据的template2视图。我知道了!!您非常接近:$('table').replaceWith(输出字符串);需要更改$('table').html(输出字符串);
function index()
{
$is_logged_in = $this->loggedin->loggedin();
if(!$is_logged_in)
{
redirect('account/createaccount', 'refresh');
}
else
{
$q = $this->account_model->folder();
$data['folder'] = $q;
// This part gets the data that's used in the html table
$userID = $this->session->userdata('id');
$data['things'] = $this->account_model->folder_Homepage($userID);
$data['main_content'] = 'account/members_home';
$this->load->view('includes/template2', $data);
}
}
// This function is used during the ajax request
public function folderSelector()
{
$fID = $this->input->post('folderCat');
$limit = 10;
$userID = $this->session->userdata('id');
$data['things'] = $this->account_model->folder_page($fID, $userID, $limit);
// This is the part that is not working, shouldn't I be able to reload
// $data['things'] and send it to the view with the new parameters?
$data['main_content'] = 'account/members_home';
$this->load->view('includes/template2', $data);
}
function loadTables(str) {
console.log("dfgdfg");
$.ajax({
url: '<?php echo base_url().'account/members_home/folderSelector';?>',
type: 'POST',
data: { folderCat: str },
success: function(output_string){
// here you need to inject the new data in the page
// in place of the old data
$('table').replaceWith(output_string);
}
});
}