how to use $.each to iterarte rows in jquery

Viewed 47

Im new to CodeIgniterIm getting data from this controller Controller:

public function selectAll(){
        $this->load->model('EmpModel');
        $res=$this->EmpModel->selectAll();
        if(count($res) > 0){
            echo json_encode(Array("success"=>"1"));
        }else{
            echo json_encode(Array("success"=>"0"));
        }
    }

I want to show it in view using $.each() view:

<table id="t_id">
        <tr>
            <th>id</th>
            <th>name</th>
            <th>email</th>
            <th>pass</th>
        </tr>
        <tbody>
    </table>
<button type="button" id="subBtn11"  >CLICK</button>
$("#subBtn11").click(function (){
    alert("ok");
   $.ajax({
      url:"<?php echo base_url('index.php/EmpCon/selectAll')?>",
      method:"get",
      dataType:"json",
      data:{

      },
      success:function (data){

          var arr=[];
         var trHTML = '';
              if(data.success==1){  
        $.each(data, function (i, item) {

            trHTML += '<tr><td>' + data.id[i] + '</td><td>' + data.name[i] + '</td><td>' + data.email[i]+'</td><td>'+data.password+'</td></tr>';
        });

        $('#t_id').append(trHTML);
        }else{
        alert("wrong");
        }
      },
      error:function(msg){
      alert(msg.responseText);
      }
   }); 
});

I googled the query but aint getting any satisfactory explanation.Thanks in advance.

3 Answers
Related