JIHAD Posted March 23, 2015 Report Posted March 23, 2015 Salut,Incerc sa creez un tabel dinamic, intr-un string, folosind date JSON.Totul merge aproape bine, mai putin datele JSON parsate.Am verificat formatul si daca parsarea se face bine, totul pare ok. Stie cineva de ce nu merge sa le adaug in string-ul content?Am bold-uit in cod ce nu functioneaza.<script> $(".clickable-row").click(function() { var content = ''; content += '<label>List of Rooms:</label>'; content += '<table>'; content += '<thead>'; content += '<tr>'; content += '<th scope=col rowspan=2>No.</th>'; content += '</tr>'; content += '<tr>'; content += '<th>Name</th>'; content += '<th>Permissions</th>'; content += '<th>Description</th>'; content += '</tr>'; content += '</thead>'; content += '<tbody>'; $.ajax({ type: "GET", dataType: "html", url: "index.php", data: "json=true&area=" + $(this).data("href"), success: function(response) { $.each(JSON.parse(response), function(idx, obj) {[U][B] content += '<tr>' + '<th>' + idx + '</th>' + '<td>' + obj.name + '</td>' + '<td>' + obj.permission + '</td>' + '<td>' + obj.description + '</td>' + '</tr>';[/B][/U] //alert( obj.name ); }); } }); content += '</tbody>'; content += '</table>'; $(content).appendTo("#listrooms").enhanceWithin(); });</script> Quote
JIHAD Posted March 23, 2015 Author Report Posted March 23, 2015 am rezolvat.problema era ca afisam rezultatul inainte de parsarea completa.rezultat final:<script> $(".clickable-row").click(function() { $.ajax({ type: "GET", dataType: "html", url: "index.php", data: "json=true&area=" + $(this).data("href"), success: function(response) { var content = ''; content += '<label>List of Rooms:</label>'; content += '<table>'; content += '<thead>'; content += '<tr>'; content += '<th scope=col rowspan=2>No.</th>'; content += '</tr>'; content += '<tr>'; content += '<th>Name</th>'; content += '<th>Permissions</th>'; content += '<th>Description</th>'; content += '</tr>'; content += '</thead>'; content += '<tbody>'; $.each(JSON.parse(response), function(idx, obj) { content += '<tr>'; content += '<th>' + idx + '</th>'; content += '<td>' + obj.name + '</td>'; content += '<td>' + obj.permission + '</td>'; content += '<td>' + obj.description + '</td>'; content += '</tr>'; }); content += '</tbody>'; content += '</table>'; $(content).appendTo("#listrooms").enhanceWithin(); } }); });</script> Quote
Robert1995 Posted March 24, 2015 Report Posted March 24, 2015 Requesturile AJAX sunt async.Citeste asta : javascript - How does AJAX work? - Stack OverflowPe scurt, ce faci intr-un request AJAX nu trebuie sa aiba legatura cu se intampla dupa codul care face requestu, e cam greu de explicat, dar intelegi tu cumva ( exact ce ai facut in varianta 2 a codului e ok ) Quote
AlStar Posted March 24, 2015 Report Posted March 24, 2015 ce faci intr-un request AJAX nu trebuie sa aiba legatura cu se intampla dupa codul care face requestu, e cam greu de explicatBa nu-i greu deloc.Dupa apelarea unei metode asincrone, codul is continua executia astfel ca liniile ce urmeaza nu pot avea acces la valorile ce ar trebui obtinute in urma executiei acelor metode asincrone.Astfel, in cazul in programarii asincrone se folosesc callback-uri - metode ce sunt apelate la finalul executiei. ex. in cazul $.ajax (), dai parametru callback pentru succes si pentru eroare. Quote