简体   繁体   English

使用模式显示来自另一个表的数据-Codeigniter

[英]Show data from another table with modal - Codeigniter

i have 2 tables 我有2张桌子

表1(图1)

Now when i click detail , i want to show data from table 2 which have reportcode as i click on table 1 (image 1) 现在,当我单击详细信息时,我想显示表2中的数据,因为我单击表1时具有报告代码(图像1)

表2(图2)

And now i want to show it on modal , so here is the example 现在我想在模式上显示它,所以这是示例

1) click detail button -> get reportcode -> show reimbursename,etc to modal 1)单击详细信息按钮->获取报告代码->显示报销名称等为模态

Can you explain to me what should i do first ? 你能告诉我我应该先做什么吗? and can you suggest me a plan please ,any answers will be appreciated. 并能给我建议一个计划,任何答案将不胜感激。 Thanks 谢谢

My suggestion is: 我的建议是:

1 - Add one class to detail button, ie: detailButton and a data attribute or href with the especific reportCode. 1-向详细信息按钮添加一个类,即: detailButton和具有especific reportCode的数据属性或href。

<table>
   <tr> 
     <td> ... </td>
     <td> <button class='detailButton' href='<?php echo $reportCode; ?>' ... </button> </td>

2 - Add jquery to the bottom of the page: 2-将jquery添加到页面底部:

$('.detailButton').click(function(e){
    e.preventDefault();
    var reportCode = $(this).attr('href');

    var url = "yourUrl/controller/function";
    $.post(url,{ code:reportCode },function(data){
        //do stuff
        //i.e: $('.modal').html(data).show();
    });
});

Now you have a function that gets the reportCode, sends it to your controller by POST, you return something and the function gets the response and attach to a html. 现在,您有了一个获取reportCode的函数,并通过POST将其发送到控制器,您返回了一些内容,并且该函数获取了响应并附加到html。

Note, this way you must return a table from your controller. 注意,这种方式必须从控制器返回一个表。 You could build dinamically too. 您也可以进行动态构建。

Hope it helps! 希望能帮助到你!

UPDATE: You could check the values to your model and then use a exisitin template (for example one that generates the detail table), and return to your view as data to be attached at the correct position (method 1): 更新:您可以检查模型中的值,然后使用一个exisitin模板(例如,一个生成明细表的模板),并作为要附加到正确位置的数据返回到视图(方法1):

function detail(){
    $getcode= $this->input->post('reportCode');
    $data['showdetail'] = $this->model_expreport->showdetail($getcode);
    $ret = $this->load->view('detail_template',$data,true); //return as data   
    print_r($ret);
}

Or you could use the Method 2: 或者,您可以使用方法2:

function detail(){    
   $getcode= $this->input->post('reportCode');
   $data['showdetail'] = $this->model_expreport->showdetail($getcode);

   $this->output->set_content_type('application/json');
   $this->output->set_output(json_encode($data));
}

This way, the view will recive a JSON that you could iterate and build your own page. 这样,视图将获取可迭代并构建自己的页面的JSON。 Or you could create the full view and return it as data (in order to only append to your view). 或者,您可以创建完整视图并将其作为数据返回(以便仅附加到视图中)。

You could use both. 您可以同时使用。

In the view, you will recive either a full view: 在视图中,您将获得完整视图:

$.post(url,{ code:reportCode },function(data){
    $('#modal').html(data); //put the 'detail' response to the modal
}

Or with JSON you must iterate and build your own div dinamically, there are a lot of tutorials for this: https://uno-de-piera.com/cargar-json-con-jquery-y-codeigniter/ 或使用JSON,您必须进行迭代并动态创建自己的div,为此有很多教程: https : //uno-de-piera.com/cargar-json-con-jquery-y-codeigniter/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM