0

我正在使用背景网格。我发现我的一列具有“未定义”值。当列中有未定义时,Backgrid 排序无法正常工作。我查看了源代码。

onClick: 函数 (e) { e.preventDefault();

var columnName = this.column.get("name");

if (this.column.get("sortable")) {
  if (this.direction() === "ascending") {
    this.sort(columnName, "descending", function (left, right) {
      var leftVal = left.get(columnName);
      var rightVal = right.get(columnName);
      if (leftVal === rightVal) {
        return 0;
      }
      else if (leftVal > rightVal) { return -1; }
      return 1;
    });
  }
  else if (this.direction() === "descending") {
    this.sort(columnName, null);
  }
  else {
    this.sort(columnName, "ascending", function (left, right) {
      var leftVal = left.get(columnName);
      var rightVal = right.get(columnName);
      if (leftVal === rightVal) {
        return 0;
      }
      else if (leftVal < rightVal) { return -1; }
      return 1;
    });
  }
}

},

我将代码更改为以下代码并且排序正常(假设 undefined 小于任何值):

onClick: 函数 (e) { e.preventDefault();

var columnName = this.column.get("name");

if (this.column.get("sortable")) {
  if (this.direction() === "ascending") {
    this.sort(columnName, "descending", function (left, right) {
      var leftVal = left.get(columnName);
      var rightVal = right.get(columnName);
      if (leftVal === undefined && rightVal != undefined) {
        return 1;
      }
      if (leftVal != undefined && rightVal === undefined) {
        return -1;
      }
      if (leftVal === rightVal) {
        return 0;
      }
      else if (leftVal > rightVal) { return -1; }
      return 1;
    });
  }
  else if (this.direction() === "descending") {
    this.sort(columnName, null);
  }
  else {
    this.sort(columnName, "ascending", function (left, right) {
      var leftVal = left.get(columnName);
      var rightVal = right.get(columnName);
      if (leftVal === undefined && rightVal != undefined) {
        return -1;
      }
      if (leftVal != undefined && rightVal === undefined) {
        return 1;
      }
      if (leftVal === rightVal) {
        return 0;
      }
      else if (leftVal < rightVal) { return -1; }
      return 1;
    });
  }
}

},

排序时还有其他方法可以处理未定义的值吗?谢谢!

4

1 回答 1

0

实际上,我将我的 backgrid 升级到最新的 0.3.5,它支持 Backgrid.Column 的 sortValue 属性: http ://wyuenho.github.io/backgrid/api/index.html#!/api/Backgrid.Column-cfg-defaults 。我可以定义一个函数来为未定义的列值返回一个空字符串,以使排序正常工作。这将解决我的问题。

于 2014-01-31T23:10:06.767 回答