0

I am having trouble getting the correct totalRecords value from my collection when performing a search with Backgrid's Client-Side filter extension.

Specifically, when I use the backspace key on my keyboard.

If I do not use the backspace, and type slowly, this seems to work fine:

//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.");
});

It seems like the totalRows skips a collection update(?) when a backspace is fired?

How can I get the current totalRows, when using backspace? Do I need to reset, fetch or refresh the collection? I am unsure. Help?

I simply need the totalRows that are currently displayed in the grid, at any moment.

AussieJoe
  • 1,285
  • 1
  • 14
  • 29

1 Answers1

0

I ended up "altering" the backgrid-filter.js extension.

I altered the search function, like so:

/**
   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);
},

Works well. I do not understand why Backgrid does not have an exposed property for easy access to the current total rows.

AussieJoe
  • 1,285
  • 1
  • 14
  • 29