我使用handsontable作为位于use服务器上的MySQL数据库的前端。用户对前端手柄上的数据进行过滤、排序、修改等操作。
由于各种原因,后端MySQL数据库中发生了更改。我需要把这些信息同步回用户前端。
我可以很容易地使用HTTP请求从数据库重新下载数据,并使用下面的代码片段将这些数据写入到handsontable。
$http({
method: "POST",
url: "my-data-url",
data: "my-post-data"
}).then(function(response){
// response contains the handsontable data
var json_response = response.data;
// Update the data in my table instance
hot.updateSettings({
data: json_response
})
// hot looses its sort and filter conditions...
// how can I maintain these or re-apply them??
});问题是,此代码清除了所有用户当前的排序和筛选条件。当可处理的数据对象被更新时,我想保存这些数据。有人知道我是怎么做到的吗?
发布于 2017-08-03 08:13:47
我设法解决了这个问题。每个方面的解决办法如下:
滤波器
在更新数据设置时,将Handsontable的版本更新为v1.11.0版本时会维护过滤器。
排序
更新上述方法如下:
$http({
method: "POST",
url: "my-data-url",
data: "my-post-data"
}).then(function(response){
// response contains the handsontable data
var json_response = response.data;
// Get sort data of handsontable prior to updating the data setting
if (isSorted(hot) == true) {
var sortData = getSortData(hot)
}
// Update the data in my table instance
hot.updateSettings({
data: json_response
})
// Re-apply previous sorts
if (sortData) {
hot.sort(sortData.sortColumn, sortData.sortOrder);
}
});
// Determines if sorts have been applied
var isSorted = function (hot_instance) {
return hot_instance.sortingEnabled && typeof hot_instance.sortColumn != 'undefined' && typeof hot_instance.sortOrder != 'undefined';
}
// gets the sort data for a handsontable passed
var getSortData = function (hot_instance) {
var sortData = [];
// Get sorted column number
var columnNo = hot_instance.sortColumn;
// Get current sort order of column
// false = desceding order
var sortOrder = hot_instance.sortOrder;
sortData.sortColumn = columnNo;
sortData.sortOrder = sortOrder;
return sortData;
}希望这能帮助有类似问题的人。
https://stackoverflow.com/questions/44319607
复制相似问题