2017-04-08 65 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)); 
?>