Search code examples
javascriptangularjsconditional-statementsdisabled-input

How to remove row from table / disable the submit button permanently once user clicked on submit


var app = angular.module("myApp", []);
app.controller("myCtrl", function($scope) {
   
    $scope.items = [
        {name: 'xxx', amount: 13, years: 2, interest: 2},    
        {name: 'yyy', amount: 23, years: 1, interest: 3},
        {name: 'zzz', amount: 123, years: 4, interest: 4}
    ];
    $scope.sub = function()
    {
    }
    
});
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<body ng-app="myApp">

<table ng-controller="myCtrl" border="1">
    <tbody>
            <tr ng-repeat="item in items">
                <td>{{item.name}}</td>
                <td><input type=text ng-value="item.years" ng-model="first"></td>
                <td><input type=text ng-value="item.amount" ng-model="amount"></td>
                <td><input type=submit ng-click="sub()"></td>
            </tr>
            
        </tbody>
    </table>           



</body>
</html>

I above snippet what happening is when user clicks on submit button, data gets store in db and returns with updated. So what I want is once I received updated that particular click row should be removed or that button should become PERMANENTLY disabled irrespective of refresh.

I try to implement it disabled but once it received updated it making all the button disabled.

please Help me for this


Solution

  • Delete the row or disable the button in your sub() function.

    var app = angular.module("myApp", []);
    app.controller("myCtrl", function($scope) {
        $scope.items = [
            {name: 'xxx', amount: 13, years: 2, interest: 2, disabled: false},    
            {name: 'yyy', amount: 23, years: 1, interest: 3,disabled: false},
            {name: 'zzz', amount: 123, years: 4, interest: 4,disabled: false}
        ];
        
        $scope.sub = function(name)
        {
           for(var i=0;i<$scope.items.length;i++){
              if($scope.items[i].name === name){
                 if($scope.disableButton){
                   $scope.items[i].disabled = true;
                 }else{
                   $scope.items.splice(i,1);
                 }
              }
           }
        }
        
    });
    <!DOCTYPE html>
    <html>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
        <body ng-app="myApp">
         <label>Disable button (will delete if not checked):
         <input type="checkbox" ng-model="disableButton">
         </label><br/>
        <table ng-controller="myCtrl" border="1">
            <tbody>
                    <tr ng-repeat="item in items">
                        <td>{{item.name}}</td>
                        <td><input type=text ng-value="item.years" ng-model="first"></td>
                        <td><input type=text ng-value="item.amount" ng-model="amount"></td>
                        <td><input type=submit ng-disabled="item.disabled" ng-click="sub(item.name)"></td>
            </tr>
          </tbody>
        </table>           
      </body>
    </html>