How to use $ .each for iterarte strings in jquery

Im new to CodeIgniterIm receiving 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 this with $ .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 looked at a Google request but did not get a satisfactory explanation. Thanks in advance.

+4
source share
3 answers

I have a sample for $.eachPlease check: https://jsfiddle.net/erwzpvok/

You can modify your controller file below to get the data:

//Considering $res contain your data array.
$json = array("status" => "1", "data" =>$res);

echo json_encode($json);

And then try below:

    var trHTML = '';
      // it might be data.data as per controller.
    $.each(data, function(i, item) {
// add all other fields, that you want to show in below trHTML.
      trHTML += '<tr><td>' + item.Id + '</td><td>' +item.Name + '</td></tr>';

    });

     $('#t_id').append(trHTML);
+2
source

Try as shown below, but you did not return the object from the controller.

$.each(data, function (i, item) {

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

, 1

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

ss $.each()

$. each (data.ss, function (i, item) {

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

0

Source: https://habr.com/ru/post/1677140/


All Articles