【问题标题】:How to remove extra added row for generated JSON on button click either using AngularJS or JavaScript?如何使用 AngularJS 或 JavaScript 在按钮单击时删除生成的 JSON 的额外添加行?
【发布时间】:2016-11-11 09:21:55
【问题描述】:

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

我的要求是我需要在表中添加相应的json names and show them,然后需要在button单击上为added/selected json 名称生成json 对象。它工作正常(我可以在 UI 表上显示我的json names 并且可以在button 点击后为我的selected/added json 命名数据的get/generate json 数据对象button)。

但是,问题是:在生成 json 对象或单击 Send 按钮后,我可以看到 one row is adding extra on my UI table after clicking of Send buttonI don't need this added extra row for my UI table,我只需要添加的 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',
        });
  });

【问题讨论】:

    标签: javascript jquery angularjs json angularjs-ng-repeat


    【解决方案1】:

    您正在更新 ng-repeat 中使用的 $rootScope.firstJsonNames 和 $scope.secondJsonNames,因此值显示在表中。使用新变量创建 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',
            });
      });
    

    【讨论】:

    • 感谢您的回复和帮助!是的,它现在按照要求工作正常!再次感谢您!
    猜你喜欢
    • 1970-01-01
    • 2011-12-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-22
    • 2016-11-05
    • 2022-11-30
    相关资源
    最近更新 更多