0

When I remove table row in Angular it's removing all rows after index, I want to remove the particlar row.

HTML :

<div>
<table class="table">
    <tr>
        <td>Date of GPA</td>
        <td>Type</td>
        <td>Grade</td>
        <td>Standard GPA</td>
        <td></td>
    </tr>
    <tr ng-repeat="gpa in ctrl.gpatests">
        <td>
            <md-input-container>
                <input ng-model="gpa[$index].date" ui-mask="99/99/9999">
            </md-input-container>
        </td>
        <td>
            <md-select ng-model="gpa[$index].type" ng-required="true">
                <md-option ng-value="item.id" ng-repeat="item in ctrl.dispHsGradYr">{{item.displayName}}</md-option>
            </md-select> 
        </td>
        <td>
            <md-select ng-model="gpa[$index].grade" ng-required="true" >
                <md-option ng-value="item.id" ng-repeat="item in ctrl.dispHsGradYr">{{item.displayName}}</md-option>
            </md-select> 
        </td>
        <td>3.0</td>
        <td><input type="text" ng-model="gpa[$index].id"/>{{$index}}<span ng-click="removeGPAScore(gpa[$index].id)"> x </span></td>
    </tr>
</table>
<button class="md-raised md-primary savebtn md-button md-default-theme" ng-click="addGPAScore()" type="BUTTON" aria-disabled="false">
        <span class="ng-scope">ADD GPA</span>
</button>
{{ctrl.gpatests | json}}
</div>

removeGPAScore , addGPAScore functions

Script :

$scope.addGPAScore(){
    var data = {};
    data.id ='' ;
    data.date ='' ;
    data.type ='';
    data.grade ='';
    data.gpaValue ='';
    ctrl.gpatests.push(data);
}

$scope.removeGPAScore(index){
    ctrl.gpatests=newDataList;
    ctrl.gpatests.splice( index, 1 );
}

What went wrong in the script.?

1
  • You mean you want to remove dom element or remove data associated with that row Commented May 15, 2017 at 7:57

3 Answers 3

1

Since you are splice-ing on the same array, you need to pass $index itself instead of gpa[$index].id to the removeGPAScore function. Like this,

<span ng-click="removeGPAScore($index)"> x </span>
Sign up to request clarification or add additional context in comments.

5 Comments

When I Delete it its still removes the other rows. can you please create a JS fiddle for me.
Can you add more Script to this plnkr please
it's still removing all the rows after that particular id. I think my add more script has some issue.
Yes, i Found Issue in Add Script. i have give wrong model name
@Developer glad you found it.. hope it is working now
0

Why is that ng-click="removeGPAScore(gpa[$index].id)"> ?

Mention it as: ng-click="removeGPAScore(gpa[$index])">

1 Comment

is my add row script is correct .? can you optimize it.
0

Replace ng-model="gpa[$index].date" with Use ng-model="gpa.date"

Then Use

 $scope.removeGPAScore(index){
    ctrl.gpatests=newDataList;
    ctrl.gpatests.splice( index, 1 );
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.