次のような JSON を返す関数が PHP にあります。
{"tableRow":[ {"tipo":"Noche"},{"patente":"XZ7410"},{"nombre":"Marcela Bien"},
{"revisado":0},{"id_registro":"10"},
{"tipo":"Vespertino"},{"patente":"EW3651"},{"nombre":"Alexis Diaz"},
{"revisado":1},{"id_registro":"9"} ]
}
また、デフォルトで作成されたいくつかの HTML コンテンツを含むこのテーブルを用意します。
<table class="table table-condensed table-striped table-hover table-bordered pull-left" id="data-table">
<thead>
<tr>
<th>Turno</th>
<th>ID Máquina</th>
<th>Operador</th>
<th>Semáforo</th>
<th> </th>
</tr>
</thead>
<tbody id="data-update">
<tr>
<td>Noche</td>
<td>XZ7410</td>
<td>Marcela Bien</td>
<td><img src="/monitor/web/images/bullet_red.png" /></td>
<td><a class="btn btn-success btn-mini" href="/monitor/web/frontend_dev.php/ver-detalles/10">Ver detalles</a></td>
</tr>
<tr>
<td>Vespertino</td>
<td>EW3651</td>
<td>Alexis Diaz</td>
<td><img src="/monitor/web/images/bullet_orange.png" /></td>
<td><a class="btn btn-success btn-mini" href="/monitor/web/frontend_dev.php/ver-detalles/9">Ver detalles</a></td>
</tr>
</tbody>
</table>
#data-update HTML コンテンツを PHP から生成されたもので更新し、JSON として jQuery に渡したいので、次のコードを書きました。
$(document).ready(function() {
setInterval(function() {
$.ajax({
type: 'get',
url: '<?php echo url_for('dashboard/BuildAjax') ?>',
dataType: 'json',
success: function(data) {
var newRows;
for (var i in data.tableRow) {
newRows += "<tr><td>" + data.tableRow[i].tipo + "</td>"
newRows += "<td>" + data.tableRow[i].patente + "</td>"
newRows += "<td>" + data.tableRow[i].nombre + "</td>"
newRows += "<td>" + data.tableRow[i].revisado + "</td>"
newRows += "<td>" + data.tableRow[i].id_registro + "</td></tr>"
}
$("#data-update").html(newRows);
},
error: function() {
alert('<?php echo __('Error al cargar los datos') ?>');
}
});
}, 10000);
});
HTML コンテンツは変更されていますが、正しい方法ではありません。どういう意味?結果として、次のようなテーブルを取得しています。
Turno ID Maquina Operador Semaforo
Noche undefined undefined undefined
undefined XZ7410 undefined undefined
undefined undefined Marcela Bien undefined
undefined undefined undefined 1
undefined undefined undefined undefined 10
Vespertino undefined undefined undefined
undefined EW3651 undefined undefined
undefined undefined Alexis Diaz undefined
undefined undefined undefined 0
undefined undefined undefined undefined 12
いつ次のようなものを取得する必要があります:
Turno ID Maquina Operador Semaforo
Noche XZ7410 Marcela Bien 1 10
Vespertino EW3651 Alexis Diaz 0 12
なにが問題ですか?どこで間違いを犯したのかわかりません。ヘルプやアドバイスはありますか?