Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angularjs 自定义角度过滤器功能_Angularjs - Fatal编程技术网

Angularjs 自定义角度过滤器功能

Angularjs 自定义角度过滤器功能,angularjs,Angularjs,我正在努力写简单的逻辑。 请看一看,看你是否能帮助我 我有下面的代码,它根据im从服务器返回的数据显示了当前UI上的三个表 我有一个搜索框,可以过滤每个表中的数据。它也工作得很好 我的要求: 当根据输入在所有三个表中都找不到数据时,我需要显示一条通用消息“找不到数据” <div> <input type="text" ng-model="searchStudent"> <div> <div ng-repeat="studentPermissio

我正在努力写简单的逻辑。 请看一看,看你是否能帮助我

我有下面的代码,它根据im从服务器返回的数据显示了当前UI上的三个表

我有一个搜索框,可以过滤每个表中的数据。它也工作得很好

我的要求: 当根据输入在所有三个表中都找不到数据时,我需要显示一条通用消息“找不到数据”

<div>
 <input type="text" ng-model="searchStudent">
<div>
    <div ng-repeat="studentPermissions in studentPermissions">
        <div ng-repeat="student in studentPermissions.entities">
            <table>
                <thead>
                    <tr>
                        <td >Student</td>
                        <td>{{student.StudentName}}</td>
                    </tr>
                    <tr>
                        <th></th>
                        <th ng-repeat="permission in student.entityStudents[0].userPermissions"><div><span>{{permission.Name}}</span></div></th>
                    </tr>
                </thead>
                <tbody>
                    <tr ng-repeat="student in student.entityStudents | filter:searchStudent">
                        <td>{{student.FirstName}} {{student.LastName}}</td>
                        <td ng-repeat="permission in student.userPermissions">
                            <input ng-model="permission.Checked" type="checkbox">
                        </td>
                    </tr>
                </tbody>
            </table>
        </div>
    </div>
</div>
我试着设置国旗开/关的技巧,但每次都出了问题

我的样本数据:

$scope.studentPermissions  = [ 
{entities[{StudentName: 'Tester', entityStudents[{FirstName: 'Test0', LastName: 'Test0', userPermissions[{prop 1, prop2, prop3}]}]}]},
{entities[{StudentName: 'Tester', entityStudents[{FirstName: 'Test1', LastName: 'Test2', userPermissions[{prop 1, prop2, prop3}]}]}]},
{entities[{StudentName: 'Tester', entityStudents[{FirstName: 'Test3', LastName: 'Test4', userPermissions[{prop 1, prop2, prop3}]}]}]},
]

您可以使用ngIf/ngShow/ngHide/ngClass进行此操作,但您必须稍微更改编写重复的方式。例如,您可以这样写:

ng-repeat="student in filteredData = (student.entityStudents | filter:searchStudent)">
 
您还可以执行以下操作:

ng-repeat="student in student.entityStudents | filter:searchStudent as filteredData"
因此,现在您有了对filteredData的引用,您可以在ngShow/Hide/If/Class中使用filteredData.length作为布尔值。如果长度为0,则为falsy:

<p ng-hide="filteredData.length">No Data Found</p> 

未找到任何数据

这是一个有效的演示,我认为它符合您的要求,这是我所能告诉您的。请注意,我将您的筛选器移动到了第一个嵌套的repeat,以便在表不匹配时完全隐藏,并且您只有一条“未找到数据”消息


更新了你在谈论哪三张桌子?我在发布的代码中只看到一个表。您想知道筛选的学生集合何时为空吗?@JB Nizet,有一个外部ng重复,即。。所以表的数量取决于外部ng重复。使用当前数据,我在UI上创建了3个表。@immirza代码中有一个过滤器-过滤器:searchStudent。基于此,您想显示通用消息吗?您好,更新对您有效吗?@jme11不是真的,我正试图为您创建具有确切数据的plnkr,以便您能够正确地了解。我将很快恢复。这很好,但问题是它显示重复的消息。如果您在我的代码中看到,我有一个外部ng repeat,它创建了多个表。因此,

在每次迭代outerloop时都不会重复找到任何数据。你明白我的意思了吗?然而,如果在每个表中都找不到数据,我只需要显示一次消息。我认为您没有抓住要点。您不必在repeat中使用filteredData。和其他变量一样。你可以在你想在页面上的任何地方使用它。如果要在结果为空时隐藏整个表,没有问题,只需向表中添加一个ng hide,或使用ngClass添加一个类。我将为您做一个示例。@jme11,噢,我想filterData的作用域就在outer ng repeat内。我不知道它可以在html文件中的任何地方访问。早些时候,我在外部ng repeat作用域之外使用,但它不起作用。我的理解是,filterData只是一个全局变量?对吗?在这里,我刚刚修改了文档中的一个演示:。好吧,从技术上讲,不是一个全球性的。你不能在页面上的“任何地方”使用它。我应该写得更好。您可以在同一范围内的任何地方使用它。因此,如果页面上有两个单独作用域的控制器,并且重复在一个控制器中,则只能在该视图中使用它,而不能在另一个控制器的作用域视图中使用它。

<p ng-hide="filteredData.length">No Data Found</p>