我有以下数据表
<br><button id="addRow">Add New Row</button><br>
<table class="table table-striped table-bordered table-hover " id="example" cellSpacing=0 width="100%">
<thead>
<tr>
<th>Image</th>
<th>number</th>
<th>Creation_date</th>
<th>ei</th>
<th>name</th>
<th>profit</th>
</tr>
</thead>
<?php
foreach ( $data_req_table as $k => $v ) :
?>
<tr style="text-align: center;">
<td>
<img width="100px"
data-original="image.jpg"
src="image.jpg"
/>
</td>
<td><? echo $v['number']; ?></td>
<td><? echo $v['creationdate']; ?></td>
<td><? echo $v['ei']; ?></td>
<td><? echo $v['name']; ?></td>
<td><? echo $v['profit']; ?></td>
</tr>
<?php
endforeach;
?>
</table>
我通过Jquery - Ajax脚本添加行。以下:
<script type="text/javascript">
$(document).ready(function () {
debugger;
var t = $('#example').DataTable({
"searching": true,
//"paging": true,
"order": [[ 2, "desc" ]],
aLengthMenu: [
[25, 50, 100, 200, -1],
[25, 50, 100, 200, "All"]
], iDisplayLength: -1
});
var counter = 1;
$('#addRow').on('click', function ciicici() {
var now = new Date();
var now = now.toMysqlFormat();
var tii = new Date();
tii.setSeconds(tii.getSeconds() - 15);
var tii = tii.toMysqlFormat();
//alert(tii);
$.post( "sql.php", { timing: now,seconding: tii })//.done({});
.done(function( data ) {
var dataParts = data.split("##")
var lenghtylenght = dataParts.length;
for (i = 0; i <= lenghtylenght; i++) {
var dataPart = dataParts[i];
var splitData = dataPart.split("///");
t.row.add([
splitData[0],
splitData[1],
splitData[2],
splitData[3],
splitData[4],
splitData[5]
]).draw();
alert('done');
}
counter++;
});
//setTimeout(function(){ciicici();}, 15000);
});
$('#addRow').click();
});
</script>
和php正在
$ippi = 0;
foreach ( $data_rreq as $k => $v ) : //data_rreq is the pdo fetched array
$ippi++;
echo "<td><img width='100px' src='image.jpg' /></td>".'///';
echo '<td>'.$v['number'].'</td>'.'///';
echo '<td>'.$v['creationdate'].'</td>'.'///';
echo '<td>'.$v['ei'].'</td>'.'///';
echo '<td>'.$v['name'].'</td>'.'///';
echo '<td>'.$v['profit'].'</td>';
if($ippi < $len){
echo "##";
}
endforeach;
代码有效,但我有两个问题:
1)数据表问题,每次自动添加一行时,我都会收到错误:请求的未知参数&#39; 1&#39;排......它看起来像循环太多,它没有找到任何结果,它有一个错误。我的数据表参考如下:
<script src="http://cdnjs.cloudflare.com/ajax/libs/datatables/1.10.0/js/jquery.dataTables.min.js"></script>
2)然后我想问一下是否有办法避免使用split函数将5个不同的参数输入到请求中,使用可能是某种json编码的数组。
Markoc酒店
答案 0 :(得分:1)
我认为你的循环需要修补:
for (i = 0; i <= lenghtylenght; i++)
替换为:
for (i = 0; i < lenghtylenght; i++)
否则你会超出索引。