简体   繁体   English

添加控制到ng-repeat以过滤数据

[英]Add controls to ng-repeat to filter data

I have some json data that I output with ng-repeat and I have added some form controls to filter data output from another json. 我有一些json数据,我用ng-repeat输出,我添加了一些表单控件来过滤另一个json的数据输出。

Simplified sample 简化样本

First JSON data: 第一个JSON数据:

var technologies = [
  {"id":"5", "slug":"mysql", "label":"MySQL", "category":"database"},
  {"id":"4", "slug":"html", "label":"HTML", "category":"markup"}
]

and the output: 和输出:

<ul>
  <li ng-repeat="tech in technologies">
    <span>{{tech.label}}</span> 
    <span><label>required expertise 
      <select>
        <option>1</option>
        <option>2</option>
        <option>3</option>
        <option>4</option>
        <option>5</option>
      </select></label>
    </span>
  </li>
</ul>

(actually, the technologies are at this point already filtered from another choice that the user makes on a different page, but I assume that makes no difference) (实际上,此时的技术已经从用户在不同页面上做出的另一个选择中过滤掉了,但我认为这没有区别)

The 2nd json contains the expertise property that I want to use as a filter 第二个json包含我想用作过滤器的专业属性

var people = [
{
  'label': 'MySQL',
  'slug': 'mysql',
  'talents':[
    {
      'name': 'Stanislav Lem',
      'expertise': 5,
      'desire': 0,
      'last_used': '2009-01'
    },
    {
      'name': 'Ijon Tichy',
      'expertise': 1,
      'desire': 5,
      'last_used': '2011-06'
    }
  ]
},  ...
]

...and that is for now being plain used as ......现在这被用作了

<ul>
  <li ng-repeat="item in people">
    {{item.label}}<br>
    <ul>
      <li ng-repeat="person in item.talents">{{person.name}} &middot; Expertise: {{person.expertise}}</li>
    </ul>
  </li>
</ul>

What I need is a filter that uses the options from the first output to filter the second output. 我需要的是一个过滤器,它使用第一个输出中的选项来过滤第二个输出。 For example when MySQL required expertise is set to '2', only Stanislav Lem will be displayed. 例如,当MySQL要求的专业知识设置为“2”时,只显示Stanislav Lem。 Actually I will also need a 'no results found' message if the noone matches the expertise, but I guess I can figure that part out myself. 实际上,如果没人匹配专业知识,我还需要一个“没有找到结果”的消息,但我想我可以自己解决这个问题。

Plunker sample is here: http://plnkr.co/edit/B0aQp9aCJ2g4OM6fZ3qe?p=preview Plunker样本在这里: http ://plnkr.co/edit/B0aQp9aCJ2g4OM6fZ3qe?p=preview

Working Plunker: http://plnkr.co/edit/jcb6SfKVPkwt7FFCZFHX?p=preview Working Plunker: http ://plnkr.co/edit/jcb6SfKVPkwt7FFCZFHX?p =preview

The first thing you'll need to do is add an ng-model attribute to the select . 您需要做的第一件事是在select添加ng-model属性。 I've added this as a property of the technology so you can easily find it. 我已将此添加为该技术的属性,因此您可以轻松找到它。

<select ng-model="tech.required">

Next, I added the following two functions to your controller to aid in filtering the second list: 接下来,我向控制器添加了以下两个函数,以帮助过滤第二个列表:

$scope.getFilterForSlug = function(slug) {
    var technology = getTechBySlug(slug);

    return function(person) {
        return person.expertise >= (technology.required || 0);
    }
}

function getTechBySlug(slug) {
    return _.find($scope.technologies, {
        slug: slug
    });
}

I'm using lodash in getTechBySlug , as the name states, to get the correct technology object based on the slug. 正如名称getTechBySlug ,我在getTechBySlug中使用lodash,以获取基于slug的正确技术对象。 Using that, getFilterForSlug returns a function which compares the person's expertise level with the desired level from the select . 使用它, getFilterForSlug返回一个函数,该函数将人的专业水平与select所需的级别进行比较。 If an option wasn't selected, then the desired level is set to 0. 如果未选择选项,则将所需级别设置为0。

Lastly, in the HTML, I added the following: 最后,在HTML中,我添加了以下内容:

<ul>
    <li ng-repeat="person in filteredPeople = (item.talents | filter: getFilterForSlug(item.slug))">{{person.name}} · Expertise: {{person.expertise}}</li>
</ul>
<span ng-if='!filteredPeople.length'>No Results!</span>

This code gets a filter based on the current slug, and then filters the people using that function. 此代码根据当前slug获取过滤器,然后使用该函数过滤人员。 It stores the results in a variable filteredPeople , which is used in the span to display a message when there are no results. 它将结果存储在变量filteredPeople ,该变量在span用于在没有结果时显示消息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM