如何从Backgrid客户端搜索获取totalRows

时间:2016-03-31 15:07:56

标签: javascript backbone.js client-side backspace backgrid

使用Backgrid的客户端过滤器扩展程序执行搜索时,无法从我的集合中获取正确的totalRecords值。

具体来说,当我在键盘上使用退格键时。

如果我不使用退格键并慢慢输入,这似乎工作正常:

//Search input field - keyup event
$("input[name='q']").keyup(function () {

    console.log('searchcontainer input - keyup event');
    console.log($(this).val())
    console.log($(this).val().length)

    var key = event.keyCode || event.charCode;
    if (key == 8) { //'8' == backspace key 
        console.log('backspace was keyed!')

        //how do I refresh the 'totalRecords' property on the collection?
    }

    console.log((_myCollection.state.totalRecords || "0") + " records found."));

    $("#lblRecordsFound").text((_myCollection.state.totalRecords || "0") + " records found.");
});

看起来,当触发退格时,totalRows会跳过集合更新(?)?

如何在使用退格键时获取当前的totalRows?我是否需要重置,获取或刷新集合?我不确定。帮助

我只需要随时在网格中显示的totalRows。

1 个答案:

答案 0 :(得分:0)

我最终"改变" backgrid-filter.js扩展名。

我改变了搜索功能,如下所示:

/**
   Takes the query from the search box, constructs a matcher with it and
   loops through collection looking for matches. Reset the given collection
   when all the matches have been found.

   If the collection is a PageableCollection, searching will go back to the
   first page.
*/
search: function () {
  var matcher = _.bind(this.makeMatcher(this.query()), this);
  var col = this.collection;
  if (col.pageableCollection) col.pageableCollection.getFirstPage({ silent: true });
  col.reset(this.shadowCollection.filter(matcher), { reindex: false });
  var message = " items found.";
  if (col.pageableCollection.state.totalRecords == 1) {
      message = " item found.";
  }
  $("#lblRecordsFound").text((col.pageableCollection.state.totalRecords || "0") + message);
},

运作良好。我不明白为什么Backgrid没有公开的属性来轻松访问当前的总行。