根据特定条件

时间:2018-05-30 10:58:41

标签: jquery html angularjs

我在UI中显示一个动态填充的表,其中包含很少的记录,用于保存用户输入的CSV文件的数据。

在通过UI删除一些记录并选择提交时,以下submit功能与控制器交互并返回List<Object>。这将取代现有的表。

我现在正在尝试突出显示成功状态的行。但徒劳无功。

有人可以指导我缺少什么吗?

// CONTROLLER UPLOAD FILE
uploadApp.controller('uploadFileController', [
    '$scope',
    '$rootScope',
    '$http',
    '$window',
    function($scope, $rootScope, $http, $window) {
        $scope.uploadResult = "";
        $scope.doUploadFile = function() {
            var file = $scope.uploadedFile;
            var url = "/uploadfile";

            var data = new FormData();
            data.append('uploadfile', file);

            var config = {
                transformRequest: angular.identity,
                headers: {
                    'Content-Type': undefined
                }
            }

            $http.post(url, data, config).then(
                function(response) {
                    $rootScope.policyData= response.data;
                });
        };

        $scope.deletRecord = function(RoleID) {
            var index = -1;
            var policyArray = eval($scope.policyData);
            for (var i = 0; i < policyArray.length; i++) {
                if (policyArray[i].RoleID === RoleID) {
                    index = i;
                    break;
                }
            }
            if (index === -1) {
                alert("Something gone wrong");
            }
            $scope.policyData.splice(index, 1);
            console.log(eval($scope.policyData));
        }

        $scope.submit = function(policyData) {
            $http({
                'url': '/updateOverride',
                'method': 'POST',
                'headers': {
                    'Content-Type': 'application/json'
                },
                'data': $scope.policyData
            }).then(function(response) {
                $scope.policyData = response.data;
                console.log(response);
                console.log($scope.policyData);
                for (var i = 0; i < response.data.length; i++) {
                    $("tr:contains(Success)").addClass("highlightRow");
                }

                $("#alert_success").show()
                $window.scrollTo(0, 0);
            })
        };
    }
]);

html中的表格部分

<table id="pasRecordTable" st-table="display_records"
                  st-safe-src="policyData" ng-init="getData()" ng-show="policyData"
                  class="table table-bordered table-striped" ng-controller="uploadFileController">
                  <caption>*PAS - MDM Reject records.</caption>
                  <thead class="thead-dark">
                     <tr>
                        <!-- <th scope="col">#</th> -->
                        <th>Policy Number</th>
                        <th>Source ID</th>
                        <th width="110px">First Name</th>
                        <th>Middle Name</th>
                        <th>Last Name</th>
                        <th>Error description</th>
                        <th>PAS Validated</th>
                        <th>Note</th>
                        <th>Action</th>
                     </tr>
                  </thead>
                  <tbody>
                     <tr st-select-row="row" st-select-mode="multiple"
                        ng-repeat="row in display_records" id="{{row.PolicyNumber}}">
                        <!-- <th scope="row">1</th> -->
                        <td>{{row.PolicyNumber}}</td>
                        <td>{{row.RoleID}}</td>
                        <td>{{row.FirstName}}</td>
                        <td>{{row.MiddleName}}</td>
                        <td>{{row.LastName}}</td>
                        <td>{{row.ErrorDescription}}</td>
                        <td>{{row.PASValidated}}</td>
                        <td>{{row.Note}}</td>
                        <td><button type="button" class="btn btn-default btn-danger"
                           ng-click="deletRecord(row.RoleID)">
                           <i class="fa fa-trash" aria-hidden="true"></i>
                           </button>
                        </td>
                     </tr>
                     <tr>
                        <!-- <td></td> -->
                        <td></td>
                        <td></td>
                        <td></td>
                        <td style="border-bottom: 1px solid #ddd;"></td>
                        <td style="border-bottom: 1px solid #ddd;">
                           <button id="showRecordSubmit" type="submit" class="btn btn-success btn-submit" ng-click="submit(policyData)">Submit</button>
                        </td>
                        <td></td>
                        <td></td>
                        <td></td>
                        <td></td>
                     </tr>
                  </tbody>
               </table>

highlightRow css:

.highlightRow {
  background-color: #DA8C38;
}

提交前Before submit

提交后After submit

2 个答案:

答案 0 :(得分:3)

  1. 您循环浏览数据,但选择器是相同的,因此您可以设置一次。
  2. 作为最佳做法,不要混合jQuery和Angular - 你不知道什么时候会改变视图(html),所以你的代码无论如何都不会工作。
  3. Angular方法是在行上使用ng-class指令:

    &#13;
    &#13;
    angular.module('app', []).controller('ctrl', function($scope) {
      $scope.items = [
        {
          name: 'item1',
          status: 'success'
        },
        {
          name: 'item2',
          status: 'fail'
        },
        {
          name: 'item3',
          status: 'success'
        },
      ];
    });
    &#13;
    .highlightRow {
      background: green;
    }
    &#13;
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
    <div ng-app="app" ng-controller="ctrl">
      <table>
        <tr ng-repeat="item in items" ng-class="{highlightRow: item.status === 'success'}">
          <td ng-bind="item.name"></td>
          <td ng-bind="item.status"></td>
        </tr>
      </table>
    </div>
    &#13;
    &#13;
    &#13;

    在您的代码中,您需要将ng-class指令添加到tr。类似的东西:

    <tr ng-class="{highlightRow: row.Note === 'success'}" st-select-row="row" st-select-mode="multiple" ng-repeat="row in display_records" id="{{row.PolicyNumber}}">
    

答案 1 :(得分:1)

:contains的字符串参数周围添加引号,如下所示:

$("tr:contains('Success')").addClass("highlightRow");

JQuery :contains() selector docs