试图用incodeigniter设置crud。我从来没有做过,所以我不明白我哪里出了问题。我能够将数据插入数据库,并显示插入数据窗口。我附上了结果的图片。从数据库中检索数据,但未将其加载到表中。
结果:
CONTROLLER
function get($id = null){
$this->load->model('model_peca');
$this->load->model('model_perfil');
// $this->load->view('telas/pecas/view_addvalue');
if ($id !=null) {
$data = $this->model_peca->getData($id);
} else {
$data = $this->model_peca->getData();
}
$dados ['telaativa'] = 'pecas';
$dados ['tela'] = 'pecas/view_addvalue';
$this->load->view('view_home', $dados);
echo json_encode($data->result_object());
}
function edit($id){
$this->load->model('model_peca');
$data = $this->model_peca->getData($id)->result_object();
// }
// else{
$this->load->view('telas/pecas/view_edit',array('data'=>$data));
// }
}
function add(){
//$this->load->view('insert');
$this->load->model('model_peca');
$this->load->view('telas/pecas/view_insert');
}
function insert(){
$this->load->model('model_peca');
if ($this->input->post()) {
$peca_parent_id = $this->input->post('peca_parent_id');
$nr_peca = $this->input->post('nr_peca');
//echo $peca_parent_id;
// die;
$insert = $this->model_peca->insert($peca_parent_id,$nr_peca);
if ($insert > 0) {
$result = 200;
}
else{
$result = 0;
}
echo json_encode(array("result"=>$result));
}
}
function deleteoperacao($id){
$delete = $this->model_peca->delete($id);
if ($delete > 0) {
$result = 200;
}
else{
$result = 0;
}
echo json_encode(array("result"=>$result));
}
function update($id){
if ($this->input->post()) {
$peca_parent_id = $this->input->post('peca_parent_id');
$nr_peca = $this->input->post('nr_peca');
// echo $barang;
$insert = $this->model_peca->update($id,$peca_parent_id,$nr_peca);
if ($insert > 0) {
$result = 200;
}
else{
$result = 0;
}
echo json_encode(array("result"=>$result));
}
}
模型
function getData($id =null){
if ($id ==null) {
$data = $this->db->get('peca_value');
}
else{
$data = $this->db->get_where('peca_value',array('id'=>$id));
}
return $data;
}
function insert($peca_parent_id,$nr_peca){
$data = array('peca_parent_id'=>$peca_parent_id,'nr_peca'=>$nr_peca);
return $this->db->insert('peca_value', $data);
}
function update($id,$peca_parent_id,$nr_peca){
$data = array('peca_parent_id'=>$peca_parent_id,'nr_peca'=>$nr_peca);
$where = array('id'=>$id);
return $this->db->update('peca_value', $data,$where);
}
function delete($id){
$where = array('id'=>$id);
return $this->db->delete('peca_value',$where);
}
}
查看
<head>
<link rel="stylesheet" href="assets/css/bootstrap/bootstrap.min.css">
<link rel="stylesheet" href="assets/datatables/dataTables.bootstrap.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<!-- <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous"> -->
<script src="http://code.jquery.com/jquery-3.2.1.min.js"></script>
<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
<div class="content-wrapper">
<section class="content-header">
<h1>Lista Peças
</h1>
<ol class="breadcrumb">
<li><a href="#"><i class="fa fa-dashboard"></i> Home</a></li>
<li>Peças</li>
<li class="active">Lista pecas</li>
</ol>
</section>
</head>
<body>
<div class="container" style="margin-top: 50px;">
<button class="btn btn-primary btn-act" data-name='add' href="<?php echo base_url('Home/add') ?>"> Nova Operação</button>
<br>
<br>
<div class="row">
<div class="col-lg-12">
<div class="panel panel-default">
<h1></h1>
<table class="table table-hover">
<thead>
<th>peca_parent_id</th><th>nr_peca</th><th>Editar/Apagar</th>
</thead>
<tbody id="wrap">
</tbody>
</table>
</div>
</div>
</div>
</div>
<div id="myModal" class="modal fade" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title"></h4>
</div>
<div class="modal-body">
</div>
<div class="modal-footer">
</div>
</div>
</div>
<!------------------------------------------------------------------------------->
<style type="text/css">
body{
background: #eee;
}
</style>
<script src="../assets/js/jquery/jquery-2.2.3.min.js"></script>
<script src="../assets/js/bootstrap/bootstrap.min.js"></script>
<script src="../assets/datatables/jquery.dataTables.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
list();
$(document).on('click',".update",function(){
$.ajax({
url: "<?php echo base_url('Home/update'); ?>"+$(this).data('id'),
method: "POST",
data: {
peca_parent_id: $(".name").val(),
nr_peca: $(".price").val()
},
dataType: 'json',
success:function(r){
if (r.result ==200) {
$("#myModal").modal('hide');
list();
}
}
});
});
$(document).on('click',".insert",function(){
$.ajax({
url: "<?php echo base_url('Home/insert'); ?>",
method: "POST",
data: {
peca_parent_id: $(".name").val(),
nr_peca: $(".price").val()
},
dataType: 'json',
success:function(r){
if (r.result ==200) {
$("#myModal").modal('hide');
list();
}
}
});
});
$(document).on('click','.btn-delete',function(){
$.ajax({
url: $(this).attr("href"),
dataType : 'json',
success: function(r){
if (r.result == 200) {
$("#myModal").modal('hide');
list();
}
}
});
});
function list(){
$.ajax({
url: "<?php echo base_url('Home/get'); ?>",
dataType:"json",
success: function(r){
var html ='';
var i;
for (i = 0;i < r.length; i++) {
html += "<tr>";
html += "<td>"+r[i].peca_parent_id+"</td>";
html += "<td>"+r[i].nr_peca+"</td>";
html += "<td><button data-name='edit' class='btn btn-act btn-update btn-success' href='<?php echo base_url('Home/edit'); ?>"+r[i].id+"'>Edit</button> <button class='btn-act btn btn-danger' data-name='delete' href='<?php echo base_url('Home/deleteoperacao'); ?>"+r[i].id+"'>Hapus</button></td>";
html += "</tr>";
}
$("#wrap").html(html);
}
});
}
});
// aksi btn-act
$(document).on("click", ".btn-act",function(){
var name = $(this).data('name');
// jika data-name sama dengan edit;
if (name=='edit') {
$("#myModal .modal-title").html("Edit Data");
$("#myModal .modal-body").load($(this).attr('href'));
$("#myModal").modal('show');
$("#myModal .modal-footer").html('<button type="button" class="btn btn-danger btn-close" data-dismiss="modal">Tutup</button>')
}
if (name=='add') {
$("#myModal .modal-title").html("Tambah Data");
$("#myModal .modal-body").load($(this).attr('href'));
$("#myModal").modal('show');
var href = $(this).attr("href");
$("#myModal .modal-footer").html('<button type="button" class="btn btn-danger btn-close" data-dismiss="modal">Tutup</button>')
}
if(name=='delete'){
$("#myModal .modal-title").html("Konfirmasi");
$("#myModal .modal-body").html("Apakah Anda Akan Menghapusnya?");
$("#myModal").modal('show');
var href = $(this).attr("href");
$("#myModal .modal-footer").html('<button href="'+href+'" type="button" class="btn btn-danger btn-delete" data-dismiss="modal">Hapus</button>')
}
});