javascript - 如何使用 AngularJS 或 JavaScript 删除按钮单击时生成的 JSON 的额外添加行?

标签 javascript jquery angularjs json angularjs-ng-repeat

我正在为我的两个 JSON 文件数据执行一些操作,例如添加删除

我的要求是我需要添加各自的json名称并在表格中显示它们,然后需要为添加/选择生成json对象 button 单击时的 json 名称。它工作正常(我可以在 UI 表格上显示我的 json 名称,并且可以为我的选择/添加获取/生成 json 数据对象> 单击按钮后的 json 名称数据)。

但是,问题是:生成 json 对象后或单击 Send 按钮后,我可以看到 单击 Send 按钮后,一行在我的 UI 表格上添加了额外内容, 我不需要为我的 UI 表添加额外的行,我只需要我添加的 json 名称,只有那些在单击 Send< 后应该显示在我的表中的内容 按钮。我的两个 json 表发生了这种情况。(我有两个单独的“发送”按钮,一个用于第一个 JSON,另一个用于第二个 JSON)。

我不确定这里出了什么问题?请帮助我在单击按钮时在表中显示所选的 json 名称,其中不应包含使用 AngularJSJavaScript 添加的额外行。先感谢您 !创建Plnkr .

html:

<div>   
<p>First Table:</p>
    <table>
        <thead>
            <tr>
                <th>Name</th>
                <th>Add</th>
            </tr>
        </thead>
        <tbody>
            <tr ng-repeat="(key, value) in myFirstJson.Testing">
                <td>{{getKey(value)}}</td>
                <td><button ng-click="addFirst(value)">Add</button></td>
            </tr>
        </tbody>
    </table>
    <br> <br>

    <table border="1" class="table">
        <thead>
            <tr>
                <th>Name</th>
                <th>Delete</th>
            </tr>
        </thead>
        <tbody>
            <tr ng-repeat="(key, value) in firstJsonNames track by $index">
                <td>{{getKey(value)}}</td>
                <td><button ng-click="deleteFirst($index)">Delete</button></td>
            </tr>
            <tr ng-hide="firstJsonNames.length > 0">
                <td colspan="3">
                    <p>No Names</p>
                </td>
            </tr>
        </tbody>
    </table>
    <br>
    <br>
    <table>
        <tbody>
            <tr>
                <td>First Dropdown:<select ng-model="firstJsonNames.firstdropdown">
                        <option value="Test1">Test1</option>
                        <option value="Test2">Test2</option>
                        <option value="Test3">Test3</option>
                </select><br />
                </td>
            </tr>

            <tr>
                <td>First Input:<input type="text" maxlength="50" size="50"
                     ng-model="firstJsonNames.firstInput" /> <br /></td>
            </tr>
        </tbody>
    </table>
    <br>
    <br>
    <button ng-click="generateFirstJson()">Send</button>

    <br>
    <br><p>Second Table:</p>
    <table>
        <thead>
            <tr>
                <th>Name</th>
                <th>Add</th>
            </tr>
        </thead>
        <tbody>
            <tr ng-repeat="(key, value) in mySecondJson.MyTest">
                <td>{{value.Main.static.name}}</td>
                <td><button ng-click="addSecond(value.Main.static.name)">Add</button></td>
            </tr>
        </tbody>
    </table>
    <br>
    <br> 

    <table border="1" class="table">
        <thead>
            <tr>
                <th>Name</th>
                <th>Delete</th>
            </tr>
        </thead>
        <tbody>
            <tr ng-repeat="name in secondJsonNames">
                <td>{{name}}</td>
                <td><button ng-click="deleteSecond(name)">Delete</button></td>
            </tr>
            <tr ng-hide="mySecondJson.MyTest.length">
                <td colspan="3">
                    <p>No Names</p>
                </td>
            </tr>
        </tbody>
    </table>
    <br>
    <br>
    <label>Enter Second Input Data:</label> <input
        ng-model="secondJsonNames.SecondInput" placeholder="Input Text"><br>
    <br>
    <button ng-click="generateSecondJson()">Send</button>
    <br>
    <br>


</div>

app.js:

var app = angular.module('myApp', ['ui.router']);
    app.controller('TestCtrl',function($scope, $http,$state,$stateParams,filterFilter,$rootScope) {
      $rootScope.firstJsonNames = [];
      $scope.secondJsonNames = [];
      $scope.firstJsonNames.firstdropdown="Test1";
      $scope.firstJsonNames.firstInput="1.5";

        if($rootScope.myFirstJson == undefined)
        {
            $http.get('test.json').success(function(response) {
                $rootScope.myFirstJson = response;
            });
        }    
          $scope.addFirst = function (name){
            $rootScope.firstJsonNames.push(name);
            console.log($rootScope.firstJsonNames);
          };
           $scope.deleteFirst = function (index){
            $rootScope.firstJsonNames.splice(index,1);
          };

        $scope.getKey = function(item) {
            return Object.keys(item)[0];
          };
        $scope.generateFirstJson = function(){
           $rootScope.firstJsonNames.push({firstdropdown:$rootScope.firstJsonNames.firstdropdown, firstInput:$rootScope.firstJsonNames.firstInput});
          console.log(angular.toJson($rootScope.firstJsonNames));
       };

        //second json
          if($rootScope.mySecondJson == undefined)
        {
            $http.get('test1.json').success(function(response) {
                $rootScope.mySecondJson = response;
            });
        }
        $scope.addSecond = function (name){
            $scope.secondJsonNames.push(name);
            console.log($scope.secondJsonNames);
          };
           $scope.deleteSecond = function (name){
             index = $scope.secondJsonNames.indexOf(name);
             $scope.secondJsonNames.splice(index,1);
          };

        $scope.generateSecondJson = function(){
          $scope.secondJsonNames.push({SecondInput:$scope.secondJsonNames.SecondInput});
          console.log(angular.toJson($scope.secondJsonNames));
        };
    });
   app.config(function($stateProvider, $urlRouterProvider) {
    $urlRouterProvider.otherwise('/home');
    $stateProvider
        .state('home', {
            url: '/home',
            templateUrl: 'main.html',
            controller: 'TestCtrl',
        });
  });

最佳答案

您正在更新 $rootScope.firstJsonNames 和 $scope.secondJsonNames,它们在 ng-repeat 中使用,因此值显示在表中。使用新变量创建 json。

供您引用:

我用过

$scope.newjson2 = [];
      $scope.newjson1 = [];

Plunker http://plnkr.co/edit/r0VTaaT2rcfkiBNqyRmt?p=preview

JS:

var app = angular.module('myApp', ['ui.router']);
    app.controller('TestCtrl',function($scope, $http,$state,$stateParams,filterFilter,$rootScope) {
      $rootScope.firstJsonNames = [];
      $scope.secondJsonNames = [];
      $scope.firstJsonNames.firstdropdown="Test1";
      $scope.firstJsonNames.firstInput="1.5";
      $scope.newjson2 = [];
      $scope.newjson1 = [];

        if($rootScope.myFirstJson == undefined)
        {
            $http.get('test.json').success(function(response) {
                $rootScope.myFirstJson = response;
            });
        }    
          $scope.addFirst = function (name){
            $rootScope.firstJsonNames.push(name);
            console.log($rootScope.firstJsonNames);
          };
           $scope.deleteFirst = function (index){
            $rootScope.firstJsonNames.splice(index,1);
          };

        $scope.getKey = function(item) {
            return Object.keys(item)[0];
          };
        $scope.generateFirstJson = function(){
           $scope.newjson1 = angular.copy($rootScope.firstJsonNames);
            $scope.newjson1.push({firstdropdown:$scope.firstJsonNames.firstdropdown, firstInput:$scope.firstJsonNames.firstInput});
          console.log(angular.toJson( $scope.newjson1));
       };

        //second json
          if($rootScope.mySecondJson == undefined)
        {
            $http.get('test1.json').success(function(response) {
                $rootScope.mySecondJson = response;
            });
        }
        $scope.addSecond = function (name){
            $scope.secondJsonNames.push(name);
            console.log($scope.secondJsonNames);
          };
           $scope.deleteSecond = function (name){
             index = $scope.secondJsonNames.indexOf(name);
             $scope.secondJsonNames.splice(index,1);
          };

        $scope.generateSecondJson = function(){
          $scope.newjson2 = angular.copy($scope.secondJsonNames);
          $scope.newjson2.push({SecondInput:$scope.secondJsonNames.SecondInput});
          console.log(angular.toJson($scope.newjson2));
        };
    });
   app.config(function($stateProvider, $urlRouterProvider) {
    $urlRouterProvider.otherwise('/home');
    $stateProvider
        .state('home', {
            url: '/home',
            templateUrl: 'main.html',
            controller: 'TestCtrl',
        });
  });

关于javascript - 如何使用 AngularJS 或 JavaScript 删除按钮单击时生成的 JSON 的额外添加行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40544740/

相关文章:

jquery - APEX ORDS Rest API 返回内容类型 :text/html

javascript - 按 Enter 键执行 jquery 函数

html - 按钮上的 Angular ngHide 在它原来的位置留下空间

angularjs - 如何使用 ionic 框架在 angularJS 中自定义下拉列表

javascript - 将字符串的第一个字符转换为大写

javascript - 在 JavaScript 中,整数在存储布局中是如何表示的?

javascript - 实例化对象时引用Javascript对象方法

javascript - 在 <a> 标签上创建 "title"属性

javascript - Angular 验证器和 ng-maxlength 使用

javascript - 深度扩展,扩展到对象数组?