我对knockout js很陌生,我正在尝试使用php ajax创建一个数据来自数据库的表。数据绑定不使用ajax php结果,而如果我在同一个页面中创建一个具有数据绑定变量的表,则它将使用表。如何我能让ajax结果与knockout一起工作吗。谢谢你的帮助
数据加载php:
include("db.php");
$result=mysqli_query($conn,"select * from codes");
$par='\$parent';
echo '<table class="table table-striped box" >
<thead>
<tr>
<td align=center> <b>Code</b></td>
<td align=center><b>Starting Date</b></td>
<td align=center><b>Closing Date</b></td>
<td align=center><b>Select</b></td></tr></thead>
';
echo '<tbody data-bind="foreach:{data:rows,as:\'Row\'}">';
echo "<tr>
<td data-bind='text:Row.codes'></td>
<td data-bind='text:Row.sDate'></td>
<td data-bind='text:Row.eDate'></td>
<td align=center><button data-bind='click:$par.selectRow' class='btn btn-orange'>Select</button></td>
</tr>";
while($data = mysqli_fetch_row($result))
{
echo "<tr >";
echo "<td align=center data-bind='text:Row.codes'>$data[1]</td>";
echo "<td align=center data-bind='text:Row.sDate'>$data[2]</td>";
echo "<td align=center data-bind='text:Row.eDate'>$data[3]</td>";
echo "<td align=center><button data-bind='click:$par.selectRow' class='btn btn-orange addd'>Select</button></td>";
echo "</tr>";
}
echo "</tbody></table>";
以及我的ajax代码:
$(document).ready(function(){
$.ajax({
type:"POST",
url:"load.php",
dataType:"html",
success: function(response){
$("#codetable").html(response);
}
});
});
还有我的击倒剧本
$(document).ready(function(){
function Row(codes,sDate,eDate){
this.codes=codes;
this.sDate=sDate;
this.eDate=eDate;
}
var myRowViewModel = new function(){
var current= this;
this.SelectedRow = ko.observable(null);
this.rows=ko.observableArray([
new Row("45sd9e","10-08-2018",1000),
new Row("45sd9e","10-08-2018",1000),
]);
this.selectRow = function(Row){
current.SelectedRow(Row)
alert(Row.codes);
}
}
ko.applyBindings(myRowViewModel);
});