當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Embeer.js Ember.NativeArray filterBy()用法及代碼示例


Ember.js 是一個開源 JavaScript 框架,用於開發基於 Model-View-Controller (MVC) 架構的大型客戶端 Web 應用程序。 Ember.js是使用最廣泛的前端應用框架之一。它的目的是加速開發並提高生產力。目前,它被大量網站使用,包括 Square、Discourse、Groupon、Linked In、Live Nation、Twitch 和 Chipotle。

filterBy() 方法用於按屬性名稱和可選值過濾數組。

用法:

filterBy(key,value);

參數:

  • key:這是我們要過濾的屬性的名稱。
  • value: 它是與屬性匹配的可選值。

返回值:該方法的返回值是一個過濾後的數組。

安裝和運行 Ember.js 的步驟:

第 1 步:要運行以下示例,您需要有一個 ember 項目。要創建一個,您需要先安裝ember-cli。在終端中寫入以下代碼:

npm install ember-cli

第 2 步:現在您可以通過輸入以下代碼來創建項目:

ember new <project-name> --lang en

要啟動服務器,請鍵入:

ember serve

示例 1:鍵入以下代碼以生成本示例的路由:

ember generate route filterBy1

應用程序/路線/filterBy1.js

Javascript


import Route from '@ember/routing/route';
import EmberObject from '@ember/object';
import { } from '@ember/array';
export default class FruitsRoute extends Route {
    fruits = [
        EmberObject.create({
            name: 'Lady Finger',
            isFruit: false, color: 'green'
        }),
        EmberObject.create({
            name: 'Brinjal',
            isFruit: false, color: 'purple'
        }),
        EmberObject.create({
            name: 'Apple',
            isFruit: true, color: 'red'
        }),
        EmberObject.create({
            name: 'Grapes',
            isFruit: true, color: 'green'
        }),
        EmberObject.create({
            name: 'Mango',
            isFruit: true, color: 'yellow'
        }),
        EmberObject.create({
            name: 'Watermelon',
            isFruit: true, color: 'red'
        }),
        EmberObject.create({
            name: 'Orange',
            isFruit: true, color: 'orange'
        })
    ];
    model() {
        return this.fruits;
    }
    setupController(controller, model) {
        super.setupController(controller, model);
        controller.set('fruits', this.fruits);
    }
}

應用程序/控製器/filterBy1.js

Javascript


import Ember from "ember";
import { isAny, isEvery } from "@ember/array";
export default Ember.Controller.extend({
    actions: {
        check_item(data) {
            let ans = this.fruits.find((item)=>
             item.name == data)
            alert(ans ? "Yes item present in list"
                : "No Item is not present in list");
        },
        checkVegy() {
            let ans = this.fruits.any((item)=> 
            item.isFruit == false);
            alert(ans ? "Yes Vegetable present in list"
                : "No Vegetable present in list");
        },
        Fruits() {
            let res = '';
            let ans = this.fruits.filterBy('isFruit', true);
            let temp = ans.forEach((item)=>
             res += item.getProperties('name').name + '\n')
            alert(res)
        },
        print() {
            let ans = this.fruits.getEach('name');
            alert(ans.join('\n'));
        },
    },
});

應用程序/模板/filterBy1.hbs

Javascript


{{page-title "filterBy"}}
<h3>Here is a list of eatables: </h3>
<ul>
    {{#each this.fruits as |eatable|}}
        <li>
            {{eatable.name}}
        </li>
    {{/each}}
</ul>
<br />
<div>
    <label>Enter item:</label>
    {{input value=this.temp1}}
</div>
<div>
    <input
        type="button"
        id="check_item"
        value="Check Item in list"
        {{action "check_item" this.temp1}}
    />
</div>
<br />
<input
    type="button"
    id="fruit-show"
    value="Check Fruits"
    {{action "showFruit"}}
/>
<br /><br />
<input
    type="button"
    id="check-vegy"
    value="Any Vegetable in List"
    {{action "checkVegy"}}
/>
<br /><br />
<input type="button"
       id="check-Fruits"
       value="Find Fruit"
       {{action "Fruits"}} />
<br /><br />
<input
    type="button"
    id="check-NonFruits"
    value="List all Items"
    {{action "print"}}
/>
{{outlet}}

輸出:訪問 localhost:4200filterBy/1 查看輸出

Ember.js Ember.NativeArray filterBy 方法

示例 2:鍵入以下代碼以生成本示例的路由:

ember generate route filterBy2

應用程序/路線/filterBy2.js

Javascript


import Route from "@ember/routing/route";
import EmberObject from '@ember/object';
import EmberResolver from "ember-resolver";
export default class DetailsRoute
    extends Route {
    details = [
        EmberObject.create({
            name: "Anubhav",
            mobile: "1298119967",
            city: "Patna",
            country: "India",
            gender: "M",
            zipCode: "800020",
        }),
        EmberObject.create(
            {
                name: "Yogesh",
                mobile: "1234567890",
                city: "Raipur",
                country: "India",
                gender: "M",
                zipCode: "402001",
            }),
        EmberObject.create(
            {
                name: "Satyam",
                mobile: "2222222222",
                city: "Delhi",
                country: "India",
                gender: "M",
                zipCode: "110012",
            }),
        EmberObject.create(
            {
                name: "Shivam",
                mobile: "1122113322",
                city: "Patna",
                country: "India",
                gender: "M",
                zipCode: "530068",
            }),
        EmberObject.create(
            {
                name: "Ayushi",
                mobile: "2244668800",
                city: "Jaipur",
                country: "India",
                gender: "F",
                zipCode: "302001",
            }),
    ];
    city;
    start;
    end;
    model() {
        return this.details;
    }
    setupController(controller, model) {
        super.setupController(controller, model);
        controller.set("details", this.details);
        controller.set("city", this.city);
        controller.set("start", this.start);
        controller.set("end", this.end);
    }
}

應用程序/控製器/filterBy2.js

Javascript


import Ember from "ember";
import { getProperties } from '@ember/object';
export default Ember.Controller.extend({
    actions: {
        getNames() {
            let ans = this.details.getEach('name');
            alert(ans.join('\n'));
        },
        get_city(data) {
            let ans = this.details.filterBy('city', data);
            let temp = ans.mapBy('name')
            alert(temp ? `${temp} from ${data}`
                : `No one from ${data}`)
        },
        get_emp(data) {
            let temp = '';
            let ans = this.details.forEach((item) =>
                temp += JSON.stringify(item.getProperties('zipCode'))
                + '\n')
            alert(temp)
        },
        checkFemale() {
            let res = this.details.any((emp) =>
                emp.gender == 'F');
            alert(res ? `Female employee present in List`
                : `No Female present`);
        },
    },
});

應用程序/模板/filterBy2.hbs

Javascript


{{page-title "filterBy"}}
<h3>List of People: </h3>
<br /><br />
<table>
    <tr>
        <th>Name</th>
        <th>Gender</th>
        <th>Mobile</th>
        <th>City</th>
        <th>Country</th>
        <th>Zip Code</th>
    </tr>
    {{#each @model as |detail|}}
        <tr>
            <td>{{detail.name}}</td>
            <td>{{detail.gender}}</td>
            <td>{{detail.mobile}}</td>
            <td>{{detail.city}}</td>
            <td>{{detail.country}}</td>
            <td>{{detail.zipCode}}</td>
        </tr>
    {{/each}}
</table>
<br />
<div>
    <label>Enter City:</label>
    {{input value=this.temp1}}
</div>
<div>
    <input
        type="button"
        id="check_city"
        value="Get Employee"
        {{action "get_city" this.temp1}}
    />
</div>
<br />
<div>
    <input
        type="button"
        id="get_zip"
        value="Get All zipCode"
        {{action "get_emp"}}
    />
</div>
<br />
<div>
    <input
        type="button"
        id="check-female"
        value="Check Female"
        {{action "checkFemale"}}
    />
</div>
<br />
<input
    type="button"
    id="get-names"
    value="Get All names from list"
    {{action "getNames"}}
/>
{{outlet}}

輸出:訪問 localhost:4200/filterBy2 查看輸出

Ember.js Ember.NativeArray filterBy 方法

參考:https://api.emberjs.com/ember/4.4/classes/Ember.NativeArray/methods/filterBy?anchor=filterBy



相關用法


注:本文由純淨天空篩選整理自satyam00so大神的英文原創作品 Ember.js Ember.NativeArray filterBy() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。