【问题标题】:Handsontable : datas aren't returned to the php fileHandsontable:数据不返回到 php 文件
【发布时间】:2015-08-22 22:15:15
【问题描述】:

我是法国人,请原谅我的英语:) 我检查了很多主题。有些似乎是同样的问题,但它仍然不起作用。我也是 javascript 的初学者。

所以,这是我的代码: `

$(document).ready(function () {

var container = document.getElementById('Grille_competences');

var headerRenderer = function (instance, td, row, col, prop, value,
cellProperties) {
Handsontable.renderers.TextRenderer.apply(this, arguments);
td.style.fontWeight = 'bold';
td.style.textAlign = 'center';
td.style.backgroundColor = '#B0C4DE';
};

//Initialisation des données de la grille.
var data = [["Compétences","Description","Code",""],
        ["", "", "",""],
        ["","", "",""],
        ["","", "",""],
        ["","", "",""],
        ["","", "",""]];

//Création de la grille
hot = new Handsontable(container, {
data: data,
height: 800,
width: 1183,
colWidths: [35,200,25,80],
manualRowResize: true,
colHeaders: true,
rowHeaders: true,
mergeCells: true,
stretchH: 'all',
columnSorting: true,
contextMenu: true,    
contextMenuCopyPaste: {
  swfPath: './zeroclipboard/dist/ZeroClipboard.swf'
},


//Fonctionnalités lors d'un clic droit dans la grille.

 contextMenu: {    

   items: {
    "row_above": {
        name: 'Insérer ligne au dessus',
      disabled: function () {            
        return hot.getSelected()[0] === 0;
      }
    },
    "row_below": {
        name: 'Insérer ligne en dessous',
        disabled: function() {            
        return hot.getSelected()[0] === 0;
    }
  },
    "hsep1": "---------",
    "col_left": {
        name: 'Insérer colonne à gauche',
        disabled: function () {
          return hot.getSelected()[0] === 0;
        }
    },
    "col_right": {
        name: 'Insérer colonne à droite',
        disabled: function() {
          return hot.getSelected()[0] === 0;
        }
    },
    "hsep2": "---------",
    "remove_row": {
      name: 'Supprimer la ligne',
      disabled: function () {
        return hot.getSelected()[0] === 0;
      }
    },

    "remove_col": {
      name: 'Supprimer la colonne',
      disabled: function () {
        return hot.getSelected()[0] === 0;
      }
    },
    "hsep3": "---------",
    "copy": {
      name:'Copier',
      disabled: function () {
        return hot.getSelected()[0] === 0;
      }
    },
    "paste": {
      name: 'Coller',
      disabled: function(){
        return hot.getSelected()[0] === 0;
      }
    },
    "hsep4": "---------",
    "undo": {
      name:'Précédent',
      disabled: function(){
        return hot.getSelected()[0] === 0;
      }
    },
    "redo": {
      name: 'Suivant',
      disabled: function(){
        return hot.getSelected()[0] === 0;
      }
    },
    "hsep5": "---------",
    "make_read_only": {
      name: 'Lecture seule',
      disabled: function() {
        return hot.getSelected()[0] === 0;
      }
    },
    "alignment": {
      name: 'Alignement du texte',
      disabled: function () {
        return hot.getSelected()[0] === 0;        
      }          
    },
    "mergeCells": {
      name: 'Fusionner les cellules',
      disabled: function () {
        return hot.getSelected()[0] === 0;
      }
    },


    },
},


//Entetes de la grille en lecture seule. 
cells: function(row, col, prop) {
var cellProperties = {};
if(row===0){
cellProperties.renderer = headerRenderer;
}   
if(row === 0 && col <3){
       cellProperties.readOnly = true;
       }
return cellProperties;
}
});

//Lors d'un clic sur le bouton valider, transmission des données de la grille.


});

document.getElementById('save').onclick=function () {

            $.ajax({
            url: "testGetData.php",
            dataType: 'json',
            data: {'data':hot.getData()}, //retourne les données des cellules
            type: 'GET',
            success: function (data) {
                alert(data);
            },



        });
            document.location.href='testGetData.php';
    };

`

这里是 testGetData.php 的代码:

<?php

foreach($_GET['data'] as $value)
    echo $value;
?>

问题是 testGetData.php 似乎没有收到 $_GET['data']。 我尝试了许多我在论坛或 Handsontable 的文档上看到的不同的东西。 我以为原因是范围,但我做了和例子一样的事情(至少我相信^^)。

你能帮帮我吗?我不明白出了什么问题。我需要重新审视我的代码。

【问题讨论】:

  • 你测试过hot.getData()是否真的返回了任何数据?应该有一个数组数组。我使用了handsontables,但使用jQuery 连接它,然后在呈现数据以供编辑的容器上使用添加的jQuery 方法handsontable()。在您的情况下,这将类似于:$(container).handsontable('getData')$('#Grille_competences'.handsontable('getData')
  • 很抱歉,但我不知道如何测试 hot.getData() 返回的内容。我测试了data: {'data':$(container).handsontable('getData')}, 并且 href 不再起作用了...
  • 您可以在浏览器上打开页面进行编辑时打开 JavaScript 控制台(在 Firefox 上只需按 k ),然后您可以在命令行中输入 $('#Grille_competences').handsontable('getData')底端。带有&gt;&gt; 提示符的那个。这个命令应该返回提到的数组。
  • 好的,谢谢,使用 hot.getData(),返回一个数组数组。但是在 testGetData.php 文件中仍然被认为是未定义的

标签: javascript jquery ajax handsontable


【解决方案1】:

问题与 hansontable 无关,但与您使用 jQuery ajax 方法发送数据的方式有关:您 指定了 dataType: 'json' 但处理 .php 文件返回纯文本格式。

你应该改变两件事,首先是在你的 .html 文件中:

document.getElementById('save').onclick=function () { ... } 函数替换为:

$('#save').click(function(){
  $.ajax({
    url: "testGetData.php",
    dataType: 'json',
    data: {data: hot.getData() }, 
    type: 'GET',
    success: function (retobj) { alert(JSON.stringify(retobj)); }
  });  
}

(如果你不使用 jQuery 库有什么意义...)里面的东西或多或少没有改变,尽管我稍微改变了success() 函数。它将再次将返回的 JavaScript 对象 retobj 重新格式化回 JSON 格式,因此您将能够看到它的所有元素。 jQuery ajax()method 和 dataType:'json' 的巧妙之处在于,从 .php 脚本返回的数据将作为 JavaScript 对象出现在 success() 函数中,而无需您执行任何操作!

其次 - 最重要的事情 - 在您的 .php 文件中:

为了向success() 函数提供所需的json 格式字符串,testGetData.php 脚本需要json 格式返回一些数据。最简单的方法是使用 php 函数json_encode()

<?php
echo json_encode($_REQUEST);
?>

假设hot.getData() 传递了一个这样的数组:

data= [[0,1,2,3],
       [100,101,102,103],
       [200,201,202,203]];

然后在单击#save-按钮后,警报函数将返回以下字符串(我对其进行了测试-虽然没有动手操作):

{"data":[["0","1","2","3"],["100","101","102","103"],["200","201","202","203"]]}

【讨论】:

  • 非常感谢!我不是很明白json的含义
  • 而且我不知道您实际上可以发送任何数据对象(在本例中为数组数组)到 URL 使用 GET 方法。我一直认为这只能使用POST 方法! ;-)
猜你喜欢
  • 1970-01-01
  • 2013-10-29
  • 1970-01-01
  • 2018-03-03
  • 2016-09-08
  • 2016-03-12
  • 1970-01-01
  • 2017-05-10
  • 2011-07-19
相关资源
最近更新 更多