Deleting entire row angular 2 material?
JSON
[
{ position: 1, name: 'test', value: 1.0079, symbol: 'HHH' },
{ position: 2, name: 'test2', value: 4.0026, symbol: 'BBB' },
{ position: 3, name: 'test3', value: 6.941, symbol: 'BB' },
{ position: 4, name: 'test4', value: 9.0122, symbol: 'CC' },
]
TS
isAllSelected() {
const numSelected = this.selection.selected.length;
const numRows = this.dataSource.data.length;
return numSelected === numRows;
}
removeSelectedRows() {
this.selection.selected.forEach(item => {
let index: number = this.data.findIndex(d => d === item);
console.log(this.data.findIndex(d => d === item));
this.dataSource.data.splice(index,1);
this.dataSource = new MatTableDataSource<Element>(this.dataSource.data);
setTimeout(() => {
this.dataSource.paginator = this.paginator;
});
});
this.selection = new SelectionModel<Element>(true, );
}
/** Selects all rows if they are not all selected; otherwise clear selection. */
masterToggle() {
this.isAllSelected() ?
this.selection.clear() :
this.dataSource.data.forEach(row => this.selection.select(row));
}
}
HTML
<div class="example-container mat-elevation-z8">
<mat-table #table [dataSource]="dataSource">
<!-- Checkbox Column -->
<ng-container matColumnDef="actions">
<mat-header-cell *matHeaderCellDef>
</mat-header-cell>
<mat-cell *matCellDef="let row">
<button mat-icon-button color="#b71c1c">
<mat-icon aria-label="Delete">delete</mat-icon>
</button>
</mat-cell>
</ng-container>
<!-- Position Column -->
<ng-container matColumnDef="position">
<mat-header-cell *matHeaderCellDef> No. </mat-header-cell>
<mat-cell *matCellDef="let element"> {{element.position}} </mat-cell>
</ng-container>
<!-- Weight Column -->
<ng-container matColumnDef="value">
<mat-header-cell *matHeaderCellDef> value </mat-header-cell>
<mat-cell *matCellDef="let element"> {{element.value}} </mat-cell>
</ng-container>
<!-- Symbol Column -->
<ng-container matColumnDef="symbol">
<mat-header-cell *matHeaderCellDef> Symbol </mat-header-cell>
<mat-cell *matCellDef="let element"> {{element.symbol}} </mat-cell>
</ng-container>
<mat-header-row *matHeaderRowDef="displayedColumns"></mat-header-row>
<mat-row *matRowDef="let row; columns: displayedColumns;"
(click)="selection.toggle(row)">
</mat-row>
</mat-table>
</div>
i want to delete the entire row using one click button
Explanation -> when i click on delete button the entire row should get delete but here I'm doing API call for example when i delete any row it will send name to the API!
here it is my stackbliz demo -https://stackblitz.com/edit/delete-rows-mat-table-f5f7tr?file=app%2Ftable-selection-example.ts
add a comment |
JSON
[
{ position: 1, name: 'test', value: 1.0079, symbol: 'HHH' },
{ position: 2, name: 'test2', value: 4.0026, symbol: 'BBB' },
{ position: 3, name: 'test3', value: 6.941, symbol: 'BB' },
{ position: 4, name: 'test4', value: 9.0122, symbol: 'CC' },
]
TS
isAllSelected() {
const numSelected = this.selection.selected.length;
const numRows = this.dataSource.data.length;
return numSelected === numRows;
}
removeSelectedRows() {
this.selection.selected.forEach(item => {
let index: number = this.data.findIndex(d => d === item);
console.log(this.data.findIndex(d => d === item));
this.dataSource.data.splice(index,1);
this.dataSource = new MatTableDataSource<Element>(this.dataSource.data);
setTimeout(() => {
this.dataSource.paginator = this.paginator;
});
});
this.selection = new SelectionModel<Element>(true, );
}
/** Selects all rows if they are not all selected; otherwise clear selection. */
masterToggle() {
this.isAllSelected() ?
this.selection.clear() :
this.dataSource.data.forEach(row => this.selection.select(row));
}
}
HTML
<div class="example-container mat-elevation-z8">
<mat-table #table [dataSource]="dataSource">
<!-- Checkbox Column -->
<ng-container matColumnDef="actions">
<mat-header-cell *matHeaderCellDef>
</mat-header-cell>
<mat-cell *matCellDef="let row">
<button mat-icon-button color="#b71c1c">
<mat-icon aria-label="Delete">delete</mat-icon>
</button>
</mat-cell>
</ng-container>
<!-- Position Column -->
<ng-container matColumnDef="position">
<mat-header-cell *matHeaderCellDef> No. </mat-header-cell>
<mat-cell *matCellDef="let element"> {{element.position}} </mat-cell>
</ng-container>
<!-- Weight Column -->
<ng-container matColumnDef="value">
<mat-header-cell *matHeaderCellDef> value </mat-header-cell>
<mat-cell *matCellDef="let element"> {{element.value}} </mat-cell>
</ng-container>
<!-- Symbol Column -->
<ng-container matColumnDef="symbol">
<mat-header-cell *matHeaderCellDef> Symbol </mat-header-cell>
<mat-cell *matCellDef="let element"> {{element.symbol}} </mat-cell>
</ng-container>
<mat-header-row *matHeaderRowDef="displayedColumns"></mat-header-row>
<mat-row *matRowDef="let row; columns: displayedColumns;"
(click)="selection.toggle(row)">
</mat-row>
</mat-table>
</div>
i want to delete the entire row using one click button
Explanation -> when i click on delete button the entire row should get delete but here I'm doing API call for example when i delete any row it will send name to the API!
here it is my stackbliz demo -https://stackblitz.com/edit/delete-rows-mat-table-f5f7tr?file=app%2Ftable-selection-example.ts
add a comment |
JSON
[
{ position: 1, name: 'test', value: 1.0079, symbol: 'HHH' },
{ position: 2, name: 'test2', value: 4.0026, symbol: 'BBB' },
{ position: 3, name: 'test3', value: 6.941, symbol: 'BB' },
{ position: 4, name: 'test4', value: 9.0122, symbol: 'CC' },
]
TS
isAllSelected() {
const numSelected = this.selection.selected.length;
const numRows = this.dataSource.data.length;
return numSelected === numRows;
}
removeSelectedRows() {
this.selection.selected.forEach(item => {
let index: number = this.data.findIndex(d => d === item);
console.log(this.data.findIndex(d => d === item));
this.dataSource.data.splice(index,1);
this.dataSource = new MatTableDataSource<Element>(this.dataSource.data);
setTimeout(() => {
this.dataSource.paginator = this.paginator;
});
});
this.selection = new SelectionModel<Element>(true, );
}
/** Selects all rows if they are not all selected; otherwise clear selection. */
masterToggle() {
this.isAllSelected() ?
this.selection.clear() :
this.dataSource.data.forEach(row => this.selection.select(row));
}
}
HTML
<div class="example-container mat-elevation-z8">
<mat-table #table [dataSource]="dataSource">
<!-- Checkbox Column -->
<ng-container matColumnDef="actions">
<mat-header-cell *matHeaderCellDef>
</mat-header-cell>
<mat-cell *matCellDef="let row">
<button mat-icon-button color="#b71c1c">
<mat-icon aria-label="Delete">delete</mat-icon>
</button>
</mat-cell>
</ng-container>
<!-- Position Column -->
<ng-container matColumnDef="position">
<mat-header-cell *matHeaderCellDef> No. </mat-header-cell>
<mat-cell *matCellDef="let element"> {{element.position}} </mat-cell>
</ng-container>
<!-- Weight Column -->
<ng-container matColumnDef="value">
<mat-header-cell *matHeaderCellDef> value </mat-header-cell>
<mat-cell *matCellDef="let element"> {{element.value}} </mat-cell>
</ng-container>
<!-- Symbol Column -->
<ng-container matColumnDef="symbol">
<mat-header-cell *matHeaderCellDef> Symbol </mat-header-cell>
<mat-cell *matCellDef="let element"> {{element.symbol}} </mat-cell>
</ng-container>
<mat-header-row *matHeaderRowDef="displayedColumns"></mat-header-row>
<mat-row *matRowDef="let row; columns: displayedColumns;"
(click)="selection.toggle(row)">
</mat-row>
</mat-table>
</div>
i want to delete the entire row using one click button
Explanation -> when i click on delete button the entire row should get delete but here I'm doing API call for example when i delete any row it will send name to the API!
here it is my stackbliz demo -https://stackblitz.com/edit/delete-rows-mat-table-f5f7tr?file=app%2Ftable-selection-example.ts
JSON
[
{ position: 1, name: 'test', value: 1.0079, symbol: 'HHH' },
{ position: 2, name: 'test2', value: 4.0026, symbol: 'BBB' },
{ position: 3, name: 'test3', value: 6.941, symbol: 'BB' },
{ position: 4, name: 'test4', value: 9.0122, symbol: 'CC' },
]
TS
isAllSelected() {
const numSelected = this.selection.selected.length;
const numRows = this.dataSource.data.length;
return numSelected === numRows;
}
removeSelectedRows() {
this.selection.selected.forEach(item => {
let index: number = this.data.findIndex(d => d === item);
console.log(this.data.findIndex(d => d === item));
this.dataSource.data.splice(index,1);
this.dataSource = new MatTableDataSource<Element>(this.dataSource.data);
setTimeout(() => {
this.dataSource.paginator = this.paginator;
});
});
this.selection = new SelectionModel<Element>(true, );
}
/** Selects all rows if they are not all selected; otherwise clear selection. */
masterToggle() {
this.isAllSelected() ?
this.selection.clear() :
this.dataSource.data.forEach(row => this.selection.select(row));
}
}
HTML
<div class="example-container mat-elevation-z8">
<mat-table #table [dataSource]="dataSource">
<!-- Checkbox Column -->
<ng-container matColumnDef="actions">
<mat-header-cell *matHeaderCellDef>
</mat-header-cell>
<mat-cell *matCellDef="let row">
<button mat-icon-button color="#b71c1c">
<mat-icon aria-label="Delete">delete</mat-icon>
</button>
</mat-cell>
</ng-container>
<!-- Position Column -->
<ng-container matColumnDef="position">
<mat-header-cell *matHeaderCellDef> No. </mat-header-cell>
<mat-cell *matCellDef="let element"> {{element.position}} </mat-cell>
</ng-container>
<!-- Weight Column -->
<ng-container matColumnDef="value">
<mat-header-cell *matHeaderCellDef> value </mat-header-cell>
<mat-cell *matCellDef="let element"> {{element.value}} </mat-cell>
</ng-container>
<!-- Symbol Column -->
<ng-container matColumnDef="symbol">
<mat-header-cell *matHeaderCellDef> Symbol </mat-header-cell>
<mat-cell *matCellDef="let element"> {{element.symbol}} </mat-cell>
</ng-container>
<mat-header-row *matHeaderRowDef="displayedColumns"></mat-header-row>
<mat-row *matRowDef="let row; columns: displayedColumns;"
(click)="selection.toggle(row)">
</mat-row>
</mat-table>
</div>
i want to delete the entire row using one click button
Explanation -> when i click on delete button the entire row should get delete but here I'm doing API call for example when i delete any row it will send name to the API!
here it is my stackbliz demo -https://stackblitz.com/edit/delete-rows-mat-table-f5f7tr?file=app%2Ftable-selection-example.ts
asked Jan 3 at 10:11
Just Teach MeJust Teach Me
1017
1017
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
It is always better to check for index != -1
removeSelectedRows(row) {
let index = ELEMENT_DATA.findIndex(x => x.position == row.position);
if (index != -1) {
ELEMENT_DATA.splice(index, 1)
}
this.dataSource = new MatTableDataSource<Element>(ELEMENT_DATA);
}
actually that delete icon there was a check box and when i click on particular check box it gets the CODE column value and on the basis of code column value I'm deleting the entire row! but right now i dont have check box so when i want to delete the row and click on the delete icon the row of code value get pass to the API how?
– Just Teach Me
Jan 3 at 11:24
look at here stackblitz.com/edit/angular-gbf9kz-ex9qcb?file=app/…
– Just Teach Me
Jan 3 at 11:29
Do you want to delete a row on checkbox check?
– Prashant Pimpale
Jan 3 at 11:55
1
yes but now its working!
– Just Teach Me
Jan 3 at 12:18
Done! but add if condition!
– Prashant Pimpale
Jan 3 at 12:44
|
show 1 more comment
You can try this:
HTML:
<!-- Checkbox Column -->
<ng-container matColumnDef="actions">
<mat-header-cell *matHeaderCellDef> </mat-header-cell>
<mat-cell *matCellDef="let row">
<button mat-icon-button color="#b71c1c" (click)="removeSelectedRow(row)">
<mat-icon aria-label="Delete">delete</mat-icon>
</button>
</mat-cell>
</ng-container>
TS:
removeSelectedRow(row) {
//const index = this.data.findIndex(obj => obj === row);
const index = this.data.findIndex(obj => obj.codeData == row.codeData);
this.data.splice(index, 1);
this.dataSource = new MatTableDataSource<Element>(this.dataSource.data);
}
StackBlitz HERE
Result:

1
this is absolutely right
– Just Teach Me
Jan 3 at 10:54
this.data.findIndex .... this.data coming from?
– Just Teach Me
Jan 3 at 10:56
1
The findIndex() method returns the index of the first element in the array that satisfies the provided testing function. And this.data refers to your data => data = Object.assign(ELEMENT_DATA);
– Quentin
Jan 3 at 11:02
each row contain uniq value and that uniq value is my this.codeData value so i want to delete this row in the reference of codeData value so how can i pass this value here
– Just Teach Me
Jan 3 at 11:10
In this case, when deleting, you get the object's codeData:const index = this.data.findIndex(obj => obj.codeData === row.codeData);I added a codeData to your data:{ codeData: 'ABC', position: 1, name: 'test', value: 1.0079, symbol: 'HHH' },...You can go to the StackBlitz HERE, I changed it.
– Quentin
Jan 3 at 11:29
add a comment |
add a function for deleting the row:
deleteRow(row) {
this.dataSource.data.splice(row.position-1, 1);
this.dataSource = new MatTableDataSource<Element>(this.dataSource.data);
}
and in the HTML call this function when the delete button is clicked
<button (click)="deleteRow(row)" mat-icon-button color="#b71c1c">
<mat-icon aria-label="Delete">delete</mat-icon>
</button>
1
Not good to play withrow.position-1condition
– Prashant Pimpale
Jan 3 at 11:05
@PrashantPimpale could you please look in to my new Qn sorry but you always gve me a simple answers!
– Just Teach Me
Jan 8 at 18:18
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54020147%2fdeleting-entire-row-angular-2-material%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
It is always better to check for index != -1
removeSelectedRows(row) {
let index = ELEMENT_DATA.findIndex(x => x.position == row.position);
if (index != -1) {
ELEMENT_DATA.splice(index, 1)
}
this.dataSource = new MatTableDataSource<Element>(ELEMENT_DATA);
}
actually that delete icon there was a check box and when i click on particular check box it gets the CODE column value and on the basis of code column value I'm deleting the entire row! but right now i dont have check box so when i want to delete the row and click on the delete icon the row of code value get pass to the API how?
– Just Teach Me
Jan 3 at 11:24
look at here stackblitz.com/edit/angular-gbf9kz-ex9qcb?file=app/…
– Just Teach Me
Jan 3 at 11:29
Do you want to delete a row on checkbox check?
– Prashant Pimpale
Jan 3 at 11:55
1
yes but now its working!
– Just Teach Me
Jan 3 at 12:18
Done! but add if condition!
– Prashant Pimpale
Jan 3 at 12:44
|
show 1 more comment
It is always better to check for index != -1
removeSelectedRows(row) {
let index = ELEMENT_DATA.findIndex(x => x.position == row.position);
if (index != -1) {
ELEMENT_DATA.splice(index, 1)
}
this.dataSource = new MatTableDataSource<Element>(ELEMENT_DATA);
}
actually that delete icon there was a check box and when i click on particular check box it gets the CODE column value and on the basis of code column value I'm deleting the entire row! but right now i dont have check box so when i want to delete the row and click on the delete icon the row of code value get pass to the API how?
– Just Teach Me
Jan 3 at 11:24
look at here stackblitz.com/edit/angular-gbf9kz-ex9qcb?file=app/…
– Just Teach Me
Jan 3 at 11:29
Do you want to delete a row on checkbox check?
– Prashant Pimpale
Jan 3 at 11:55
1
yes but now its working!
– Just Teach Me
Jan 3 at 12:18
Done! but add if condition!
– Prashant Pimpale
Jan 3 at 12:44
|
show 1 more comment
It is always better to check for index != -1
removeSelectedRows(row) {
let index = ELEMENT_DATA.findIndex(x => x.position == row.position);
if (index != -1) {
ELEMENT_DATA.splice(index, 1)
}
this.dataSource = new MatTableDataSource<Element>(ELEMENT_DATA);
}
It is always better to check for index != -1
removeSelectedRows(row) {
let index = ELEMENT_DATA.findIndex(x => x.position == row.position);
if (index != -1) {
ELEMENT_DATA.splice(index, 1)
}
this.dataSource = new MatTableDataSource<Element>(ELEMENT_DATA);
}
answered Jan 3 at 11:19
Prashant PimpalePrashant Pimpale
3,74531035
3,74531035
actually that delete icon there was a check box and when i click on particular check box it gets the CODE column value and on the basis of code column value I'm deleting the entire row! but right now i dont have check box so when i want to delete the row and click on the delete icon the row of code value get pass to the API how?
– Just Teach Me
Jan 3 at 11:24
look at here stackblitz.com/edit/angular-gbf9kz-ex9qcb?file=app/…
– Just Teach Me
Jan 3 at 11:29
Do you want to delete a row on checkbox check?
– Prashant Pimpale
Jan 3 at 11:55
1
yes but now its working!
– Just Teach Me
Jan 3 at 12:18
Done! but add if condition!
– Prashant Pimpale
Jan 3 at 12:44
|
show 1 more comment
actually that delete icon there was a check box and when i click on particular check box it gets the CODE column value and on the basis of code column value I'm deleting the entire row! but right now i dont have check box so when i want to delete the row and click on the delete icon the row of code value get pass to the API how?
– Just Teach Me
Jan 3 at 11:24
look at here stackblitz.com/edit/angular-gbf9kz-ex9qcb?file=app/…
– Just Teach Me
Jan 3 at 11:29
Do you want to delete a row on checkbox check?
– Prashant Pimpale
Jan 3 at 11:55
1
yes but now its working!
– Just Teach Me
Jan 3 at 12:18
Done! but add if condition!
– Prashant Pimpale
Jan 3 at 12:44
actually that delete icon there was a check box and when i click on particular check box it gets the CODE column value and on the basis of code column value I'm deleting the entire row! but right now i dont have check box so when i want to delete the row and click on the delete icon the row of code value get pass to the API how?
– Just Teach Me
Jan 3 at 11:24
actually that delete icon there was a check box and when i click on particular check box it gets the CODE column value and on the basis of code column value I'm deleting the entire row! but right now i dont have check box so when i want to delete the row and click on the delete icon the row of code value get pass to the API how?
– Just Teach Me
Jan 3 at 11:24
look at here stackblitz.com/edit/angular-gbf9kz-ex9qcb?file=app/…
– Just Teach Me
Jan 3 at 11:29
look at here stackblitz.com/edit/angular-gbf9kz-ex9qcb?file=app/…
– Just Teach Me
Jan 3 at 11:29
Do you want to delete a row on checkbox check?
– Prashant Pimpale
Jan 3 at 11:55
Do you want to delete a row on checkbox check?
– Prashant Pimpale
Jan 3 at 11:55
1
1
yes but now its working!
– Just Teach Me
Jan 3 at 12:18
yes but now its working!
– Just Teach Me
Jan 3 at 12:18
Done! but add if condition!
– Prashant Pimpale
Jan 3 at 12:44
Done! but add if condition!
– Prashant Pimpale
Jan 3 at 12:44
|
show 1 more comment
You can try this:
HTML:
<!-- Checkbox Column -->
<ng-container matColumnDef="actions">
<mat-header-cell *matHeaderCellDef> </mat-header-cell>
<mat-cell *matCellDef="let row">
<button mat-icon-button color="#b71c1c" (click)="removeSelectedRow(row)">
<mat-icon aria-label="Delete">delete</mat-icon>
</button>
</mat-cell>
</ng-container>
TS:
removeSelectedRow(row) {
//const index = this.data.findIndex(obj => obj === row);
const index = this.data.findIndex(obj => obj.codeData == row.codeData);
this.data.splice(index, 1);
this.dataSource = new MatTableDataSource<Element>(this.dataSource.data);
}
StackBlitz HERE
Result:

1
this is absolutely right
– Just Teach Me
Jan 3 at 10:54
this.data.findIndex .... this.data coming from?
– Just Teach Me
Jan 3 at 10:56
1
The findIndex() method returns the index of the first element in the array that satisfies the provided testing function. And this.data refers to your data => data = Object.assign(ELEMENT_DATA);
– Quentin
Jan 3 at 11:02
each row contain uniq value and that uniq value is my this.codeData value so i want to delete this row in the reference of codeData value so how can i pass this value here
– Just Teach Me
Jan 3 at 11:10
In this case, when deleting, you get the object's codeData:const index = this.data.findIndex(obj => obj.codeData === row.codeData);I added a codeData to your data:{ codeData: 'ABC', position: 1, name: 'test', value: 1.0079, symbol: 'HHH' },...You can go to the StackBlitz HERE, I changed it.
– Quentin
Jan 3 at 11:29
add a comment |
You can try this:
HTML:
<!-- Checkbox Column -->
<ng-container matColumnDef="actions">
<mat-header-cell *matHeaderCellDef> </mat-header-cell>
<mat-cell *matCellDef="let row">
<button mat-icon-button color="#b71c1c" (click)="removeSelectedRow(row)">
<mat-icon aria-label="Delete">delete</mat-icon>
</button>
</mat-cell>
</ng-container>
TS:
removeSelectedRow(row) {
//const index = this.data.findIndex(obj => obj === row);
const index = this.data.findIndex(obj => obj.codeData == row.codeData);
this.data.splice(index, 1);
this.dataSource = new MatTableDataSource<Element>(this.dataSource.data);
}
StackBlitz HERE
Result:

1
this is absolutely right
– Just Teach Me
Jan 3 at 10:54
this.data.findIndex .... this.data coming from?
– Just Teach Me
Jan 3 at 10:56
1
The findIndex() method returns the index of the first element in the array that satisfies the provided testing function. And this.data refers to your data => data = Object.assign(ELEMENT_DATA);
– Quentin
Jan 3 at 11:02
each row contain uniq value and that uniq value is my this.codeData value so i want to delete this row in the reference of codeData value so how can i pass this value here
– Just Teach Me
Jan 3 at 11:10
In this case, when deleting, you get the object's codeData:const index = this.data.findIndex(obj => obj.codeData === row.codeData);I added a codeData to your data:{ codeData: 'ABC', position: 1, name: 'test', value: 1.0079, symbol: 'HHH' },...You can go to the StackBlitz HERE, I changed it.
– Quentin
Jan 3 at 11:29
add a comment |
You can try this:
HTML:
<!-- Checkbox Column -->
<ng-container matColumnDef="actions">
<mat-header-cell *matHeaderCellDef> </mat-header-cell>
<mat-cell *matCellDef="let row">
<button mat-icon-button color="#b71c1c" (click)="removeSelectedRow(row)">
<mat-icon aria-label="Delete">delete</mat-icon>
</button>
</mat-cell>
</ng-container>
TS:
removeSelectedRow(row) {
//const index = this.data.findIndex(obj => obj === row);
const index = this.data.findIndex(obj => obj.codeData == row.codeData);
this.data.splice(index, 1);
this.dataSource = new MatTableDataSource<Element>(this.dataSource.data);
}
StackBlitz HERE
Result:

You can try this:
HTML:
<!-- Checkbox Column -->
<ng-container matColumnDef="actions">
<mat-header-cell *matHeaderCellDef> </mat-header-cell>
<mat-cell *matCellDef="let row">
<button mat-icon-button color="#b71c1c" (click)="removeSelectedRow(row)">
<mat-icon aria-label="Delete">delete</mat-icon>
</button>
</mat-cell>
</ng-container>
TS:
removeSelectedRow(row) {
//const index = this.data.findIndex(obj => obj === row);
const index = this.data.findIndex(obj => obj.codeData == row.codeData);
this.data.splice(index, 1);
this.dataSource = new MatTableDataSource<Element>(this.dataSource.data);
}
StackBlitz HERE
Result:

edited Jan 3 at 11:32
answered Jan 3 at 10:44
QuentinQuentin
431215
431215
1
this is absolutely right
– Just Teach Me
Jan 3 at 10:54
this.data.findIndex .... this.data coming from?
– Just Teach Me
Jan 3 at 10:56
1
The findIndex() method returns the index of the first element in the array that satisfies the provided testing function. And this.data refers to your data => data = Object.assign(ELEMENT_DATA);
– Quentin
Jan 3 at 11:02
each row contain uniq value and that uniq value is my this.codeData value so i want to delete this row in the reference of codeData value so how can i pass this value here
– Just Teach Me
Jan 3 at 11:10
In this case, when deleting, you get the object's codeData:const index = this.data.findIndex(obj => obj.codeData === row.codeData);I added a codeData to your data:{ codeData: 'ABC', position: 1, name: 'test', value: 1.0079, symbol: 'HHH' },...You can go to the StackBlitz HERE, I changed it.
– Quentin
Jan 3 at 11:29
add a comment |
1
this is absolutely right
– Just Teach Me
Jan 3 at 10:54
this.data.findIndex .... this.data coming from?
– Just Teach Me
Jan 3 at 10:56
1
The findIndex() method returns the index of the first element in the array that satisfies the provided testing function. And this.data refers to your data => data = Object.assign(ELEMENT_DATA);
– Quentin
Jan 3 at 11:02
each row contain uniq value and that uniq value is my this.codeData value so i want to delete this row in the reference of codeData value so how can i pass this value here
– Just Teach Me
Jan 3 at 11:10
In this case, when deleting, you get the object's codeData:const index = this.data.findIndex(obj => obj.codeData === row.codeData);I added a codeData to your data:{ codeData: 'ABC', position: 1, name: 'test', value: 1.0079, symbol: 'HHH' },...You can go to the StackBlitz HERE, I changed it.
– Quentin
Jan 3 at 11:29
1
1
this is absolutely right
– Just Teach Me
Jan 3 at 10:54
this is absolutely right
– Just Teach Me
Jan 3 at 10:54
this.data.findIndex .... this.data coming from?
– Just Teach Me
Jan 3 at 10:56
this.data.findIndex .... this.data coming from?
– Just Teach Me
Jan 3 at 10:56
1
1
The findIndex() method returns the index of the first element in the array that satisfies the provided testing function. And this.data refers to your data => data = Object.assign(ELEMENT_DATA);
– Quentin
Jan 3 at 11:02
The findIndex() method returns the index of the first element in the array that satisfies the provided testing function. And this.data refers to your data => data = Object.assign(ELEMENT_DATA);
– Quentin
Jan 3 at 11:02
each row contain uniq value and that uniq value is my this.codeData value so i want to delete this row in the reference of codeData value so how can i pass this value here
– Just Teach Me
Jan 3 at 11:10
each row contain uniq value and that uniq value is my this.codeData value so i want to delete this row in the reference of codeData value so how can i pass this value here
– Just Teach Me
Jan 3 at 11:10
In this case, when deleting, you get the object's codeData:
const index = this.data.findIndex(obj => obj.codeData === row.codeData); I added a codeData to your data: { codeData: 'ABC', position: 1, name: 'test', value: 1.0079, symbol: 'HHH' },... You can go to the StackBlitz HERE, I changed it.– Quentin
Jan 3 at 11:29
In this case, when deleting, you get the object's codeData:
const index = this.data.findIndex(obj => obj.codeData === row.codeData); I added a codeData to your data: { codeData: 'ABC', position: 1, name: 'test', value: 1.0079, symbol: 'HHH' },... You can go to the StackBlitz HERE, I changed it.– Quentin
Jan 3 at 11:29
add a comment |
add a function for deleting the row:
deleteRow(row) {
this.dataSource.data.splice(row.position-1, 1);
this.dataSource = new MatTableDataSource<Element>(this.dataSource.data);
}
and in the HTML call this function when the delete button is clicked
<button (click)="deleteRow(row)" mat-icon-button color="#b71c1c">
<mat-icon aria-label="Delete">delete</mat-icon>
</button>
1
Not good to play withrow.position-1condition
– Prashant Pimpale
Jan 3 at 11:05
@PrashantPimpale could you please look in to my new Qn sorry but you always gve me a simple answers!
– Just Teach Me
Jan 8 at 18:18
add a comment |
add a function for deleting the row:
deleteRow(row) {
this.dataSource.data.splice(row.position-1, 1);
this.dataSource = new MatTableDataSource<Element>(this.dataSource.data);
}
and in the HTML call this function when the delete button is clicked
<button (click)="deleteRow(row)" mat-icon-button color="#b71c1c">
<mat-icon aria-label="Delete">delete</mat-icon>
</button>
1
Not good to play withrow.position-1condition
– Prashant Pimpale
Jan 3 at 11:05
@PrashantPimpale could you please look in to my new Qn sorry but you always gve me a simple answers!
– Just Teach Me
Jan 8 at 18:18
add a comment |
add a function for deleting the row:
deleteRow(row) {
this.dataSource.data.splice(row.position-1, 1);
this.dataSource = new MatTableDataSource<Element>(this.dataSource.data);
}
and in the HTML call this function when the delete button is clicked
<button (click)="deleteRow(row)" mat-icon-button color="#b71c1c">
<mat-icon aria-label="Delete">delete</mat-icon>
</button>
add a function for deleting the row:
deleteRow(row) {
this.dataSource.data.splice(row.position-1, 1);
this.dataSource = new MatTableDataSource<Element>(this.dataSource.data);
}
and in the HTML call this function when the delete button is clicked
<button (click)="deleteRow(row)" mat-icon-button color="#b71c1c">
<mat-icon aria-label="Delete">delete</mat-icon>
</button>
answered Jan 3 at 10:43
MoradMorad
1,3791017
1,3791017
1
Not good to play withrow.position-1condition
– Prashant Pimpale
Jan 3 at 11:05
@PrashantPimpale could you please look in to my new Qn sorry but you always gve me a simple answers!
– Just Teach Me
Jan 8 at 18:18
add a comment |
1
Not good to play withrow.position-1condition
– Prashant Pimpale
Jan 3 at 11:05
@PrashantPimpale could you please look in to my new Qn sorry but you always gve me a simple answers!
– Just Teach Me
Jan 8 at 18:18
1
1
Not good to play with
row.position-1 condition– Prashant Pimpale
Jan 3 at 11:05
Not good to play with
row.position-1 condition– Prashant Pimpale
Jan 3 at 11:05
@PrashantPimpale could you please look in to my new Qn sorry but you always gve me a simple answers!
– Just Teach Me
Jan 8 at 18:18
@PrashantPimpale could you please look in to my new Qn sorry but you always gve me a simple answers!
– Just Teach Me
Jan 8 at 18:18
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54020147%2fdeleting-entire-row-angular-2-material%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown