如何使用Ajax将数据获取到模式引导程序?

时间:2019-07-17 06:46:25

标签: php jquery ajax codeigniter bootstrap-4

我有一页显示数据库中的项目列表。我想通过jquery以bootstrap modal打开项目详细信息。我知道ajax会引发警报,因此可以成功运行。但是不能打开模式。 您能告诉我错误的代码吗?谢谢

这些是我的代码:

这是模型

function get_detail_item($id){
    $this->db->select('*');
    $this->db->from('item', 'purchase');
    $this->db->join('purchase', 'purchase.id=item.id_purchase', 'inner');
    $this->db->join('status', 'status.id=item.id_status', 'inner');
    $this->db->join('category', 'category.id=item.id_category', 'inner');
    $this->db->where('item.id', $id);
    $query = $this->db->get();
    return $query->row();
}

这是控制器

 function detail_item($id){
    $this->load->model('item_model');
    $data = $this->item_model->get_detail_item($id);
    echo json_encode($data);

}

这是按钮

<a href="#Item_Detail" class="btn btn-outline-info" data-toggle="modal" data-target="#Item_Detail" data-id="<?php echo $row->id?>">Detail</a>

这是模态

<div class="modal fade" id="Item_Detail" tabindex="-1" role="dialog" aria-labelledby="exampleModalCenterTitle" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered modal-lg" role="document">
    <div class="modal-content">
        <div class="modal-header">
            <h5 class="modal-title" id="exampleModalLongTitle">Detail Item</h5>
            <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                <span aria-hidden="true">&times;</span>
            </button>
        </div>
        <div class="modal-body">
            <p id="proName"></p>
            <p id="proRoom"></p>
            <p id="proBuilding"></p>

        </div>
        <div class="modal-footer">
            <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
        </div>
    </div>
</div>

这是Ajax

<script type="text/javascript">
$('#Item_Detail').on('show.bs.modal', function (e) {
    var productID= $(e.relatedTarget).data('id');
    $.ajax({
        url:"<?php echo base_url().'admin/detail_item/'?>/" + productID,
        method: "GET",
        dataType:"JSON",
        success:function(data)
        {
            $('#proName').val(data.name_item);
            $('#proRoom').val(data.room);
            $('#proBuilding').val(data.building);

        }
    })
});

2 个答案:

答案 0 :(得分:0)

成功写下来

success:function(data)
        {
          $('#Item_Detail').modal('show')
            $('#proName').text('').text(data.name_item);
            $('#proRoom').text('').text(data.room);
            $('#proBuilding').text('').text(data.building);

        }

更改功能返回不回声

 function detail_item($id){
    $this->load->model('item_model');
    $data = $this->item_model->get_detail_item($id);
    return $data;

}

答案 1 :(得分:0)

针对您的模型尝试以下代码:

function get_detail_item($id){
    //I do not know if you will have get conflicts with select columns because of the same names
    $this->db->join('purchase', 'purchase.id=item.id_purchase', 'inner');
    $this->db->join('status', 'status.id=item.id_status', 'inner');
    $this->db->join('category', 'category.id=item.id_category', 'inner');
    $this->db->where('item.id', $id);
    $query = $this->db->get('item');
    return $query->row_array();
}

按钮:

<a href="#" class="btn btn-outline-info js-detail" data-id="<?php echo $row->id?>">Detail</a>

请,控制器保持回声和json_encode。像这样的ajax调用:

//It good practice to use a delegate event, but you choose it
$('.js-detail').on('click', function(){
var id =  $(this).data('id');
console.log("ID: " +id);
 $.ajax({
      type: 'GET',
      url: '/admin/detail_item/'+id,
      success:function(data)
        {
            var result = JSON.parse(data);
            console.log(result);
            $('#proName').text(result.name_item);
            $('#proRoom').text(result.room);
            $('#proBuilding').text(result.building);
            $('#Item_Detail').modal('show');

        }
        error: function (data) {
         alert("error");
      }
    });
});

请,请参阅控制台日志以在执行期间导致ajax和可能的错误。