Jump to content
JIHAD

JS concatenate string

Recommended Posts

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>

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

ce faci intr-un request AJAX nu trebuie sa aiba legatura cu se intampla dupa codul care face requestu, e cam greu de explicat

Ba 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.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...