无法使用自动保存手动将数据保存到单元格

时间:2012-11-13 05:11:47

标签: jquery save undefined handsontable

我在从具有hansontable的单元格中保存数据时遇到了一些问题,并且可以使用一些建议。

问题是:

  1. 自动保存1个单元格发送未定义。
  2. 自动保存多个单元格也未定义。
  3. 点击“保存”按钮,控制台会显示:Save error. POST method is not allowed on GitHub Pages. Run this example on your own server to see the success message
  4. <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 />";
    }
    ?>
    

    已加载数据 enter image description here

    使用未定义保存 enter image description here

1 个答案:

答案 0 :(得分:4)

更改

data: change,

data : {changes: change},

找到它here