Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to disable first row selection #502

Open
kazzkiq opened this issue Aug 17, 2017 · 15 comments
Open

How to disable first row selection #502

kazzkiq opened this issue Aug 17, 2017 · 15 comments

Comments

@kazzkiq
Copy link

kazzkiq commented Aug 17, 2017

By default, in all examples, it seems the first row always starts selected. How can I prevent that, making the selection occur only after an user clicked a row?

@64n35h4
Copy link

64n35h4 commented Aug 18, 2017

Show your code, by default the code doesn't selects the first row..

@kazzkiq
Copy link
Author

kazzkiq commented Aug 18, 2017

I'd say the opposite.

In all examples the first row starts selected (unless you use selectMode: 'multi'). The same behavior replicates to my project.

I couldn't find any clue in the documentation on how to start my grid without selections.

Here is my configuration code:

Show Code
{
  editable: false,
  noDataMessage: 'No data could be found here.',
  actions: {
    add: false,
    edit: false,
    delete: false
  },
  columns: {
    titulo: {
      title: 'Name'
    }
  }
}

@Newan
Copy link

Newan commented Aug 24, 2017

i have the same.

@kazzkiq
Copy link
Author

kazzkiq commented Sep 19, 2017

(Messy) Workaround:

  1. Find data-set.js in your node_modules/ng2-smart-table;
  2. Search for this.willSelect and override it with a 'none' string:
this.willSelect = 'none';

Explanation:
willSelect is what defines if the table will start with first or last row selected. But sadly the option is not exposed to the settings object, which means you can't change its behavior unless you change it directly in the component code.

So as of today you have three options:

  • Edit the code directly in your node_modules (as described above);
  • Clone this repo, submit a PR, and wait (or use your own fork in your project);
  • Copy the entire component into your project, instead of using it via npm install.

@FunnyZ
Copy link

FunnyZ commented Jan 9, 2018

I have the same needs.

@yunier0525
Copy link

I was able to solve the problem of first click with this code made by me:

this.table.grid.dataSet.deselectAll();
this.table.grid.dataSet['rows'][0].isSelected = false;
this.table.grid.dataSet['selectedRow'] = null;

And to auto select the first row and populate the 'selected' class to well style the row:

setTimeout(function () {
   $($(`ng2-smart-table tbody tr`).first()[0]).trigger('click');
});

The two code snippets together solve the problem that the self-selecting row does not have the selected class.

I hope this small contribution will help someone.
Regards.

@NicolaLC
Copy link

Hi to all, I've fixed this bug by my own and I've maded a pull request, if you're interested in.

@TheoCanonne
Copy link

TheoCanonne commented Jul 31, 2020

Hi !
As the pull request of @NicolaLC has not been accepted yet, I've managed to select only the row that the user clicked on.

I use the LocalDataSource to store my data and when it changes ( new data loaded or pagination events ), I set the selected row of the user.

Here is the code I've made :

ngAfterViewInit() {
    this.localDataSource.onChanged().subscribe(() => {
      this.table.grid.dataSet.deselectAll();
      if (this.dataSelected) {
        this.table.grid.dataSet.getRows().map(
          row => {
            if (row.getData() === this.dataSelected) {
              return this.test.grid.dataSet.selectRow(row);
            }
         });
      }
    });
}

Hope it will help ^^

@yim222
Copy link

yim222 commented Oct 25, 2020

Solution: The userRowSelect event supposed to handle it. Worked and solved for me

<ng2-smart-table [settings]="tableSetting" [source]="tableSource" (userRowSelect)="onSelectRow($event)"></ng2-smart-table>

@muysewinkel
Copy link

Solution: The userRowSelect event supposed to handle it. Worked and solved for me

<ng2-smart-table [settings]="tableSetting" [source]="tableSource" (userRowSelect)="onSelectRow($event)"></ng2-smart-table>

This helps not triggering the initial select function. However, row one is still selected and thus will not trigger the userRowSelect function if the first row is clicked.

@wqzyow
Copy link

wqzyow commented Jan 15, 2021

this.table.grid.dataSet.select(-1);

@tuhu95
Copy link

tuhu95 commented Jul 8, 2021

Just add selectedRowIndex: -1 to your table settings like this
tableSettings = { hideSubHeader: true, mode: 'external', selectedRowIndex: -1 };

@Peeripapo
Copy link

Humati's solution is perfect!

@apappas1129
Copy link

apappas1129 commented Dec 28, 2021

Just add selectedRowIndex: -1 to your table settings like this tableSettings = { hideSubHeader: true, mode: 'external', selectedRowIndex: -1 };

Not working for me

I tried this.table.grid.dataSet.deselectAll(); (table is @ViewChild('smartTableComponent') table?: Ng2SmartTableComponent;), but it doesn't do anything.

I figured, for my use case, I do this:

// function deselectRows
this.table.grid.dataSet.deselectAll(); // Maybe this is still needed for component's state?
// class "selected" is not removed by function above, so let's remove it ourselves
const tr = document.querySelector('ng2-smart-table > table > tbody > tr.ng2-smart-row.selected');
tr && tr.classList.remove('selected');

And I had to do it after I load data in the source to make sure tr is already rendered.

void this.source.load(DATA).then(() => this.deselectRows());

@ScotteYx
Copy link

ScotteYx commented Dec 5, 2023

Just add selectedRowIndex: -1 to your table settings like this tableSettings = { hideSubHeader: true, mode: 'external', selectedRowIndex: -1 };

Works perfectly, thanks! Weird it's not in the docs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests