【问题标题】:Search filter in angular jsAngular js中的搜索过滤器
【发布时间】:2019-12-14 11:51:37
【问题描述】:

我正在尝试使用 angularjs 为以下应用程序实现搜索过滤器。但它没有按预期工作。我是新手,所以我不确定我在这里做错了什么。有人可以帮忙吗?

到目前为止,这是我的代码:-

index.html 文件:

<html>    
   <head>
      <meta charset="utf-8">
      <meta name="viewport" content="width=device-width, initial-scale=1">
      <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
      <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
      <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
      <script src="Controller/app.js"></script>
      <script src="Controller/storage.js"></script>    
   </head>
   <body ng-app="kfgPm">
    <div ng-controller="MainCtrl" class="container">
        <form name="kfgPmForm" ng-submit="submitForm(kfgPmForm.$valid)" novalidate>

            <div class="col-sm-12" class="form-horizontal" role="form">
                <div class="form-group col-sm-6">
                    <label for="projectID" class="col-sm-3 col-form-label">Project ID: </label>
                    <div class="col-sm-9">
                        <input type="text" class="form-control" id="projectID" required ng-model="itm.projectID">
                    </div>
                </div>

                <div class="form-group col-sm-6">
                    <label for="projectName" class="col-sm-3 col-form-label">Project Name: </label>
                    <div class="col-sm-9">
                        <input type="text" class="form-control" id="projectName" required ng-model="itm.projectName">
                    </div>
                </div>
            </div>

            <div class="col-sm-12" class="form-horizontal" role="form">
                <div class="form-group col-sm-6">
                    <label for="projectOwner" class="col-sm-3 col-form-label">Project Owner: </label>
                    <div class="col-sm-9">
                        <input type="text" class="form-control" id="projectOwner" required ng-model="itm.projectOwner">
                    </div>
                </div>

                <div class="form-group  col-sm-6">
                    <label for="keyStake" class="col-sm-3 col-form-label">Key Stakeholders: </label>
                    <div class="col-sm-9">
                        <input type="text" class="form-control" id="keyStake" required ng-model="itm.keyStake">
                    </div>
                </div>
            </div>

            <div class="col-sm-12" class="form-horizontal" role="form">
                <div class="form-group col-sm-6">
                    <label for="prepBy" class="col-sm-3 col-form-label">Prepared By: </label>
                    <div class="col-sm-9">
                        <input type="text" class="form-control" id="prepBy" required ng-model="itm.prepBy">
                    </div>
                </div>

                <div class="form-group  col-sm-6">
                    <label for="reqDate" class="col-sm-3 col-form-label">Requested Date: </label>
                    <div class="col-sm-9">
                        <input type="text" class="form-control" id="reqDate" required ng-model="itm.reqDate">
                    </div>
                </div>
            </div>

            <div class="col-sm-12" class="form-group" ng-submit="submitDetails()" role="form">
                <div class="form-group col-sm-6" class="input-group mb-3">
                    <label for="inputGroupSelect01" class="col-sm-3 col-form-label">Status: </label>
                    <div class="col-sm-9">
                        <select name="status" class="form-control custom-select" ng-options="user.option for user in arrlist" required ng-model="user.itm.status">
                                        <option value="">Select..</option>
                                       </select>
                        </select>
                    </div>
                </div>

                <div class="form-group col-sm-6">
                    <label for="dept" class="col-sm-3 col-form-label">Department: </label>
                    <div class="col-sm-9">
                        <input type="text" class="form-control" id="dept" required ng-model="itm.dept">
                    </div>
                </div>
            </div>


            <div class="col-sm-12" class="form-group purple-border">

                <div class="col-sm-2">
                    <label for="projSummary">Project Summary: </label>
                </div>
                <div class="col-sm-10">
                    <textarea class="form-control" id="projSummary" required ng-model="itm.projSummary" rows="3"></textarea>
                </div>
            </div>
        </form>


        <div class="form-row text-center">
            <div class="col-12">
                <button ng-disabled="kfgPmForm.$invalid" ng-click="update(itm)" class="btn btn-info">SUBMIT</button>
                <div><br></div>
                </form>
                <div><br></div>


                <div class="col-sm-12" class="form-horizontal">
                    <label for="search" class="col-sm-3 col-form-label">Search: </label>
                    <div class="col-sm-6">
                        <input ng-model="searchText" class="form-control" ng-keyup="filterFunc()">
                        <div><br></div>
                    </div>
                </div>

                <div class="results">

                    <table class="table table-bordered table-responsive-md table-striped text-center">
                        <thead class="thead-light">
                            <tr>
                                <th>Project ID</th>
                                <th>Project Name</th>
                                <th>Project Owner</th>
                                <th>Key Stakeholders</th>
                                <th>Prepared By</th>
                                <th>Requested Date</th>
                                <th>Status</th>
                                <th>Department</th>
                                <th>Project Summary</th>
                                <th>ACTIONS</th>
                            </tr>
                        </thead>
                        <tbody>

                            <tr ng-repeat="itm in master | filter: itm.search">
                                <td><span ng-hide="editMode">{{itm.projectID}}</span>
                                    <input type="text" ng-show="editMode" ng-model="itm.projectID" />
                                </td>
                                <td><span ng-hide="editMode">{{itm.projectName}}</span>
                                    <input type="text" ng-show="editMode" ng-model="itm.projectName" />
                                </td>
                                <td><span ng-hide="editMode">{{itm.projectOwner}}</span>
                                    <input type="text" ng-show="editMode" ng-model="itm.projectOwner" />
                                </td>
                                <td><span ng-hide="editMode">{{itm.keyStake}}</span>
                                    <input type="text" ng-show="editMode" ng-model="itm.keyStake" />
                                </td>
                                <td><span ng-hide="editMode">{{itm.prepBy}}</span>
                                    <input type="text" ng-show="editMode" ng-model="itm.prepBy" />
                                </td>

                                <td><span ng-hide="editMode">{{itm.reqDate}}</span>
                                    <input type="text" ng-show="editMode" ng-model="itm.reqDate" />
                                </td>
                                <td><span ng-hide="editMode">{{itm.status.option}}</span>
                                    <input type="text" ng-show="editMode" ng-model="itm.status" />
                                </td>
                                <td><span ng-hide="editMode">{{itm.dept}}</span>
                                    <input type="text" ng-show="editMode" ng-model="itm.dept" />
                                </td>
                                <td><span ng-hide="editMode">{{itm.projSummary}}</span>
                                    <input type="text" ng-show="editMode" ng-model="itm.projSummary" />
                                </td>

                                <td>
                                    <button ng-click="EditProject(itm)" class="btn btn-primary">Edit</button>
                                    <button ng-hide="editMode" ng-click="removeItem($index)" class="btn btn-danger">Delete</button>
                                </td>
                            </tr>
                        </tbody>
                    </table>
                </div>

            </div>
            <br>
        </div>


</body>

</html>

这是我的 app.js 文件:

var isHtml5Compatible = document.createElement('canvas').getContext != undefined;
if (isHtml5Compatible) {
    initiateLocalStorage();
}

function initiateLocalStorage() {
    var app = angular.module('kfgPm', ['storageService']);    
    app.controller('MainCtrl', ['$scope', 'getLocalStorage', function($scope, getLocalStorage) {    
        $scope.EditProject = EditProject;
        $scope.master = getLocalStorage.getData();

        $scope.master = [];

        $scope.update = function() {
            var IsNew = true; //if the data entered in the field is new 
            angular.forEach($scope.master, function(LItem, key) {
                if (LItem.projectID == $scope.itm.projectID) { //if the new project ID equals old project ID
                    IsNew = false; //data entered is to be edited
                    LItem.projectID = $scope.itm.projectID;
                    LItem.projectName = $scope.itm.projectName;
                    LItem.projectOwner = $scope.itm.projectOwner;
                    LItem.keyStake = $scope.itm.keyStake;
                    LItem.prepBy = $scope.itm.prepBy;
                    LItem.reqDate = $scope.itm.reqDate;
                    LItem.status = $scope.itm.status;
                    LItem.dept = $scope.itm.dept;
                    LItem.projSummary = $scope.itm.projSummary;                       
                }
            });
            if (IsNew) { //if new data
                $scope.master.push({ //add to the fields
                    'projectID': $scope.itm.projectID,
                    'projectName': $scope.itm.projectName,
                    'projectOwner': $scope.itm.projectOwner,
                    'keyStake': $scope.itm.keyStake,
                    'prepBy': $scope.itm.prepBy,
                    'reqDate': $scope.itm.reqDate,
                    'status': $scope.itm.status,
                    'dept': $scope.itm.dept,
                    'projSummary': $scope.itm.projSummary,                        
                });
            }
            getLocalStorage.update($scope.master);
            $scope.itm.projectID = '';
            $scope.itm.projectName = '';
            $scope.itm.projectOwner = '';
            $scope.itm.keyStake = '';
            $scope.itm.prepBy = '';
            $scope.itm.reqDate = '';
            $scope.itm.status = '';
            $scope.itm.dept = '';
            $scope.itm.projSummary = '';              
        },

        $scope.removeItem = function(index) {
            console.log(index);
            $scope.master.splice(index, 1)
            getLocalStorage.update($scope.master);
        },

        $scope.editItem = function(index) {
            getLocalStorage.update($scope.master);
            $scope.editing = $scope.master.indexOf(index);
        }

        function EditProject(pItem) { //if edit is clicked the data is replaced in respective fields
            $scope.itm.projectID = pItem.projectID;
            $scope.itm.projectName = pItem.projectName;
            $scope.itm.projectOwner = pItem.projectOwner;
            $scope.itm.keyStake = pItem.keyStake;
            $scope.itm.prepBy = pItem.prepBy;
            $scope.itm.reqDate = pItem.reqDate;
            $scope.itm.status = pItem.status;
            $scope.itm.dept = pItem.dept;
            $scope.itm.projSummary = pItem.projSummary;
            console.log(pItem);
        }

        $scope.arrlist = [{
            "id": 1,
            "option": "One"
        }, {
            "id": 2,
            "option": "Two"
        }];
        $scope.userselected = $scope.arrlist[1];               
        $scope.LItem = angular.copy($scope.update);

        $scope.filterFunc = function() {
            $scope.LItem = $filter('filter')($scope.update, { $: $scope.searchText });
        }
        $scope.submitForm = function(isValid) {
            if (isValid) {
                alert('Submitted Successfully');
            }
        };
    }]);    
}

我正在尝试实现对所有列的搜索,这样当我在搜索文本字段中键入内容时,它应该只返回包含这些搜索词的行,而其余行将隐藏在表格中。

【问题讨论】:

  • 消除任何与问题无关的问题。如果您的问题不是 HTML 错误,请确保没有 HTML 错误。 描述问题。“它没有按预期工作”的描述性不足以帮助人们理解您的问题。相反,告诉其他读者预期的行为应该是什么。 最少 尽可能少地使用仍然会产生相同问题的代码。要处理的代码越多,人们就越不可能找到您的问题。

标签: javascript angularjs angularjs-directive


【解决方案1】:

首先,我看到以下问题:

<body ng-app="kfgPm">
<div ng-controller="MainCtrl" class="container">
    <form name="kfgPmForm" ng-submit="submitForm(kfgPmForm.$valid)" novalidate>

        <!-- duplicate class attribute -->
        <div class="col-sm-12" class="form-horizontal" role="form">

            <!-- end tag seen too early -->
            </div>
        </div>
  1. 重复的类属性 -- 第二个将被忽略
  2. 过早看到结束标记 - 控制器的范围将受到限制

仅这两个错误就会导致应用失败。

还有angular.jsbootstrap.js 不能一起玩。他们不协调他们对 DOM 的操作。

【讨论】:

  • 很抱歉这不是一个重复的类属性,这是一个编辑错误。我现在已经更正了我的代码。你现在可以检查吗?我认为 app.js 文件中存在一些错误,但我无法弄清楚。
  • 这些并不是 HTML 中唯一的错误。要查看更多信息,请查看此 DEMO on PLNKR 中的代码编辑器。
猜你喜欢
  • 2017-04-28
  • 1970-01-01
  • 1970-01-01
  • 2016-02-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-06
  • 2021-11-08
相关资源
最近更新 更多