Add, Remove & Update specific data In JSON in AngularJS

后端 未结 2 1346
不思量自难忘°
不思量自难忘° 2021-02-10 13:52

I have pulled data from json file. Now its displayed over DOM. On HTML Page, I have three option 1) Edit Data 2) Delete Particular Data & 3) Add New Data.

How to per

2条回答
  •  旧巷少年郎
    2021-02-10 14:40

    Here is my approach to this requirement. Let me know if any further improvement can be added. The entire code can be found at the below URL:

    Angular Save, Update and Delete

    The sample screenshots from the code can be found here...

    controller:
    
    
    'use strict';
    
    function MainController($scope, SharedService, ngDialog) {
    
      $scope.account_type_selected = "Savings";
      $scope.sharedService = SharedService;
      $scope.savingsMain = [];
      $scope.checkingsMain = [];
      $scope.addToCheckingsAccounts = {};
      $scope.addToSavingsAccounts = {};
    
    
      $scope.setAccountType = function (type) {
        if (type === "allAccounts") {
          $scope.showSavings = true;
          $scope.showCheckings = true;
        } else if (type === "savingsAccounts") {
          $scope.showSavings = true;
          $scope.showCheckings = false;
        } else if (type === "checkingAccounts") {
          $scope.showSavings = false;
          $scope.showCheckings = true;
        }
        $scope.account_type_selected = type;
      };
    
      $scope.$watch('savingsMain', function ($scope) {
        return $scope.savingsMain;
      });
    
      $scope.selectedAccountType = function (showAccount) {
        console.log(showAccount);
        if (showAccount === "Savings") {
          $scope.sharedService.accountType = "Savings";
        } else {
          $scope.sharedService.accountType = "Checkings";
        }
      };
    
    
      $scope.saveAccounts = function () {
        if ($scope.sharedService.accountType === "Savings") {
          $scope.addToSavingsAccounts = {
            "account_type": $scope.sharedService.accountType,
            "amount": $scope.sharedService.amount,
            "date": $scope.sharedService.date,
            "maturity": $scope.sharedService.maturity
          };
          $scope.showSavings = true;
    
          $scope.savingsMain.push($scope.addToSavingsAccounts);
        } else {
          $scope.addToCheckingsAccounts = {
            "account_type": $scope.sharedService.accountType,
            "amount": $scope.sharedService.amount,
            "bic": $scope.sharedService.BIC,
            "iban": $scope.sharedService.IBAN
          };
          $scope.showCheckings = true;
          $scope.checkingsMain.push($scope.addToCheckingsAccounts);
    
        }
        ngDialog.close();
    
      };
    
      $scope.deleteDataFromSharedService = function (accountType, item) {
        if (accountType === "Savings") {
          $scope.savingsMain = _.without($scope.savingsMain, _.findWhere($scope.savingsMain, { date: item }));
        } else if (accountType === "Checkings") {
          $scope.checkingsMain = _.without($scope.checkingsMain, _.findWhere($scope.checkingsMain, { bic: item }));
        }
      };
    
      $scope.closeDialog = function () {
        ngDialog.close();
      };
    
      $scope.accountTypeModel = [];
    
    
      $scope.prop = {
        "type": "select",
        "name": "account_type",
        "value": $scope.sharedService.accountType,
        "accountTypeData": ["Savings", "Checkings"]
      };
    
    }

提交回复
热议问题