首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >手持式.刷新数据和维护过滤器和排序条件

问手持式.刷新数据和维护过滤器和排序条件
EN

Stack Overflow用户
提问于 2017-06-02 01:47:29
回答 1查看 1.9K关注 0票数 1

我使用handsontable作为位于use服务器上的MySQL数据库的前端。用户对前端手柄上的数据进行过滤、排序、修改等操作。

由于各种原因,后端MySQL数据库中发生了更改。我需要把这些信息同步回用户前端。

我可以很容易地使用HTTP请求从数据库重新下载数据,并使用下面的代码片段将这些数据写入到handsontable。

代码语言:javascript
复制
$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??
  });

问题是,此代码清除了所有用户当前的排序和筛选条件。当可处理的数据对象被更新时,我想保存这些数据。有人知道我是怎么做到的吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-08-03 08:13:47

我设法解决了这个问题。每个方面的解决办法如下:

滤波器

在更新数据设置时,将Handsontable的版本更新为v1.11.0版本时会维护过滤器。

排序

更新上述方法如下:

代码语言:javascript
复制
$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;
}

希望这能帮助有类似问题的人。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44319607

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档