AngularJs的筛选器对某些值不起作用。

huangapple go评论56阅读模式
英文:

AngularJs filter for few values doesn't work

问题

我正在使用AngularJS进行工作,一切都运行得很顺利,但当我搜索薪水为30的人时(这只是一个示例),它不起作用,如果我添加一个更多的值,它就开始工作了,有人可以解释一下问题出在哪里吗?

Plunker链接https://plnkr.co/edit/bCz8jq8KSd7XCk2WmHo1?p=info

<!DOCTYPE html>
<html lang="en" ng-app="myModule">
<head>
    <title>Document</title>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.7.9/angular.min.js"></script>
    <script src="script.js"></script>
</head>
<body ng-controller="myController">
    Rows to display : <input type="number" step="1" ng-model="rowLimit" max="5" min="0" />
    Sort By :
    <select ng-model="sortColumn">
        <option value="name">Name ASC</option>
        <option value="+dateOfBirth">Date of Birth ASC</option>
        <option value="+gender">Gender ASC</option>
        <option value="-salary">Salary DESC</option>
    </select>
    Search : <input type="text" placeholder="Search employees" ng-model="searchText" />
    <!-- Search : <input type="text" placeholder="Search employees" ng-model="searchText.city"/> -->
    Hide Salary: <input type="checkbox" ng-model="hideSalary" />
    <table>
        <thead>
            <tr>
                <th>Firstname</th>
                <th>Lastname</th>
                <th>Date Of Birth</th>
                <th>Gender</th>
                <th>Salary(number)</th>
                <th ng-hide="hideSalary">Salary(currency)</th>
            </tr>
        </thead>
        <tbody>
            <tr ng-repeat="employee in emp | limitTo:rowLimit | orderBy:sortColumn | filter:searchText">
                <!-- <tr ng-repeat="employee in emp | limitTo:rowLimit | orderBy:'-firstName'"> -->
                <td>{{employee.firstName | uppercase}}</td>
                <td>{{employee.lastName}}</td>
                <td>{{employee.DOB | date:"dd-MM-yyyy"}}</td>
                <td>{{employee.gender | lowercase}}</td>
                <td>{{employee.salary | number:2}}</td>
                <td>{{employee.salary | currency:'$'}}</td>
            </tr>
        </tbody>
    </table>
</body>
</html>

script.js

var myApp = angular.module('myModule', []).controller("myController", function ($scope) {
    var emp = [
        {firstName:"Ben",lastName:"Ding",DOB:new Date('December 26, 1991'),gender:"Male",salary:500},
        {firstName:"John",lastName:"Deo",DOB:new Date('December 26, 1991'),gender:"Male",salary:6500.21},
        {firstName:"Rajesh",lastName:"kumar",DOB:new Date('December 26, 1991'),gender:"Male",salary:3500},
        {firstName:"Rashmi",lastName:"singh",DOB:new Date('December 26, 1991'),gender:"female",salary:8500.78},
        {firstName:"soumya",lastName:"kumari",DOB:new Date('December 26, 1991'),gender:"female",salary:30}
    ];
    $scope.emp = emp;
    $scope.rowLimit = 5;
    $scope.sortColumn = "name";
});
英文:

i am working with angularjs filter everything is working smoothly but when i am searching for a person whose salary is 30(just an example) it doesn't work, if i add one more value it starts working can anybody explain me whats wrong with it?

Plunker link https://plnkr.co/edit/bCz8jq8KSd7XCk2WmHo1?p=info

&lt;!DOCTYPE html&gt;
&lt;html lang=&quot;en&quot; ng-app=&quot;myModule&quot;&gt;
&lt;head&gt;
&lt;title&gt;Document&lt;/title&gt;
&lt;script src=&quot;https://ajax.googleapis.com/ajax/libs/angularjs/1.7.9/angular.min.js&quot;&gt;&lt;/script&gt;
&lt;script src=&quot;script.js&quot;&gt;&lt;/script&gt;
&lt;/head&gt;
&lt;body ng-controller=&quot;myController&quot;&gt;
Rows to display : &lt;input type=&quot;number&quot; step=&quot;1&quot; ng-model=&quot;rowLimit&quot; max=&quot;5&quot; min=&quot;0&quot; /&gt;
Sort By :
&lt;select ng-model=&quot;sortColumn&quot;&gt;
&lt;option value=&quot;name&quot;&gt;Name ASC&lt;/option&gt;
&lt;option value=&quot;+dateOfBirth&quot;&gt;Date of Birth ASC&lt;/option&gt;
&lt;option value=&quot;+gender&quot;&gt;Gender ASC&lt;/option&gt;
&lt;option value=&quot;-salary&quot;&gt;Salary DESC&lt;/option&gt;
&lt;/select&gt;
Search : &lt;input type=&quot;text&quot; placeholder=&quot;Search employees&quot; ng-model=&quot;searchText&quot; /&gt;
&lt;!-- Search : &lt;input type=&quot;text&quot; placeholder=&quot;Search employees&quot; ng-model=&quot;searchText.city&quot;/&gt; --&gt;
Hide Salary: &lt;input type=&quot;checkbox&quot; ng-model=&quot;hideSalary&quot; /&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Firstname&lt;/th&gt;
&lt;th&gt;Lastname&lt;/th&gt;
&lt;th&gt;Date Of Birth&lt;/th&gt;
&lt;th&gt;Gender&lt;/th&gt;
&lt;th&gt;Salary(number)&lt;/th&gt;
&lt;th ng-hide=&quot;hideSalary&quot;&gt;Salary(currency)&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr ng-repeat=&quot;employee in emp|limitTo:rowLimit|orderBy:sortColumn | filter : searchText&quot;&gt;
&lt;!-- &lt;tr ng-repeat=&quot;employee in emp|limitTo:rowLimit|orderBy:&#39;-firstName&#39;&quot;&gt; --&gt;
&lt;td&gt;{{employee.firstName | uppercase}}&lt;/td&gt;
&lt;td&gt;{{employee.lastName}}&lt;/td&gt;
&lt;td&gt;{{employee.DOB | date:&quot;dd-MM-yyyy&quot;}}&lt;/td&gt;
&lt;td&gt;{{employee.gender |lowercase}}&lt;/td&gt;
&lt;td&gt;{{employee.salary|number:2}}&lt;/td&gt;
&lt;td&gt;{{employee.salary|currency:&#39;$&#39;}}&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;/body&gt;
&lt;/html&gt;
**script.js** 
var myApp = angular.module(&#39;myModule&#39;, []).controller(&quot;myController&quot;, function ($scope) {
var emp = [
{firstName:&quot;Ben&quot;,lastName:&quot;Ding&quot;,DOB:new Date(&#39;December 26, 1991&#39;),gender:&quot;Male&quot;,salary:500},
{firstName:&quot;John&quot;,lastName:&quot;Deo&quot;,DOB:new Date(&#39;December 26, 1991&#39;),gender:&quot;Male&quot;,salary:6500.21},
{firstName:&quot;Rajesh&quot;,lastName:&quot;kumar&quot;,DOB:new Date(&#39;December 26, 1991&#39;),gender:&quot;Male&quot;,salary:3500},
{firstName:&quot;Rashmi&quot;,lastName:&quot;singh&quot;,DOB:new Date(&#39;December 26, 1991&#39;),gender:&quot;female&quot;,salary:8500.78},
{firstName:&quot;soumya&quot;,lastName:&quot;kumari&quot;,DOB:new Date(&#39;December 26, 1991&#39;),gender:&quot;female&quot;,salary:30}
]
$scope.emp = emp;
$scope.rowLimit = 5;
$scope.sortColumn = name;
});

答案1

得分: 2

DOB 值在未应用筛选器的情况下为 "1991-12-25T18:30:00.000Z",其中包含 30。这就是筛选器未应用的原因。您可以将薪水中的 30 更改为任何其他数字并尝试,它将起作用。日期值将根据当前日期和时间更改。

您可以使用 https://momentjs.com/ 格式化日期值。

英文:

The DOB value without the filter applied is "1991-12-25T18:30:00.000Z" which includes 30 in it. That's the reason the filter is not applied. You can change the salary 30 to any other number and try, it would work. The date value will change based on the current date and time.

You can use https://momentjs.com/ to format the Date value.

huangapple
  • 本文由 发表于 2020年1月3日 18:26:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/59576908.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定