2012-07-17 91 views
2

我無法找到正確的方式在$ jaxery中使用$ .ajax函數將數據插入到名爲 的表中。 有沒有什麼有用的教程或例子呢?handontable:使用ajax插入數據

很多,非常感謝!


感謝您的例子,但我不能夠推着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); 

但它根本不起作用...

感謝您的幫助!

回答

7

下面的例子說明了如何加載和保存使用$就和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] 
    ] 
} 
+0

我在演示頁面上添加了一個正確的Load&Save示例:http://handsontable.com/#example9 - warpech 7月17日12:06 – warpech 2012-09-09 16:12:17

+0

鏈接已死。 [Here's](https://docs.handsontable.com/pro/1.3.4/tutorial-load-and-save.html)文檔中的另一個示例。 – user128216 2016-05-12 17:37:07