Backgrid.JS:如何将编辑推送到服务器作为 POST 请求 PHP
Backgrid.JS: How to push edits to server as POST requests to PHP
我有一个 editabletable。下面是一个呈现 table 的函数,给定要呈现 table 的元素 ID、列数组和用于构建 URL 的控制器名称。 URL 格式如下,其中 exampleController 是一个示例控制器:
- 要更新:php/gateway.php?sender=exampleController&action=update
- 列表:php/gateway.php?sender=exampleController&action=list
我希望 POST 请求包含编辑后的信息以及相应的行。
我尝试在下面的代码中做(某事)。它发出请求,但不包括 POST。
var initiateGrid = function(element_id, columns, controllerName) {
var gridModel = Backbone.Model.extend({
initialize: function() {
Backbone.Model.prototype.initialize.apply(this, arguments);
this.on("change", function(model, options) {
if (options && options.save === false) {
return;
}
this.sync();
});
},
sync: function(method, model, options) {
var methodUpdateMethodUrl = 'php/gateway.php?sender=' + controllerName + '&action=update';
options = options || {};
options.url = methodUpdateMethodUrl;
Backbone.sync(method, model, options);
}
});
var gridCollectionModel = Backbone.Collection.extend({
model: gridModel,
url: 'php/gateway.php?sender=' + controllerName + '&action=list',
});
var gridCollection = new gridCollectionModel();
var grid = new Backgrid.Grid({
columns: columns,
collection: gridCollection,
emptyText: "No Data"
});
var gridContainer = $("#" + element_id);
gridContainer.append(grid.render().el);
gridCollection.fetch({
reset: true
});
};
当然还有这个错误:
Uncaught TypeError: Cannot read property 'trigger' of undefined
尝试改变url
url:function() {
return 'php/gateway.php?sender=' + controllerName + '&action=list',
}
以下代码对我有用:
var initiateGrid = function(element_id, columns, controllerName) {
var gridModel = Backbone.Model.extend({
initialize: function() {
Backbone.Model.prototype.initialize.apply(this, arguments);
this.on("change", function(model, options) {
this.sync("update", model, options);
});
},
sync: function(method, model, options) {
var methodUrl = {
'update': 'php/gateway.php?sender=' + controllerName + '&action=update'
};
console.log(method, model, options);
if (methodUrl && methodUrl[method.toLowerCase()]) {
options = options || {};
options.url = methodUrl[method.toLowerCase()];
}
Backbone.sync(method, model, options);
}
});
var gridCollectionModel = Backbone.Collection.extend({
model: gridModel,
url: 'php/gateway.php?sender=' + controllerName + '&action=list',
});
var gridCollection = new gridCollectionModel();
var grid = new Backgrid.Grid({
columns: columns,
collection: gridCollection,
emptyText: "No Data"
});
var gridContainer = $("#" + element_id);
gridContainer.append(grid.render().el);
gridCollection.fetch({
reset: true
});
};
然后 PHP 得到一个数组:
function get_form_data() {
$dump = json_decode(file_get_contents('php://input'));
$transfer_info = array();
foreach ($dump as $key => $value) {
$transfer_info[$key] = $value;
}
return $transfer_info;
}
我有一个 editabletable。下面是一个呈现 table 的函数,给定要呈现 table 的元素 ID、列数组和用于构建 URL 的控制器名称。 URL 格式如下,其中 exampleController 是一个示例控制器:
- 要更新:php/gateway.php?sender=exampleController&action=update
- 列表:php/gateway.php?sender=exampleController&action=list
我希望 POST 请求包含编辑后的信息以及相应的行。
我尝试在下面的代码中做(某事)。它发出请求,但不包括 POST。
var initiateGrid = function(element_id, columns, controllerName) {
var gridModel = Backbone.Model.extend({
initialize: function() {
Backbone.Model.prototype.initialize.apply(this, arguments);
this.on("change", function(model, options) {
if (options && options.save === false) {
return;
}
this.sync();
});
},
sync: function(method, model, options) {
var methodUpdateMethodUrl = 'php/gateway.php?sender=' + controllerName + '&action=update';
options = options || {};
options.url = methodUpdateMethodUrl;
Backbone.sync(method, model, options);
}
});
var gridCollectionModel = Backbone.Collection.extend({
model: gridModel,
url: 'php/gateway.php?sender=' + controllerName + '&action=list',
});
var gridCollection = new gridCollectionModel();
var grid = new Backgrid.Grid({
columns: columns,
collection: gridCollection,
emptyText: "No Data"
});
var gridContainer = $("#" + element_id);
gridContainer.append(grid.render().el);
gridCollection.fetch({
reset: true
});
};
当然还有这个错误:
Uncaught TypeError: Cannot read property 'trigger' of undefined
尝试改变url
url:function() {
return 'php/gateway.php?sender=' + controllerName + '&action=list',
}
以下代码对我有用:
var initiateGrid = function(element_id, columns, controllerName) {
var gridModel = Backbone.Model.extend({
initialize: function() {
Backbone.Model.prototype.initialize.apply(this, arguments);
this.on("change", function(model, options) {
this.sync("update", model, options);
});
},
sync: function(method, model, options) {
var methodUrl = {
'update': 'php/gateway.php?sender=' + controllerName + '&action=update'
};
console.log(method, model, options);
if (methodUrl && methodUrl[method.toLowerCase()]) {
options = options || {};
options.url = methodUrl[method.toLowerCase()];
}
Backbone.sync(method, model, options);
}
});
var gridCollectionModel = Backbone.Collection.extend({
model: gridModel,
url: 'php/gateway.php?sender=' + controllerName + '&action=list',
});
var gridCollection = new gridCollectionModel();
var grid = new Backgrid.Grid({
columns: columns,
collection: gridCollection,
emptyText: "No Data"
});
var gridContainer = $("#" + element_id);
gridContainer.append(grid.render().el);
gridCollection.fetch({
reset: true
});
};
然后 PHP 得到一个数组:
function get_form_data() {
$dump = json_decode(file_get_contents('php://input'));
$transfer_info = array();
foreach ($dump as $key => $value) {
$transfer_info[$key] = $value;
}
return $transfer_info;
}