Detail 这是我的 detail_barang -6ren">
gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-31 21:21:05 25 4
gpt4 key购买 nike

我正在使用 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();
}

但是当我点击按钮时,错误出现“Kesalahan!”我的代码有什么问题?

最佳答案

我看到的问题是你如何使用你的模型返回的数据

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.

简而言之,删除result()

希望我的回答对你有帮助。

关于javascript - 无法使用ajax在codeigniter中显示模态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48305239/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com