我的删除按钮有问题

时间:2017-01-16 23:36:22

标签: javascript php codeigniter-3

Image

我的删除按钮有问题。如果我在第2行按删除,数据将在同一行删除。但是,以防万一。如果我按第2行中的删除按钮,数据将丢失是底部的数据。不是第2行中的数据。当我将javascript添加到我的代码中时,错误开始。

我的控制器代码

<?php

public function index()
{
    $data['result'] = $this->Magen->view();
    $config['menuagen'] = 'active';
    $this->load->view('element/header', $config);
    $this->load->view('agen/list_agen', $data);
    $this->load->view('element/footer');
}

public function manage($id=""){
    $submit = $this->input->post('submit');
    if($submit){
        $id = $this->input->post('id');
        $datainput = array(
            'id_agen' => $this->input->post('id_agen'),
            'nama_agen' => $this->input->post('nama_agen'),
            'alamat' => $this->input->post('alamat'),
            'no_telp' => $this->input->post('no_telp'),
            'keterangan' => $this->input->post('keterangan'),
        );

        if ($id){
            $this->Magen->update($id, $datainput);
        }else{
            $this->Magen->insert($datainput);

        }

        redirect('agen');
    }

    if ($id){
        $data['detail'] = $this->Magen->detail($id);
        $data['mode'] = 'Edit';
    }else{
        $data['mode'] ='Tambah';
    }

    $config['menuagen'] = 'active';
    $this->load->view('element/header', $config);
    $this->load->view('agen/form_agen', $data);
    $this->load->view('element/footer');
}

public function delete($id){
    $this->Magen->delete($id);
    redirect('agen');
}
}

我的模型代码

<?php

//query insert/save

public function insert($data){
    return $this->db->insert('agen', $data);
}

//update

public function update($id, $data){

$this->db->where('id_agen', $id);
return $this->db->update('agen', $data);
}

//delete

public function delete($id){
    return $this->db->delete('agen', array('id_agen' => $id));
}

//view

public function view(){
    return $this->db->get('agen')->result_array();
}

//query detail

public function detail($id){
    $this->db->where('id_agen', $id);
    return $this->db->get('agen')->result_array();
}

我的HTML代码

        <h2 class="view-title">Kelola Agen</h2>
    <div id="masonry" class="row">
        <div class="module-wrapper masonry-item col-lg-12 col-md-12 col-sm-12 col-xs-12">
            <section class="module module-headings">
                <div class="module-inner">
                    <div class="module-heading">
                        <h3 class="module-title">Daftar Agen</h3>
                        <ul class="actions list-inline">
                            <li><a href="<?php echo base_url();?>index.php/Agen/manage"><span class="icon icon_plus"></span></a></li>
                            <!--<li><button type="button" class="btn btn-xs btn-primary" data-dismiss="modal"><span class="icon icon_plus"></span></button></li>-->
                        </ul>

                    </div>
                        <div class="module-content collapse in" id="content-1">
                            <div class="module-content-inner no-padding-bottom">
                                <div class="table-responsive">
                                    <!-- di sini kontenny -->
                                <table id="datatables-1" class="table table-striped display">
                                    <thead>
                                        <tr>
                                            <th>No</th>
                                            <th>ID Agen</th>
                                            <th>Nama Agen</th>
                                            <th>Alamat</th>
                                            <th>No. Telp</th>
                                            <th>Keterangan</th>
                                            <th>Action</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <?php if(@$result):?>
                                            <?php $i = 1;?>
                                            <?php foreach ($result as $row):?>
                                                <tr>
                                                    <td><?php echo $i++;?></td>
                                                    <td><?php echo $row['id_agen'];?></td>
                                                    <td><?php echo $row['nama_agen'];?></td>
                                                    <td><?php echo $row['alamat'];?></td>
                                                    <td><?php echo $row['no_telp'];?></td>
                                                    <td><?php echo $row['keterangan'];?></td>
                                                    <td>
                                                    <a href="<?php echo base_url();?>index.php/agen/manage/<?php echo $row['id_agen'];?>"class="btn btn-xs btn-primary"><i class="fa fa-pencil-square-o" aria-hidden="true"></i>Edit</a>
                                                    <a class="btn btn-xs btn-danger AlertDelete" ><i class="fa fa-trash"></i>Delete</a>
                                                    </td>
                                                </tr>
                                            <?php endforeach?>
                                        <?php endif?>
                                    </tbody>
                                </table>
                                </div>
                            </div>
                        </div>
                    </div>
                </section>
            </div>
        </div>

我的Javascript代码

<script type="text/javascript">
    $('.AlertDelete').click(function(e) {

      swal({
          title: "Yakin akan menghapus data?",
          text: "Data yang sudah dihapus tidak akan bisa dikembalikan",
          type: "warning",
          showCancelButton: true,
          confirmButtonColor: '#DD6B55',
          confirmButtonText: 'Iya, hapus data!',
          cancelButtonText: "Batalkan!",
          confirmButtonClass: "btn-danger",
          closeOnConfirm: false,
          closeOnCancel: false
        },

        function(isConfirm) {
          if (isConfirm) {
            var data = $(this).serialize(); 
            $.post('<?php echo base_url();?>index.php/agen/delete/<?php echo $row['id_agen'];?>', data);
            swal("Terhapus!", "Data berhasil dihapus. Kamu akan diarahkan ke halaman sebelumnya secara otomatis", "success");
            setTimeout("location.href = 'http://localhost/cvatikan/index.php/agen';",1000);
          } else {
            swal("Dibatalkan", "Hapus data dibatalkan", "error");
          }
        });
    });
</script>

我希望有人可以解决这个问题。在此之前谢谢:)

1 个答案:

答案 0 :(得分:0)

php代码中,$ result迭代后,您可以回显EACH $row['id_agen']

<?php foreach ($result as $row):?>
<tr>
  <td><?php echo $i++;?></td>
  ...
  <td>
    <a href=..<?php echo $row['id_agen'];?...>Edit</a>
    ...
  </td>
</tr>
<?php endforeach?>

但是在js代码中,$row[id_agen]如何绑定到每个单一

$.post('<?php echo base_url();?>index.php/agen/delete/<?php echo $row['id_agen'];?>', data);