Wahyu Artadianto
Wahyu Artadianto

Reputation: 117

how using foreach in ajax codeigniter

Dear Expert need help how to add foreach between variable tbaris :

function show_infouser(Kode)
   { save_method = 'update';
       $('#form')[0].reset(); // reset form on modals
       $('.form-group').removeClass('has-error'); // clear error class
       $('.help-block').empty(); // clear error string
       $.ajax({url : "<?php echo site_url('infouser/ajax_user')?>/" + Kode,
       type: "GET",
       dataType: "JSON",
       success: function(data){
         if (data == null)
         { showConfirmButton: false }); }
         else {
// want to show foreach in here
           var tbaris = '<tr><td style="width:3px; font-size:11px;"><center>'+data.No+'</center></td>';
           tbaris += '<td style="width:3px; font-size:11px;"><center>'+data.Name+'</center></td>';
           tbaris += '<td style="width:3px; font-size:11px;"><center>'+data.Job+'</center></td>';
           tbaris += '<td style="width:3px; font-size:11px;"><center>'+data.Status+'</center></td>';
           $('#tableuser tbody').html(tbaris);
           $('#modal_form1').modal('show');
           $('.modal-title').text('SHOW ROOM');}},
          error: function (jqXHR, textStatus, errorThrown)
               {
                   alert('Error get data from ajax' + errorThrown);
                   alert('Error get data from ajax' + textStatus);
                   alert('Error get data from ajax' + jqXHR);
               }
           });
       }

Upvotes: 2

Views: 1553

Answers (1)

Hikmat Sijapati
Hikmat Sijapati

Reputation: 6969

Try as below using $.each()...

 function show_infouser(Kode)
       { 
           save_method = 'update';
           $('#form')[0].reset(); // reset form on modals
           $('.form-group').removeClass('has-error'); // clear error class
           $('.help-block').empty(); // clear error string

           $.ajax({
           url : "<?php echo base_url('infouser/ajax_user');?>/" + Kode,
           type: "GET",
           dataType: "JSON",
           success: function(response){
             if (response == null)
             { showConfirmButton: false } 
             else {
// want to show foreach in here
            var tbaris ='';
            $.each(response,function(index) {
            tbaris += '<tr><td style="width:3px; font-size:11px;"><center>'+response[index].No+'</center></td>';
           tbaris += '<td style="width:3px; font-size:11px;"><center>'+response[index].Name+'</center></td>';
           tbaris += '<td style="width:3px; font-size:11px;"><center>'+response[index].Job+'</center></td>';
           tbaris += '<td style="width:3px; font-size:11px;"><center>'+response[index].Status+'</center></td>';
           }
           $('#tableuser tbody').html(tbaris);
           $('#modal_form1').modal('show');
           $('.modal-title').text('SHOW ROOM');
             }
         },
          error: function (jqXHR, textStatus, errorThrown)
               {
                   alert('Error get data from ajax' + errorThrown);
                   alert('Error get data from ajax' + textStatus);
                   alert('Error get data from ajax' + jqXHR);
               }
           });
       }

Upvotes: 2

Related Questions