Postingan lainnya
Many to many relationship di codeigniter
Saya punya proyek sistem pengarsipan surat. Proyek saya memiliki data dengan many to many relationship, pada fitur disposisi. Yaitu antara surat dan user disposisi. Logika nya 1 surat bisa memilih lebih dari 1 user yang menerima disposisi. 1 user dapat menerima disposisi lebih dari 1 surat
Jadi saya membuat tabel surat_user untuk mengelola relasi tersebut Pertanyaan nya bagaimana cara insert/update ke tabel relasi tersebut dan ke tabel surat, dengan kondisi saat input data surat, pengguna bisa melakukan multiple select.
Saya sudah bisa membuat view nya tetapi masih bingung dengan proses input dan update nya di MVC
Berikut MVC saya, saat ini hanya bisa menambahkan ke tabel surat Kode Controller Input data
public function add_sk() {
$this->_validate();
$data = array(
'no_surat'=>$this->input->post('no_surat'),
'id_status'=>$this->input->post('id_status')
);
$insert = $this->M_sk_non->add_sk($data);
echo json_encode(array("status" => TRUE));
}
Kode Controller Edit data
public function ajax_edit($id) {
$data = $this->M_sk_non->get_id_sk($id);
echo json_encode($data);
}
public function edit_sk() {
$this->_validate();
$data = array(
'id_surat'=>$this->input->post('id_surat'),
'no_surat'=>$this->input->post('no_surat'),
'id_status'=>$this->input->post('id_status')
);
$this->M_sk_non->update_sk(array('id_surat'=> $this->input->post('id_surat')), $data);
echo json_encode(array("status" => TRUE));
}
Model
var $table='surat';
public function add_sk($data) {
$this->db->insert($this->table,$data);
return $this->db->insert_id();
}
public function get_sk() {
$this->db->join('status_surat', 'status_surat.id_status = surat.id_status');
$query = $this->db->get('surat');
return $query->result();
}
public function get_id_sk($id) {
$this->db->from($this->table);
$this->db->where('id_surat',$id);
$query = $this->db->get();
return $query->row();
}
public function update_sk($where,$data) {
$this->db->update($this->table, $data, $where);
return $this->db->affected_rows();
}
Ajax pada View
function tambah_sk() {
save_method ='add';
$('#form')[0].reset();
//identifikasi jika input kosong
$('.form-group').removeClass('has-error');
$('.help-block').empty();
$('#form')[0].reset();
$('#modal_form').modal('show');
$('.modal_title').text('Tambah Surat Keluar Non Undangan');
}
function save() {
var url;
if(save_method=='add') {
url = '<?php echo site_url('index.php/SK_Non_Und/add_sk/'); ?>';
} else {
url = '<?php echo site_url('index.php/SK_Non_Und/edit_sk/'); ?>';
}
//var formData = new FormData($('#form')[0]);
$.ajax({
url:url,
type:"POST",
data: $('#form').serialize(),
dataType:"JSON",
success: function(data)
{
if(data.status) //Jika sukses tutup modal dan reload tabel
{
$('#modal_form').modal('hide');
location.reload();
}
else
{
for (var i = 0; i < data.inputerror.length; i++)
{
$('[name="'+data.inputerror[i]+'"]').parent().parent().addClass('has-error'); //select parent twice to select div form-group class and add has-error class
$('[name="'+data.inputerror[i]+'"]').next().text(data.error_string[i]); //select span help-block class set text error string
}
}
$('#btnSave').text('save');
$('#btnSave').attr('disabled',false);
},
error: function (jqXHR, textStatus, errorThrown)
{
alert('Error adding / update data');
}
});
}
function edit_sk(id) {
save_method = 'update';
$('#form')[0].reset();
//identifikasi jika input kosong
$('.form-group').removeClass('has-error');
$('.help-block').empty();
//load dari ajax
$.ajax({
url: "<?php echo site_url('index.php/SK_Non_Und/ajax_edit/'); ?>"+id,
type: "GET",
dataType: "JSON",
success: function(data) {
$('[name="id_surat"]').val(data.id_surat);
$('[name="no_surat"]').val(data.no_surat);
$('[name="id_status"]').select2("val",data.id_status);
$('#modal_form').modal('show');
$('.modal_title').text('Edit Surat Keluar Non Undangan');
},
error: function (jqXHR, textStatus, errorThrown)
{
alert('Error get data from ajax');
}
});
}
Modal View
<!-- Modal ketika button edit / tambah di klik,, manggilnya pake JS... pake onclick()-->
<div class="modal fade" id="modal_form" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal_title"></h4>
</div>
<form action="#" id="form" class="form-horizontal" method="post" enctype="multipart/form-data" role="form">
<div class="modal-body form">
<input type="hidden" name="id_surat" value="">
<div class="form-group">
<label class="control-label col-md-3">No Surat</label>
<div class="col-md-8">
<input type="text" name="no_surat" class="form-control no_surat" placeholder="Masukkan Nomor Surat">
<span class="help-block"></span>
</div>
</div>
<div class="form-body">
<div class="form-group">
<label class="control-label col-md-3">Status</label>
<div class="col-md-8">
<select class="form-control select2 id_status" name="id_status" onchange="setForm(this.value)" style="width: 100%;">
<option value=""></option>
<?php
$query = $this->db->query("SELECT * FROM status_surat");
foreach ($query->result() as $row) { ?>
<option value="<?php echo $row->id_status; ?>"> <?php echo $row->status; ?></option>
<?php
}
?>
</select>
<span class="help-block"></span>
</div>
</div>
</div>
<div id="disposisi" class="form-body" style=" display: none;">
<div class="form-group">
<label class="control-label col-md-3">Disposisi</label>
<div class="col-md-8">
<select class="form-control select2 id_user" name="id_user[]" multiple="multiple" style="width: 100%;>
<option value=""></option>
<?php
$query = $this->db->query("SELECT * FROM user");
foreach ($query->result() as $row) { ?>
<option value="<?php echo $row->id_user; ?>"> <?php echo $row->nama; ?></option>
<?php
}
?>
</select>
<span class="help-block"></span>
</div>
</div>
</div>
</div>
</form>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="button" onclick="save()" class="btn btn-primary">Save</button>
</div>
</div><!-- /.modal-content -->
</div><!-- /.modal-dialog -->
</div><!-- /.modal -->
Demo view
Belum ada Jawaban. Jadi yang pertama Jawaban
Login untuk ikut Jawaban