2017-04-08 81 views
1

我正试图在handontable中加载数据。Handsontable - 表未呈现

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)); 
?> 

当我点击“试用”按钮,在目标数据很好更新,但并不是不顾hot.render表()指令。

有关我在做什么错的任何想法?

Rgds

回答

1

我发现了问题。

.loadData方法缺少的JS脚本,有一个json_encode错误在PHP

这里是一个工作示例。

<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)); 
?>