【发布时间】:2021-12-15 11:43:47
【问题描述】:
简单来说,我希望 status1 转到 ajaxleadsreport 控制器
稍微复杂一点的解释:
我有 2 个视图类,称为 indexreport 和 ajaxleadsreport。 ajaxleadsreport 获取所有数据并显示它。现在我有一个 GET 变量正在传递给 indexreport,我希望能够将它传递给 ajaxleadsreport 以根据已传递的 GET 变量过滤当前数据。
控制器类:
public function listsreport($slug='')
{
$status1 = $this->input->get('status');
print_r($status1);
$main['content']=$this->load->view('crm/leads/indexreport',$content,true);
}
public function ajaxleadsreport($p='')
{
$output = array( 'html'=>$this->load->view('crm/leads/ajaxleadsreport',$content, true));
echo json_encode($output);
}
indexreport 视图类:
<?php
$i=$return=$uriseg;
$post=$this->input->post(); $sess=$this->session->userdata();
$post = $post?$post:$sess;
?>
<div>
...
</div>
$(document).ready(function (){
getleads();
});
function getleads(p){
$.ajax({
type: "post",dataType:"json",async:false,cache:true,
url: "<?php echo site_url('leads/ajaxleadsreport'); ?>"+( parseInt(p)>0?'/'+p:''),
data: $('#objlistform').serialize(),
success: function(e){$('#leadswrap').hide().html(e.html).fadeIn('slow');}
}); return false;
}
ajaxleadsreport 视图类:
<?php
$sess=$this->session->userdata();
$status1 = $this->input->get('status');
// This is where I'm trying to put my GET value of status for filtering but it gives NULL.
$post = array('fstatus'=> $status,'fpriority'=> $sessn['fpriority']);
$postd = json_encode(array_filter($post));
?>
...
<script>
$(document).ready(function() {
function sendreq(){
setpostdatas();cleartable();getleads();
}
var slug = '<?php echo $slug?>';
var postd = '<?php echo $postd; ?>';
$('#item-list').DataTable({
"processing": true,
"stateSave": true,
"serverSide": true,
"ordering": false,
"ajax": {
url: "<?php echo site_url(); ?>leads/loadLeads",
data: {slug: slug, postdata: postd},
type : 'POST',
"dataSrc": function ( d ) {
d.myKey = "myValue";
if(d.recordsTotal == 0 || d.data == null){
$("#item-list_info").text("No records found");
}
return d.data;
}
},
'columns': [
{"data": "id", "id": "id"},
{"data": "lead_status", "lead_status": "lead_status"},
{"data": "priority", "priority": "priority"},
]
});
正如您在上面的代码中看到的,我在 ajaxleadsreport 视图类中尝试了$status1 = $this->input->get('status');,但其输出为 NULL,因为 GET 值是在我的 indexreport 视图类中传递的。当我在 indexreport 控制器中执行 print_r($status1) 时,它会给出正确的输出,但在 ajaxleadsreport 控制器中为 NULL。
所以基本上现在我需要一种方法来将此 GET 值传递给 ajaxleadsreport 控制器。
【问题讨论】:
-
不要从另一个控制器调用一个控制器。如果您需要在两者中运行一些通用代码,请将该代码移至服务(或可能是受保护的控制器方法)中,两者都可以调用。然后,如果该代码需要一些参数,请将它们作为函数参数添加到服务方法并从控制器传递。因此,控制器获取参数值(GET、POST 或其他任何内容,在不同的控制器中可能不同),只需将其传递给服务,然后获取结果。
-
@MagnusEriksson 好的,所以假设你的 URL 中有这个值(
http://localhost/leads/listsreport?status=9)。现在您希望能够访问 ajaxleadsreport 控制器中的status=9值。你能用代码的方式告诉我如何实现吗?
标签: php jquery ajax codeigniter