Ngx-datatable: Possibility to select rows by property

Created on 27 Jan 2017  路  2Comments  路  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

Current behavior

The list in selected is a list of objects. This makes it hard to preselect based on a property of the objects.

Expected behavior

When setting trackByProp and/or rowIdentity I was expecting that the list selected rows/objects is based on that property. Instead I still get the complete object.

Reproduction of the problem

I currently have the following code in ngOnInit which works with a purely angular-based table to highlight a selected object:

  ngOnInit() {
    this.getCalculations();
    this._route.params
      .subscribe((params: Params) => this.selectedId = params['id']);
    this.columns = [
      { prop: 'test' },
      { prop: 'structure' },
      { prop: 'code' },
    ];
  }

This makes it possible to call the route with an additional parameter like myapp/calculations;id=c2a06883-970e-4731-9844-b8f381d0eb29 to get the respective row highlighted (in pure angular a simple case of *ngIf).

Using ngx-datatable, I now need the following additional code in getCalculations to (pre-)select the row:

    this._calculationsService.getCalculations().subscribe(
      calculations => {
        this.calculations = calculations;
        this.selected.push(this.calculations.find(calc => calc.id == this.selectedId));
      },
      error => this.errorMessage = <any>error,
    );

Which works when loading the app. But when switching between the views, the service caches the table, so when I return, the getCalculations seems to finish before route.params.subscribe is triggered and therefore nothing is selected.

To remedy this I would have to add another logic in the callback for the router parameter subscription to fill up the selected list or synchronize the selected object directly between the components (which then does not show up in the url, which I consider a feature).

Investigate

Most helpful comment

Yes, the selection code is a bit of a mess. I'm considering breaking it out into its own sub-component that users can define as part of the markup.

All 2 comments

This is part of a bigger issue--if you make changes to the selected binding after the component is created, the changes aren't reflected. It would be helpful if the selected property could be changed programmatically instead of just by user interaction.

Yes, the selection code is a bit of a mess. I'm considering breaking it out into its own sub-component that users can define as part of the markup.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

IngoManthey picture IngoManthey  路  3Comments

dinvlad picture dinvlad  路  3Comments

ChanBen picture ChanBen  路  3Comments

Csirme picture Csirme  路  3Comments

devendraYebhi picture devendraYebhi  路  3Comments