【问题标题】:handsontable: inserting data using ajax [closed]handsontable:使用 ajax 插入数据 [关闭]
【发布时间】:2012-07-16 01:31:00
【问题描述】:

我找不到在handsontable 中插入数据的正确方法 使用带有 jquery 的 $.ajax 函数的表。 有什么有用的教程或例子吗?

非常感谢!!


感谢您的示例,但我无法通过 json 将 PHP 数组推送到 handsontable (使用 PHP::json_encode())。 试了很多次,最后还是不行……

例如:我得到了一些带有行的数组:

<?php
        $row1 = array(1=>"value1",
        2=>"value2",
        3=>"value3",
        4=>"value4",
        5=>"value5");

    $row2 = array(1=>"value1",
        2=>"value2",
        3=>"value3",
        4=>"value4",
        5=>"value5");

    $row3 = .......

所以我尝试了:

$data = array($row1,$row2,$row...);
echo json_encode($data);

但它根本不起作用......

感谢您的帮助!

【问题讨论】:

    标签: jquery json handsontable


    【解决方案1】:

    以下示例展示了如何使用 $.ajax 和 Handsontable 加载和保存数据:

    var first = true;
    $("#example6grid").handsontable({
      rows: 8,
      cols: 8,
      rowHeaders: true,
      colHeaders: true,
      minSpareCols: 1,
      minSpareRows: 1,
      contextMenu: true,
      onChange: function (change) {
        if (first) {
          first = false;
          return; //don't save this change
        }
        $.ajax({ //saves changes from Handsontable
          url: "save.php",
          dataType: "json",
          type: "POST",
          data: {"data": $("#example6grid").handsontable('getData')}, //returns full array of grid data
          //data: change, //contains only information about changed cells
          success: function (data) {
            console.log("saved", data);
          },
          error: function (data) {
            console.log("error", data);
          }
        });
      }
    });
    
    $.ajax({ //loads data to Handsontable
      url: 'source.json',
      dataType: 'json',
      type: 'GET',
      success: function(res){
        $("#example6grid").handsontable("loadData", res.data);
      }
    });
    

    以上代码假定&lt;div id="example6grid" class="dataTable"&gt;&lt;/div&gt; 存在,并且该文件source.json 包含以下JSON:

    {
      "data": [
        ["", "Kia", "Nissan", "Toyota", "Honda"],
        ["2008", 10, 11, 12, 13],
        ["2009", 20, 11, 14, 13],
        ["2010", 30, 15, 12, 13]
      ]
    }
    

    【讨论】:

    • 我在演示页面中添加了一个适当的加载和保存示例:handsontable.com/#example9 – warpech Jul 17 at 12:06
    • 链接已失效。 Here's 文档中的另一个示例。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-03-21
    • 1970-01-01
    • 1970-01-01
    • 2012-05-04
    • 2014-10-06
    • 2021-04-29
    • 2018-08-11
    相关资源
    最近更新 更多