【问题标题】:Codeigniter - Show Information in Modal WindowCodeigniter - 在模态窗口中显示信息
【发布时间】:2018-08-06 15:38:49
【问题描述】:

我有一个模态窗口。它适用于 javascript。我自己尝试了一些东西,但我无法在模态窗口中显示客户的任何信息。单击信息按钮时,我想显示客户的信息。如何在模态窗口中显示任何客户信息?

控制器:

public function index()
{

    $viewData['countries'] = $this->db->get("country")->row();
    $viewData['customers'] = $this->customer_model->get_all();

    $this->lang->load('content', $this->session->userdata('people_lang'));
    $this->load->view('customers', $viewData);
}

public function getInfo(){

    $cusId = $this->input->post('cusId');

    $viewData['customers'] = $this->db->where("cusId", $cusId)->get("customer")->row();

    $this->load->view('customers/getInfo', $viewData);

}

此 Ajax 代码:

<script type="text/javascript">

    function showCustomers(str){

        $.ajax({

            type: "POST",

            data: { cusId: str },

            url: "<?=base_url('customers/getInfo');?>",

            success: function(data) {

                $('#divInfo').html(data);

            }

        });

    }

</script>

这个模态:

<!-- Modal -->
                              <div class="modal fade" id="myModal2" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
                                  <div class="modal-dialog modal-lg">
                                      <div class="modal-content">
                                          <div class="modal-header">
                                              <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                                              <h4 class="modal-title">Modal Tittle</h4>
                                          </div>
                                          <div class="modal-body">
                                              <div id="divInfo"></div>
                                          </div>
                                          <div class="modal-footer">
                                              <button data-dismiss="modal" class="btn btn-default" type="button">Close</button>
                                              <button class="btn btn-warning" type="button"> Confirm</button>
                                          </div>
                                      </div>
                                  </div>
                              </div>
                  <!-- modal -->

这个视图:

<a data-toggle="modal" href="#myModal3"><button class="btn btn-primary btn-xs" onclick="showCustomers(this.id);" id="<?php echo $customers->cusId; ?>"><i class="fa fa-eye"></i></button>

但这不起作用。我哪里做错了?

【问题讨论】:

  • 究竟是什么问题?您可以在模态正文中输出您喜欢的任何内容。您尝试了哪些方法来获取客户的数据,为什么您的方法不起作用?
  • 您想在模态中显示所有客户还是只显示一个?
  • 在客户表中单击它们的附近信息按钮时只有其中一个。实际上按ID显示信息。

标签: php ajax codeigniter model modal-dialog


【解决方案1】:

我认为您可以通过添加几行来做到这一点。

注意* 代码未经测试,我实际上可以画出解决这个问题的想法。

控制器的索引函数中

public function index(){
        $viewData['countries'] = $this->db->get("country")->row();
        $viewData['customers'] = $this->customer_model->get_all();

         //**new line
         // Assuming you already have method in your Model to get customer by id
        // Check if there's a GET request of 'customerID' then assign to the $viewDataByID array the data from the model, else nothing.

        $viewDataByID['customersById'] = (isset($_GET['cusomterID'])) ? $this->customer_model->get_by_id($id) : '';

        //Then you'll have to update the variable sent to the view by storing both in one array
        $allData['viewData']= $viewData;
        $allData['viewDataByID'] = $viewDataByID;

        $this->lang->load('content', $this->session->userdata('people_lang'));
        $this->load->view('customers', $allData);// $allData get sent to the view
    }

在您的模式中,您可以发送请求

请更新视图中的变量。您可以执行 print_r($allData) 或 var_dump($allData) 以清楚地查看数据树以及如何使用它们。

或者,如果您不想为此使用索引,您可以在控制器中创建另一个函数来按 id 处理客户,json_encode 结果,然后使用 jQuery 获取数据。

希望这会有所帮助。

【讨论】:

    【解决方案2】:

    这是您可以做到的一种方法:

     <!--modify button to include an onclick element, pass a value to it-->
    <button class="btn btn-primary btn-xs" id="info_modal" onclick="show_modal('<?=$customer_name;?>')"><i class="fa fa-eye"></i></button>
    

    您可以修改您的模态以使用一些 id 来预测您的数据:

    <div class="modal fade" id="info_modal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
       ....
    <div class="modal-body">
         View File List According to Customer ID.
         <div id="customer_name"></div>
    </div>
    

    然后在你的脚本中(如果使用 jquery):

    //add passed string to the modal and then display it 
    function show_modal(customer_name){
         $("#customer_name").html(customer_name);
         $("#info_modal").modal('show');     
    }
    

    【讨论】:

    • 什么版本的引导程序?
    • @Mark 您还需要将您的模态id 更新为info_modal,或者更改选择器
    【解决方案3】:

    我自己回答了这个问题。

    <td data-title="Edit" align="center"><a href="<?php echo base_url("customer_edit/edit/$customer->cusId"); ?>"><button class="btn btn-primary btn-xs"><i class="fa fa-pencil"></i></button></a> <a data-toggle="modal" href="#<?php  echo $customer->cusId; ?>"><button class="btn btn-primary btn-xs"><i class="fa fa-eye"></i></button></a></td>
    

    我编辑了一个类似href="#&lt;?php echo $customer-&gt;cusId; ?&gt;的href 之后我只是在模态顶部写了这段代码:

    <?php foreach ($customers as $get) { ?>
                    <div class="modal fade" id="<?php  echo $get->cusId; ?>" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
        <?php } ?>
    

    我这样编辑 id id="#&lt;?php echo $get-&gt;cusId; ?&gt;

    现在效果更好了!

    【讨论】:

      猜你喜欢
      • 2019-01-10
      • 2017-05-16
      • 1970-01-01
      • 2016-07-27
      • 2013-09-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多