0

I have a SlickGrid Table, in which there are compound filters, currently when i try to change the compound filter (lets say from Equal To to Less Than), then it makes an API call. I don't want to make an API call, how do i achieve this?

I searched in slickgrid docs, but couldn't find any property(if it is available).

Image

Akash
  • 25
  • 4
  • You'll have to give us more information. Which SlickGrid from which repo? What data source are you using (local object or AJAX?). Which API and what call specifically are you referring to? A code sample would be helpful. Distil it down to its essentials. – Ben McIntyre Oct 30 '22 at 03:13
  • yes we're missing more context, what code what you tried, what services you use? I can only assume that you use OData or GraphQL but I'm not sure which and it's normal to make an API call because the filter changed, why wouldn't you make an API call? I'm not exactly sure you can block this, I don't think I have code in place to stop the filter callback that calls the API – ghiscoding Oct 31 '22 at 13:47
  • I apologise for the lack of context. So, it's an OData service, and whenever i change filters, by default SlickGrid makes an OData API call. I want to call OData API only when there is some data inside the input box of the filter. – Akash Nov 01 '22 at 18:08
  • ok I see, I can maybe look at skipping backend calls in the Filter Service when the operator is changed without a filled input but only for version 5.x of Angular-Slickgrid. Note also that with OData you have access to the OData query before it's sent to the backend server, you could change the query to your needs (but I don't typically recommend doing that) – ghiscoding Nov 02 '22 at 15:32

1 Answers1

0

Please note that I'm the author of Angular-Slickgrid

So I looked at the problem you're having and it seems like a valid problem to look into, I agree that for some filters like the Compound Date Filter Operator we shouldn't query right away, that is after changing a the operator dropdown without providing a date. So, for that reason I am adding a new grid option skipCompoundOperatorFilterWithNullInput which will avoid triggering a filter change (it will also avoid querying the backend when implemented) when we first change the operator dropdown without providing a date being entered.

Note that this new option will only be available with Angular-Slickgrid v5.1.0+ (via this PR, now supports this and it will only be enabled by default on the Compound Date Filter (any other filters will have to explicitly enable this new flag either via grid option or via a column definition).

What if I cannot upgrade to 5.1.0? Are there any other ways of dealing with this?

Yes, it's just a bit more involving dealing with this though, it however requires a lot more work from your side. The information you need to know is that nearly every piece of code from Angular-Slickgrid and Slickgrid-Universal are protected TypeScript classes and functions which mean that you can simply use TypeScript to extends any of them. Let's take for example the CompoundDateFilter class, we could extend it this way to skip the callback triggering without a date provided (this._currentDate)

import { CompoundDateFilter, OperatorString } from '@slickgrid-universal/common';

export class CustomCompoundDateFilter extends CompoundDateFilter {
  protected onTriggerEvent(e: Event | undefined) {
    if (this._clearFilterTriggered) {
      this.callback(e, { columnDef: this.columnDef, clearFilterTriggered: this._clearFilterTriggered, shouldTriggerQuery: this._shouldTriggerQuery });
      this._filterElm.classList.remove('filled');
    } else {
      const selectedOperator = this._selectOperatorElm.value as OperatorString;
      (this._currentValue) ? this._filterElm.classList.add('filled') : this._filterElm.classList.remove('filled');

      // -- NEW CODE BELOW -- (to skip triggering callback on undefined date)
      // when changing compound operator, we don't want to trigger the filter callback unless the date input is also provided
      if (this._currentDate !== undefined) {
        this.callback(e, { columnDef: this.columnDef, searchTerms: (this._currentValue ? [this._currentValue] : null), operator: selectedOperator || '', shouldTriggerQuery: this._shouldTriggerQuery });
      }
    }

    this._clearFilterTriggered = false;
    this._shouldTriggerQuery = true;
  }
}

then use this new custom filter class in your column definitions

import { CustomCompoundDateFilter } from './custom-compoundDateFilter';

initGrid() {
  this.columnDefinitions = [{
    id: 'start', name: 'Start', field: 'start', 
    filterable: true, filter: { model: CustomCompoundDateFilter },
  }];
}

and there you have it, below is a proof that it is working since I changed the operator and as you can see below this action is no longer resulting in 0 row returned. However if I had done the inverse, which is to input the date but without an operator, it would have execute the filtering because "no operator" is defaulting to the "equal to" operator.

enter image description here

ghiscoding
  • 12,308
  • 6
  • 69
  • 112