2016-07-27 46 views
1

在下面甲笨控制器返回JSON,的Json返回陣列顯示器

{"r":[{"galleryid":"1","gname":"birthday","eventdate":"2016-07-20 00:00:00","totalphoto":"250","selectedphoto" :"100","glock":"0","userid":"1"},{"galleryid":"2","gname":"anniversary","eventdate":"2016-07-14 00:00:00","totalphoto":"500","selectedphoto":"251","glock":"0","userid":"1"}]} 

和從控制器代碼返回:

 $this->load->model('gallery_model'); 
    $data['r'] = $this->gallery_model->gallery_data($userid); 
    echo json_encode($data); 

但是鑑於它只是顯示:未定義

視圖代碼:

<script type="text/javascript"> 

// Ajax post 
$(document).ready(function() { 
    $("input#displaygallery").on('click', function (event) { 
     //var user_id = document.getElementById("userdropdown").value; 
     //alert(user_id); 

     event.preventDefault(); 
     var user_id = document.getElementById("userdropdown").value; 
     jQuery.ajax({ 
      type: "POST", 
      url: "<?php echo base_url(); ?>" + "admin_cont/gallery_controller/user_userdata", 
      dataType: 'json', 
      data: {userid: user_id}, 
      success: function (res) { 
       //console.log(res); 

       $.each(res, function (idx, obj) { 
        alert(obj.tagName); 
       }); 
      } 
     }); 
    }); 
}); 

</script> 
+0

尼斯,表現出更多的代碼,顯示Ajax調用方法 – rad11

+0

的完整的代碼以及如何你'view'顯示它們? –

+0

這裏我想這個JSON數據如下表格作爲表頭gallaeryid gname evendate作爲標題和其他相同的數據在tr – Jalpesh

回答

0

嘗試:

的javascript:

$("input#displaygallery").on('click', function (event) { 
    event.preventDefault(); 
    var user_id = document.getElementById("userdropdown").value; 
    jQuery.ajax({ 
     type: "POST", 
     url: "<?php echo base_url(); ?>" + "admin_cont/gallery_controller/user_userdata", 
     dataType: 'json', 
     data: {userid: user_id}, 
     success: function (res) { 
      var html = "<table>"; 
      html += "<thead>"; 

      html += "<tr>"; 
      html += "<th>eventdate</th>"; 
      html += "<th>galleryid</th>"; 
      html += "<th>glock</th>"; 
      html += "<th>gname</th>"; 
      html += "<th>selectedphoto</th>"; 
      html += "<th>totalphoto</th>"; 
      html += "<th>userid</th>"; 
      html += "<tr>"; 

      html += "<thead>"; 
      html += "<tbody>"; 
      for (i = 0; i <= res.r.length - 1; i++) { 
       html += "<tr>"; 
       html += "<td>" + res.r[i].eventdate + "</td>"; 
       html += "<td>" + res.r[i].galleryid + "</td>"; 
       html += "<td>" + res.r[i].glock + "</td>"; 
       html += "<td>" + res.r[i].gname + "</td>"; 
       html += "<td>" + res.r[i].selectedphoto + "</td>"; 
       html += "<td>" + res.r[i].totalphoto + "</td>"; 
       html += "<td>" + res.r[i].userid + "</td>"; 
       html += "<tr>"; 
      } 
      html += "</tbody>"; 
      html += "</table>"; 
      $("your cointainer id or class name").html(html); 
     } 
    }); 
}); 
+0

謝謝兄弟代碼完美工作 – Jalpesh

+0

做控制檯.log(res)成功,並提出問題 – rad11

+0

如果它的工作把回答有用並接受答案 – rad11