我在使用 hansontable 从单元格保存数据时遇到了一些问题,可以使用一些建议。
问题是:
- 自动保存 1 个单元格发送未定义。
- 自动保存多个单元格也未定义。
- 单击保存按钮时,控制台显示:
Save error. POST method is not allowed on GitHub Pages. Run this example on your own server to see the success message
代码:
<script>
var $container = $("#excel");
var $console = $("#excelconsole");
var $parent = $container.parent();
var autosaveNotification;
$container.handsontable({
startRows: 1,
startCols: <?=$num_rName+1?>,//count room type name from db
rowHeaders: true,
//colHeaders: ['Date/Room Types', 'Year', 'Price'],
colHeaders: <?echo json_encode($_array);?>,
minSpareCols: 0,
minSpareRows: 1,
contextMenu: true,
onChange: function (change, source) {
if (source === 'loadData') {
return; //don't save this change
}
if ($parent.find('input[name=autosave]').is(':checked')) {
clearTimeout(autosaveNotification);
$.ajax({
url: "inc/excel_save.php",
dataType: 'json',
type: "POST",
data: change, //contains changed cells' data
complete: function (data) {
$console.text('Autosaved (' + change.length + ' cell' + (change.length > 1 ? 's' : '') + ')');
autosaveNotification = setTimeout(function () {
$console.text('Changes will be autosaved');
}, 1000);
}
});
}
}
});
var handsontable = $container.data('handsontable');
$parent.find('button[name=load]').click(function () {
$.ajax({
url: "inc/excel_load.php",
dataType: 'json',
type: 'GET',
success: function (res) {
handsontable.loadData(res.data);
$console.text('Data loaded');
}
});
});
$parent.find('button[name=save]').click(function () {
$.ajax({
url: "inc/excel_save.php",
data: {"data": $("#excel").handsontable('getData')},
dataType: 'json',
type: 'POST',
success: function (res) {
if (res.result === 'ok') {
$console.text('Data saved');
}
else {
$console.text('Save error');
}
},
error: function () {
$console.text('Save error. POST method is not allowed on GitHub Pages. Run this example on your own server to see the success message.');
}
});
});
$parent.find('input[name=autosave]').click(function () {
if ($(this).is(':checked')) {
$console.text('Changes will be autosaved');
}
else {
$console.text('Changes will not be autosaved');
}
});
</script>
excel_save.php
<?
foreach($_POST as $key=>$val){//test the var passed
echo "$key=$val<br />";
}
?>
数据加载
保存未定义