通过外部控制进行 AngularJS 智能表过滤

标签 angularjs smart-table

我试图找出正确的方法来合并 st-table、st-safe-src 和通过位于表本身之外的控件过滤数据。用户可以添加、编辑和删除数据,这就是我使用安全源的原因。

任何示例或反馈都会很棒!

最佳答案

查看此示例,其中包含从智能表中添加、编辑、删除行的选项。

http://plnkr.co/edit/DtD4xC

javascript

angular.module('myApp', ['smart-table'])
.controller('mainCtrl', ['$scope', function($scope) {
  $scope.rowCollection = [{
    id: 100,
    firstName: 'Laurent',
    lastName: 'Renard',
    birthDate: new Date('1987-05-21'),
    balance: 102,
    email: 'whatever@gmail.com'
  }, {
    id: 101,
    firstName: 'Blandine',
    lastName: 'Faivre',
    birthDate: new Date('1987-04-25'),
    balance: -2323.22,
    email: 'oufblandou@gmail.com'
  }, {
    id: 102,
    firstName: 'Francoise',
    lastName: 'Frere',
    birthDate: new Date('1955-08-27'),
    balance: 42343,
    email: 'raymondef@gmail.com'
  }];
  var id = 1;
  $scope.edit = false;
  $scope.addRandomItem = function addRandomItem() {
    $scope.editrow = {id:++id};
    $scope.edit = true;
  };

  $scope.removeItem = function removeItem(row) {
    var index = $scope.rowCollection.indexOf(row);
    if (index !== -1) {
      $scope.rowCollection.splice(index, 1);
    }
  }

  $scope.editItem = function editItem(row) {
    $scope.editrow = angular.copy(row);
    $scope.edit = true;
  }

  $scope.saveItem = function saveItem(editrow) {
    var index;
    var itemStatus=false;

    for (index = 0; index < $scope.rowCollection.length; index++) {
      if ($scope.rowCollection[index].id === editrow.id) {
        itemStatus=true;
        break;
      }
    }
    if (itemStatus) {
      console.log("Replacing item:"+editrow.id);
      $scope.rowCollection.splice(index, 1, editrow);
      $scope.editrow = {id:++id};
    }
    else {
      console.log("Adding item:"+editrow.id);
      $scope.rowCollection.push(editrow);
      $scope.editrow = {id:++id};
    }
    $scope.edit = false;
  }

}]);

html
<!DOCTYPE html>
<html ng-app="myApp">
  <head>
    <link data-require="bootstrap-css@3.2.0" data-semver="3.2.0" rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css" />
    <script data-require="angular.js@1.2.21" data-semver="1.2.21" src="https://code.angularjs.org/1.2.21/angular.js"></script>
    <link rel="stylesheet" href="style.css" />
    <script src="script.js"></script>
    <script src="smart-table.debug.js"></script>
    <script src="lrInfiniteScrollPlugin.js"></script>
  </head>
  <body ng-controller="mainCtrl">
    <h3>Basic Smart-Table Starter</h3>
    <button type="button" ng-click="addRandomItem()" class="btn btn-sm btn-success">
      <i class="glyphicon glyphicon-plus">
      </i> Add random item
    </button>
    <table ng-show="edit">
      <tbody>
        <tr>
          <th>first name</th>
          <th>last name</th>
          <th>birth date</th>
          <th>balance</th>
          <th>email</th> 
          <th>action</th>
        </tr>
        <tr>
          <td><input data-ng-model="editrow.firstName" type="text" class="form-control"
                     placeholder="Enter first name" /></td>
          <td><input data-ng-model="editrow.lastName" type="text" class="form-control"
                     placeholder="Enter last name" /></td>
          <td><input data-ng-model="editrow.birthDate" type="text" class="form-control"
                     placeholder="Enter brith date" /></td>
          <td><input data-ng-model="editrow.balance" type="text" class="form-control"
                     placeholder="Enter balance" /></td>
          <td><input data-ng-model="editrow.email" type="text" class="form-control"
                     placeholder="Enter email" /></td>
          <td><button type="button" ng-click="saveItem(editrow)" class="btn btn-sm btn-success">Save</button></td>
        </tr>
      </tbody>
    </table>
    <table st-table="rowCollection" class="table table-striped">
      <thead>
        <tr>
          <th>first name</th>
          <th>last name</th>
          <th>birth date</th>
          <th>balance</th>
          <th>email</th>
          <th>edit</th>
          <th>delete</th>
        </tr>
      </thead>
      <tbody>
        <tr ng-repeat="row in rowCollection">
          <td>{{row.firstName}}</td>
          <td>{{row.lastName}}</td>
          <td>{{row.birthDate | date:'shortDate'}}</td>
          <td>{{row.balance}}</td>
          <td>{{row.email}}</td>
          <td>
            <button type="button" ng-click="editItem(row)" class="btn btn-sm btn-danger">
              <i class="glyphicon glyphicon-remove-circle">
              </i>
            </button>
          <td>
            <button type="button" ng-click="removeItem(row)" class="btn btn-sm btn-danger">
              <i class="glyphicon glyphicon-remove-circle">
              </i>
            </button>
          </td>
        </tr>
      </tbody>
    </table>
  </body>
</html>

关于通过外部控制进行 AngularJS 智能表过滤,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29261957/

相关文章:

javascript - 如何在智能表中按日期对项目进行排序

javascript - 选择所有行无法与复选框一起正常工作

javascript - $提供超过 1 个值以在描述中进行测试

angularjs - 从 firebase 中删除项目

javascript - Angular ui-router不扩展父 View

javascript - $watch 只触发一次

javascript - 将 json 模式转换为 Angular 树控制树模型

javascript - ng-repeat 中的 ng-model - 初始化唯一范围属性

angularjs - 智能表 : Use Bootstrap Datetime-Picker with AngularJs as Filter

javascript - 如何使用复选框来过滤 Angular 智能表中的数据?