【问题标题】:How to get totalRows from Backgrid Client-Side search如何从 Backgrid 客户端搜索中获取 totalRows
【发布时间】:2016-03-31 15:07:55
【问题描述】:

在使用 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。

【问题讨论】:

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


    【解决方案1】:

    我最终“更改”了 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 没有公开的属性以便于访问当前的总行数。

    【讨论】:

      猜你喜欢
      • 2020-06-25
      • 2011-05-28
      • 1970-01-01
      • 2019-05-08
      • 2016-08-31
      • 1970-01-01
      • 2011-11-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多