带有未定义值的backgrid排序



我正在使用backgrid。我发现我的一列之一具有"未定义"的价值。当列中有未定义时,backgrid排序无法正常工作。我研究了源代码。

onclick:function(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;
    });
  }
}

},

我将代码更改为以下,并且排序正确起作用(假设未定义小于任何值):

onclick:function(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;
    });
  }
}

},

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

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

相关内容

  • 没有找到相关文章

最新更新