繁体   English   中英

单击按钮时将所选行数据作为参数传递

[英]Pass selected row data as an argument on button click

我想通过单击按钮将所选行的数据发送到后端。 我可以通过为表的每一行都具有一个“移动”按钮来实现这一点,但是我的用例是不同的。 我在表格顶部只有一个按钮,我必须先选择该行,然后单击“移动”按钮以将所选行的数据发送到后端。 我正在使用智能表模块进行数据网格处理。

这是我的HTML代码

<body ng-controller="mainCtrl">
  <div class="table-container">

    <button type="button" ng-click="moveToSafe(row)" class="btn btn-sm btn-success">
      Move to safe
    </button>

    <table st-table="rowCollection" class="table">
      <thead>
        <tr>
          <th st-sort="firstName">first name</th>
          <th st-sort="lastName">last name</th>
          <th st-sort="birthDate">birth date</th>
          <th st-sort="balance">balance</th>
        </tr>
      </thead>
      <tbody>
        <tr st-select-row="row" ng-repeat="row in rowCollection">
          <td>{{row.firstName | uppercase}}</td>
          <td>{{row.lastName}}</td>
          <td>{{row.birthDate | date}}</td>
          <td>{{row.balance | currency}}</td>
        </tr>
      </tbody>
    </table>
  </div>
  <div ng-show="isLoading" class="loading-indicator"></div>
</body>

我的JS代码

angular
  .module('myApp', ['smart-table'])
  .controller('mainCtrl', ['$scope',
    function($scope) {

      $scope.isLoading = false;
      $scope.rowCollection = [{
        firstName: 'Laurent',
        lastName: 'Renard',
        birthDate: new Date('1987-05-21'),
        balance: 102
      }, {
        firstName: 'Blandine',
        lastName: 'Faivre',
        birthDate: new Date('1987-04-25'),
        balance: -2323.22
      }, {
        firstName: 'Francoise',
        lastName: 'Frere',
        birthDate: new Date('1955-08-27'),
        balance: 42343
      }];

      $scope.moveToSafe = function(row) {
        console.log(row);
        // I want the selected row data here inside 'row'
      };
    }
  ]);

根据您的变量更改此变量名称。

<html>
<head>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
    <script>

        angular.module('myApp', []).controller('namesCtrl', function($scope) {
            $scope.isLoading = false;
            $scope.checkedData = [];
                    $scope.rowCollection = [{
                      firstName: 'Laurent',
                      lastName: 'Renard',
                      birthDate: new Date('1987-05-21'),
                      balance: 102
                    }, {
                      firstName: 'Blandine',
                      lastName: 'Faivre',
                      birthDate: new Date('1987-04-25'),
                      balance: -2323.22
                    }, {
                      firstName: 'Francoise',
                      lastName: 'Frere',
                      birthDate: new Date('1955-08-27'),
                      balance: 42343
                    }];

                    $scope.collectedData = function(val){
                        $scope.checkedData.push(val);
                    }

                    $scope.moveToSafe = function() {
                      console.log($scope.checkedData)
                    };
        })
    </script>
</head>
<body ng-app="myApp" ng-controller="namesCtrl">
    <div class="table-container">
        <button type="button" ng-click="moveToSafe()" class="btn btn-sm btn-success">
            Move to Safe
        </button>
        <table st-table="rowCollection" class="table">
        <thead>
          <tr>
            <th st-sort="firstName">first name</th>
            <th st-sort="lastName">last name</th>
            <th st-sort="birthDate">birth date</th>
            <th st-sort="balance">balance</th>
            <th>Select </th>
          </tr>
        </thead>
        <tbody>
          <tr st-select-row="row" ng-repeat="row in rowCollection">
            <td>{{row.firstName | uppercase}}</td>
            <td>{{row.lastName}}</td>
            <td>{{row.birthDate | date}}</td>
            <td>{{row.balance | currency}}</td>
            <td><input type="checkbox" ng-modal="checkedData" ng-click="collectedData(row)"></td>
          </tr>
        </tbody>
      </table>
    </div>  
</body>

您应该将按钮放置在ng-repeat内,然后只有row变量将保留其值

 <tr st-select-row="row" ng-repeat="row in rowCollection">
          <td>{{row.firstName | uppercase}}</td>
          <td>{{row.lastName}}</td>
          <td>{{row.birthDate | date}}</td>
          <td>{{row.balance | currency}}</td>
           <button type="button" ng-click="moveToSafe(row)" class="btn btn-sm btn-success">
      Move to safe
    </button>
 </tr>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM