无法使用ajax在codeigniter中显示模态

时间:2018-01-17 16:01:26

标签: javascript php jquery ajax codeigniter

我正在使用codeigniter,我希望以模态显示详细数据,详细数据来自另一个表。

我有一个这样的按钮:

<button class="btn btn-info" onclick="detail_barang(&quot;<?php echo $tbrang->id_barang;?>&quot;)">Detail</button>

这是我的detail_barang函数:

function detail_barang(id)
{
  $('#form')[0].reset(); // reset form on modals

  $.ajax({
    url : "<?php echo site_url('admin/Barang/barang/ajax_detail_barang/')?>/" + id,
    type: "GET",
    dataType: "JSON",
    success: function(data)
    {
        $('#modal_detail').modal('show'); // show bootstrap modal when complete loaded
        $('.modal-title').text('Detail Barang'); // Set title to Bootstrap modal title

    },
    error: function (jqXHR, textStatus, errorThrown)
    {
        alert('Kesalahan!');
    }



    });
}

模态脚本:

<div class="modal fade" id="modal_detail">
<div class="modal-dialog">
      <div class="modal-content">
        <div class="modal-header">
          <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
          <h3 class="modal-title">Detail Barang</h3>
        </div>
        <div class="modal-body">
          <table class="table table-striped" id="tblGrid">
            <thead id="tblHead">
              <tr>
                <th>ID Barang</th>
                <th>No Inv</th>
                <th class="text-right">Kondisi</th>
              </tr>
            </thead>
            <tbody>
                <?php
                foreach($detail_barang as $detil)
                {?>
                    <tr>
                        <td><?php echo $detil->id_barang ?></td>
                        <td><?php echo $detil->no_inv ?></td>
                        <td><?php echo $detil->kondisi ?></td>
                    </tr>
                <?php } ?>
            </tbody>
          </table>
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-default " data-dismiss="modal">Close</button>
        </div>

      </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
  </div><!-- /.modal -->

ajax_detail_barang函数:

public function ajax_detail_barang($id)
{
    $where = array
    (
        'id_barang' => $id,
        'kondisi' => 'Ada'
    );

    $data['detail_barang'] = $this->modelku->get_by_id('detail_barang', $where)->result();
    echo json_encode($data);
}

get_by_id功能:

public function get_by_id($table, $where)
{
    $this->db->from($table);
    $this->db->where($where);
    $query = $this->db->get();

    return $query->row();
}

但是当我点击按钮时,出现错误&#34; Kesalahan!&#34;什么错了我的代码?

1 个答案:

答案 0 :(得分:1)

我看到的问题在于您如何使用来自ur model

的返回数据
public function get_by_id($table, $where)
{
$this->db->from($table);
$this->db->where($where);
$query = $this->db->get();

return $query->row();
//at this point it already contains the data u need
}

但是你在这里使用result();

$data['detail_barang'] = $this->modelku->get_by_id('detail_barang', $where)->result();
//i tried and u cant use result method if u already executed row method.

简而言之,请删除结果()

希望我的回答能帮到你。