2016-06-28 11 views
3

Hey guy's Ich bin neu in der Programmierung. und ich möchte Json Daten in HTML-Tabelle mit jQuery anzeigen.JSON-Daten im HTML-Tabellenformat anzeigen

Ausgang kam vom Server:

{"status":1,"insert":1,"data":[{"id":"1","name":"Jatin","email":"[email protected]","status":"1"},{"id":"2","name":"Shubham","email":"[email protected]","status":"1"},{"id":"3","name":"Aneh","email":"[email protected]","status":"1"},{"id":"52","name":"php","email":"[email protected]","status":"test"},{"id":"53","name":"faf","email":"as","status":"d"},{"id":"54","name":"taus","email":"alksd","status":"fdasd"},{"id":"55","name":"nake","email":"nakk","status":"naii"},{"id":"56","name":"test1","email":"tausf","status":"dfasd"},{"id":"57","name":"","email":"","status":""},{"id":"58","name":"shubu","email":"[email protected]","status":"12"},{"id":"59","name":"panku","email":"[email protected]","status":"3"},{"id":"60","name":"Jatin","email":"[email protected]","status":"123d"}]}

HTML-Code:

<table border="1" align="center"> 
    <tr> 
     <td> <input type="button" id="display" value="Display All Data" /> </td> 
    </tr> 
</table> 
<div id="responsecontainer" align="center"> 
</div> 

jquery:

<script> 

     $(document).ready(function() { 

     $("#display").click(function() {     

      $.ajax({ //create an ajax request to load_page.php 
      url:'http://localhost/webservice/php_webservices/WebServices.php?method=select', 
      type: "POST",    
      dataType: "html", //expect html to be returned     
      success: function(response){      
       $("#responsecontainer").html(response); 
       //alert(response); 
      } 

     }); 
    }); 
    }); 
    </script> 
+0

Probieren Sie etwas aus, um die Antwort zu verarbeiten, konvertieren Sie es in das JavaScript-Objekt, führen Sie eine Schleife für jedes Element im Array aus, erstellen Sie das erforderliche HTML-tr-td durch die Zeichenfolgenmanipulationen und Sie können loslegen. – Dharmang

+0

Oder Check AngularJS (Tabellen Beispiel http://www.w3schools.com/angular/angular_tables.asp). Es würde Ihnen erlauben, mit wenigen Codezeilen zu arbeiten. – Fredster

Antwort

2

Sie können ein einfaches Ergebnis sehen hier:

var json = '{"status":1,"insert":1,"data":[{"id":"1","name":"Jatin","email":"[email protected]","status":"1"},{"id":"2","name":"Shubham","email":"[email protected]","status":"1"},{"id":"3","name":"Aneh","email":"[email protected]","status":"1"},{"id":"52","name":"php","email":"[email protected]","status":"test"},{"id":"53","name":"faf","email":"as","status":"d"},{"id":"54","name":"taus","email":"alksd","status":"fdasd"},{"id":"55","name":"nake","email":"nakk","status":"naii"},{"id":"56","name":"test1","email":"tausf","status":"dfasd"},{"id":"57","name":"","email":"","status":""},{"id":"58","name":"shubu","email":"[email protected]","status":"12"},{"id":"59","name":"panku","email":"[email protected]","status":"3"},{"id":"60","name":"Jatin","email":"[email protected]","status":"123d"}]}' 
 
json = JSON.parse(json); 
 

 
var tb = $("#tab"); 
 

 
$.each(json.data,function(i,value){ 
 
    tb.append("<tr><td>ID: " + value.id + "</td><td>Name: " + value.name + "</td><td>Email: " + value.email + "</td><td>Status: " + value.status + "</td></tr>"); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 
 
<table id="tab"> 
 
    
 
<table>

Sie können aber verwenden möchten: folgenden Code

$.getJSON(url,function(data){ 
var tb = $("#tab"); 
    $.each(data.data,function(i,value){ 
    tb.append("<tr><td>ID: " + value.id + "</td><td>Name: " + value.name + " </td><td>Email: " + value.email + "</td><td>Status: " + value.status + "</td></tr>"); 
    }); 
}); 
+1

nett +10. Viel Spaß :) –

+0

Ich schätze es wirklich. Danke :) – Roxoradev

+0

Wertschätzung erforderlich, um Menschen zu ermutigen, guten Code zur Verfügung zu stellen. –

2

Versuchen Sie, setzen Sie einfach diesen Code in Ihren Ajax-Erfolg und ersetzen Sie die Daten mit Ihrer Antwort

var data = '{"status":1,"insert":1,"data":[{"id":"1","name":"Jatin","email":"[email protected]","status":"1"},{"id":"2","name":"Shubham","email":"[email protected]","status":"1"},{"id":"3","name":"Aneh","email":"[email protected]","status":"1"},{"id":"52","name":"php","email":"[email protected]","status":"test"},{"id":"53","name":"faf","email":"as","status":"d"},{"id":"54","name":"taus","email":"alksd","status":"fdasd"},{"id":"55","name":"nake","email":"nakk","status":"naii"},{"id":"56","name":"test1","email":"tausf","status":"dfasd"},{"id":"57","name":"","email":"","status":""},{"id":"58","name":"shubu","email":"[email protected]","status":"12"},{"id":"59","name":"panku","email":"[email protected]","status":"3"},{"id":"60","name":"Jatin","email":"[email protected]","status":"123d"}]}'; 
 
var obj = JSON.parse(data); 
 
var tableContent = "<table>"; 
 
tableContent += "<tr><th>ID</th><th>Name</th><th>Email</th><th>Status</th></tr>"; 
 
if(obj.data) { 
 
$.each(obj.data, function(key, value) { 
 
    tableContent += '<tr>'; 
 
    tableContent += '<td>'+value.id+'</td>'; 
 
    tableContent += '<td>'+value.name+'</td>'; 
 
    tableContent += '<td>'+value.email+'</td>'; 
 
    tableContent += '<td>'+value.status+'</td>'; 
 
    tableContent += '</tr>'; 
 
}); 
 
} 
 
tableContent += "</table>"; 
 
$("#responsecontainer").html(tableContent);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 
 

 
<table border="1" align="center"> 
 
    <tr> 
 
     <td> <input type="button" id="display" value="Display All Data" /> </td> 
 
    </tr> 
 
</table> 
 
<div id="responsecontainer" align="center"> 
 
</div>

+0

guten einen. Bitverbesserung möglich. +10 –

+0

Noch ein +1 für mich! Es ist eine gute Antwort, aber es kann verbessert werden. –