【问题标题】:Handsontable - Table not renderedHandsontable - 表未呈现
【发布时间】:2017-09-03 19:13:51
【问题描述】:

我正在尝试在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));
?>

当我点击“Try”按钮时,objectData 更新得很好,但尽管有 hot.render() 指令,表格却没有更新。

知道我做错了什么吗?

Rgds

【问题讨论】:

    标签: php handsontable


    【解决方案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));
    ?>
    

    【讨论】:

      猜你喜欢
      • 2017-07-21
      • 1970-01-01
      • 2015-10-02
      • 2017-06-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-09
      • 1970-01-01
      相关资源
      最近更新 更多