I am working on angular 4 project, I had a requirement to drag item, So I was using ng2-dragula plugin to do it for me. Now I need to extract data-id attribute from each row after drop to specific position.
dragulaService.drop.subscribe(
(args) =>{
const [bagName, elSource, bagTarget, bagSource, elTarget] = args;
console.log('after', $(bagSource)); // element after the inserted element
});
In $bagSource variable I will get each rows in new order. Say it be
<tr attributes data-id="23"><tr>
<tr attributes data-id="2"><tr>
<tr attributes data-id="32"><tr>
How could I extract data-id from each tr fields. I need to get each id in their new order in an array. My required result should be [23,2,32];
jquery $(element).attr("data-id") equivalent in angular 4.
Something similiar to
jquery $(element).attr("data-id")
can be achieved with@ViewChild
and some old-fashioned javascript. It would look like:Use this only when
drag zone
andtable
is not related (if it is use @user3263307 answer it's moreAngular-friendly
). However the best option would be make use of[attr.data-id]="someVariable"
, but since I don't know your app structure can't help you with implementing this method..*html File
Simply
#tableRef
is something like template variable which angular will bind with @ViewChild in component file.*.component.ts
Please note that it's a bit brutal method for angular since it's best not to interfere into HTML DOM structure. However as long we are only getting data from elements everything should be fine.
Please note that too about using
@ViewChild
.