0

Im trying to make autocomplote for my form, the data was from selected combobox.

Heres my controller :

public function cari(){
    $kode_list=$this->request->getVar('list_produksi_toko');
    //$kode_list=$_GET['kode_list'];
    $cari['hasil'] =$this->produksiModel->mencari($kode_list);
    echo json_encode($cari);
} 

model :

public function mencari($keyword)
{
    //$query= $this->builder()->getWhere('list_produksi_toko',array('kode_list'=>$id));
    $query = $this->groupBy('kode_list')->like('kode_list', $keyword)->findAll();
    return $query;
}

and javascript in my view :

<script>

          $('#produk').change(function(){
            var list_produksi_toko = $(this).val();

            
            $.ajax({
             url:"/Produksi/cari",
             method: 'post',
             data:{list_produksi_toko: list_produksi_toko},
             success:function(data){
              jQuery.parseJSON(data);
               // var ddas = data.buyer
                alert (data);
                $.each(data, function(key, val){ 
                $('#buyer').val('<value="'+data['buyer']+'">');
                 // document.getElementById('nama_barang').value=val.nama_barang;
                 // document.getElementById('jenis_kain').value=val.jenis_kain;
                 // document.getElementById('warna').value=val.warna;
                 // document.getElementById('pcs').value=val.pcs;
               });
              }
            })
          });
          //   $.ajax({
          //     url: '/ProduksiModel/getProduksi',
          //     data:"produk="+produk ,
          //   }).success(function (data) {
          //     var json = data,
          //     obj = JSON.parse(json);
          //     $('#buyer').val(obj.nama);
          //     $('#status_order').val(obj.status_order);
          //     $('#nama_barang').val(obj.nama_barang);
          //     $('#jenis_kain').val(obj.jenis_kain);
          //     $('#warna').val(obj.warna);
          //     $('#pcs').val(obj.pcs);
          //   });
          // }
        </script>

I have a problem to show each data to each form, with this code it show error in the console like this:

Uncaught TypeError: Cannot use 'in' operator to search for 'length' in {"hasil":[{"kode_list":"LPT20060001","tanggal":"2020-06-08","status_order":"Full order","buyer":"IK001,Ik Collection","nama_barang":"IK05900,Renata Blouse","jenis_kain":"Mosscreep","warna":"IK05910,Grey","pcs":"63","tanggal_input":"2020-06-18","user":"Dena","keterangan":"Cutting Done"}]}

I already try to alert the data and it show : {"hasil":[{"kode_list":"LPT20060001","tanggal":"2020-06-08","status_order":"Full order","buyer":"IK001,Ik Collection","nama_barang":"IK05900,Renata Blouse","jenis_kain":"Mosscreep","warna":"IK05910,Grey","pcs":"63","tanggal_input":"2020-06-18","user":"Dena","keterangan":"Cutting Done"}]}

How can I fix it?

Sukma Qintara
  • 69
  • 1
  • 6

1 Answers1

1

You probably need to change the variable type of data to loop through it. You can check the type of the variable by console.log(typeof data). Following is the possible solution for your problem, see if this helps you.

success:function(data){
    
    console.log(typeof data);  // should be string
    
    data = jQuery.parseJSON(data);
    
    console.log(typeof data); // should be object
   
    $.each(data['hasil'], function(key, val){ 
        
         $.each(val, function(subkey, subval){
             
            console.log(subkey);
            console.log(subval);
    
            // all values are assigned to the elements with the same id as their keys(as your code suggests)
            $('#' + subkey).val('<value="' + subval + '">');
        });
    });
}
sauhardnc
  • 1,961
  • 2
  • 6
  • 16