Ngx-datatable: Why is `selectCheck` bound?

Created on 16 May 2017  路  5Comments  路  Source: swimlane/ngx-datatable

I'm submitting a ... (check one with "x")

[ ] bug report => search github for a similar issue or PR before submitting
[x] feature request
[ ] support request => Please do not submit support request here, post on Stackoverflow or Gitter

Current behavior

Because selectCheck is bound to the selection.component it's pretty difficult to use.

Expected behavior

selectCheck is a documented as a _pure function_ that takes three arguments. There's no reason (obvious to me) that it should be bound (.bind(this)) to anything but the component where I implement it.

What is the motivation / use case for changing the behavior?

for selectCheck to be useful, we need access to fields in the class implementing the table.

// in our class which implements the table:
  selectCheck(row) {
   // compare to what? I cannot reference a field on my class here,
   // because `this` maps to the `DataTableSelectionComponent` instance,
   // which our code knows nothing about. 
  }

There are workarounds, obviously, but since selectCheck seems to be documented as a pure function, and is passed to array.filter anyway, what is the use-case for it being bound to DataTableSelectionComponent

Please tell us about your environment:

Windows, VSCode, npm, webpack + webpackDevServer

  • Table version: 9.0.0

  • Angular version: 4.0.2

  • Browser: [all | Chrome XX | Firefox XX | IE XX | Safari XX | Mobile Chrome XX | Android X.X Web Browser | iOS XX Safari | iOS XX UIWebView | iOS XX WKWebView ]
    all

  • Language: [all | TypeScript X.X | ES6/7 | ES5]
    all
Investigate

Most helpful comment

Still exist in ^16.1.1.
For future devs:

If you need access to your component where you implemented this table, declare your function like this:

public validateSelect = (item: MyTypeOrAny) => { 
    //here you can get access to your functions and vars declared in your component as usual
}

But this way you cannot call to the component functions or vars

  public validateSelect(item:MyTypeOrAny):boolean { 
    //missed context here
}

Extracted from https://github.com/swimlane/ngx-datatable/issues/560

All 5 comments

I tried to implement a workaround for this, but making that return a boolean successfully also causes onSelect to return an empty array, which means I cannot populate the workaround comparison variable. :-1:

Currently running into this issue. Which is very annoying, and was one of the reasons I changed from the last table component I was using. Very disappointed to run into the same issue here.

Seems like if there were a reason in that code to invoke the method with a particular this scope, it could be done with .call or .apply, or if the .bind is being used to return a copy of the function, then it would make more sense to wrap it than bind it.

Seems like a simple enough change though. Maybe one of us should just PR it?

@antangeles?

Is there any workaround available? I'd like to reference field from my class.

Still exist in ^16.1.1.
For future devs:

If you need access to your component where you implemented this table, declare your function like this:

public validateSelect = (item: MyTypeOrAny) => { 
    //here you can get access to your functions and vars declared in your component as usual
}

But this way you cannot call to the component functions or vars

  public validateSelect(item:MyTypeOrAny):boolean { 
    //missed context here
}

Extracted from https://github.com/swimlane/ngx-datatable/issues/560

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Matthi0uw picture Matthi0uw  路  3Comments

IngoManthey picture IngoManthey  路  3Comments

lautarobock picture lautarobock  路  3Comments

ExTheSea picture ExTheSea  路  3Comments

devendraYebhi picture devendraYebhi  路  3Comments