Angular 5 DataTables Plugin - How to pass selection data from HTML in TypeScript function call
Angular 5 DataTables Plugin - How to pass selection data from HTML in TypeScript function call
I am using SmartAdmin Angular 5 framework 1.9.1, which provides DataTables capabilities. I have installed DataTables 1.10.18, and Select 1.2.6.
I have the following component.html code, showing only the sa-datatables plugin usage"
<sa-datatable
[options]="{
data: sysMsgs,
columns: [
{data: 'checked'},
{data: 'rowid'},
{data: 'senderID'},
{data: 'message'},
{data: 'dateCreated'}
],
buttons: [ 'copy', 'csv', 'pdf', 'print',
{
extend: 'selected',
text: 'Delete',
action: handleDelete()
}
],
columnDefs: [
{
targets: 0,
orderable: false,
className: 'select-checkbox'
},
{
targets: [2],
visible: true
}
],
select: {
style: 'os',
selector: 'td:first-child'
},
order: [[ 1, 'asc' ]],
searching: true,
search: {
smart: false
}
}"
tableClass="table table-striped table-bordered table-hover">
<thead>
<tr>
<th data-hide="mobile-p">Select</th>
<th data-hide="mobile-p">ID</th>
<th data-hide="mobile-p">Sender</th>
<th data-hide="mobile-p">Message</th>
<th data-hide="mobile-p">Date Sent</th>
</tr>
</thead>
</sa-datatable>
The datatable that is created works fine. I get the checkboxes, and the Delete button does call function handleDelete(). My problem is not knowing how to pass, or get, the selected rows in the handleDelete() function, which I have defined in component.ts.
The DataTables Editor has a Soft Delete example, using jQuery that defines the selected rows like this:
var rows = table.rows( {selected: true} ).indexes();
I have tried modifying the sa-datatable, like so:
<sa-datatable id='table' name='table'
[options]="{
...
action: handleDelete(table.rows( {selected: true} ).indexes())
...
</sa-datatable>
but, it generates a compile error because table is undefined.
Any ideas on how to get a list of selected rows in component.ts?
Thanks,
Bob
This question has an accepted answers - jump to answer
Answers
I'm not familiar with your environment but maybe this doc will help:
https://datatables.net/manual/api#Accessing-the-API
Just replace
table
with$( selector ).DataTable()
if possible in your environment.Kevin
Hi Kevin,
Thanks for the idea. I tried several variants of your suggestion, but no success.
I have added code to my component.ts (notifsysmsg.ts) file, to gain access to the DT plugin component (DatatableComponent). When you break in the handleDelete() function at notifsysmsg.component.ts:68, you can inspect this.ngxDatatable. I am not seeing the selected row(s) when doing this.
If you are interested in looking:
1) Browse to http://tanglemydata.com
2) In left pane, select Notifications. (no login required)
3) Expand "System Messages" to see DT.
4) Select one or more checkboxes.
5) Set breakpoint at notifsysmsg.component.ts:68
6) Click Delete button.
7) Breaks at line 68, inspect local this: NotifSysMsgComponent, ngxDatatable
Do you see the selected row(s)?
Thanks for all your help!
Bob
Someone more familiar with your environment might be able to help more than I can. I wasn't able to find anything with
ngxDatatable
. I was able to use the following to get selected rows:Kevin
Hi Kevin,
I changed the handleDelete() to the following:
But, get error:
How were you able to get the array elements?
Thanks,
Bob
I haven't messed with the Chrome Dev Tools much but I do need to learn since I'm starting new node.js project. In poking around I put that statement in the
Watch
section at the breakpoint.This seems a bit convoluted but it does work without specifying the table ID:
There may be a better way to get the ID without specifying it.
Maybe you need to use this instead:
Kevin
Hi Kevin,
Thanks for all the help in working this out! The technique I am using is the one you developed that uses ngxDatatable. This seems like the best approach for use with the SmartAdmin DataTables plugin - but, by replacing the ngxDatatable with one of your other techniques it should be usable in other configurations.
To tidy this up for other users, that want to know how to process selected rows, I include the code I will be using.
Thanks,
Bob