我正在尝试以手动方式加载数据。
HTML文件非常基本: 只是一个表和一个按钮来加载php脚本发送的数据(名为 actions.php ):
<!doctype html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="dist/handsontable.full.js"></script>
<link rel="stylesheet" media="screen" href="dist/handsontable.full.css">
</head>
<body>
<div id="hot"></div>
<br />
<input id="try" type="button" value="Try" />
</body>
<script>
$(function() {
var objectData = [
{id: 1, name: 'Ted Right', address: ''},
{id: 2, name: 'Frank Honest', address: ''}];
$('#hot').handsontable({
data: objectData,
colHeaders: true,
minSpareRows: 1
});
var hot = $("#hot").handsontable('getInstance');
$("#try").click(function(){
$.getJSON("actions2.php", function(result){
console.log (objectData);
console.log (JSON.parse(result));
hot.render();
});
});
});
</script>
</html>
php也很基本
<?php
$result=array(
array("id" => 5, "name" => "Bill Gates", "address"=>"zzz"),
array("id" => 6, "name" => "Steve Jobs", "address"=>"xxx")
);
echo json_encode(json_encode($result));
?>
当我点击“尝试”按钮时, objectData 已更新,但不是表格,尽管 hot.render()指令。
对我做错了什么的想法?
RGDS
答案 0 :(得分:1)
我找到了问题。
js脚本中缺少.loadData 方法,并且php中有 json_encode 错误
这是一个有效的例子。
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="dist/handsontable.full.js"></script>
<link rel="stylesheet" media="screen" href="dist/handsontable.full.css">
</head>
<body>
<div id="hot" />
<br />
<input id="go" type="button" value="Click me" />
</body>
<script>
$(function() {
var objectData = [
{id: 1, name: 'Ted Right', address: ''},
{id: 2, name: 'Frank Honest', address: ''}];
$('#hot').handsontable({
data: objectData,
colHeaders: true
});
var hot = $("#hot").handsontable('getInstance');
$("#go").click(function(){
$.getJSON("actions2.php", function(result){
hot.loadData(result);
hot.render();
});
});
});
</script>
</html>
和actions2.php文件
<?php
$result=array(
array("id" => 5, "name" => "Bill Gates", "address"=>"zzz","ee"=>"zz"),
array("id" => 6, "name" => "Steve Jobs", "address"=>"xxx")
);
echo (json_encode($result));
?>