2

jqueryで$.ajax関数を使用してhandsontableテーブルにデータを挿入する正しい方法が見つかりません。これに関する役立つチュートリアルや例はありますか?

大変感謝します!!


例をありがとう、しかし私はPHP配列をjson経由で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);

しかし、それはまったく機能しません...

助けてくれてありがとう!

4

1 に答える 1

7

以下の例は、$.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);
  }
});

上記のコードは、<div id="example6grid" class="dataTable"></div>が存在し、そのファイルsource.jsonに次の JSON が含まれていることを前提としています。

{
  "data": [
    ["", "Kia", "Nissan", "Toyota", "Honda"],
    ["2008", 10, 11, 12, 13],
    ["2009", 20, 11, 14, 13],
    ["2010", 30, 15, 12, 13]
  ]
}
于 2012-07-17T10:49:14.173 に答える