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


Embeer.js ArrayProxy slice()用法及代碼示例


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

slice() 方法用於獲取數組的一部分,即接收器的切片。

用法:

slice( beginIdx, endidx );

參數:

  • beginIdx: 它是切片開始的索引。
  • endIdx: 它是切片結束的結束索引。

返回值:具有指定切片的新數組。

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

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

npm install ember-cli

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

ember new <project-name> --lang en

要啟動服務器,請鍵入:

ember serve

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

ember generate route slice1

應用程序/路線/slice1.js


import Route from '@ember/routing/route'; 
export default class FruitsRoute extends Route { 
    fruits = [ 
        { 
            'name': 'Lady Finger', 
            'isFruit': false, 
            'color': 'green', 
            'quant': 2,  
        }, 
        { 
            'name': 'Brinjal', 
            'isFruit': false, 
            'color': 'purple', 
            'quant': 2, 
        }, 
        { 
            'name': 'Apple', 
            'isFruit': true, 
            'color': 'red', 
            'quant': 5, 
        }, 
        { 
            'name': 'Grapes', 
            'isFruit': true, 
            'color': 'green', 
            'quant': 4, 
        }, 
        { 
            'name': 'Mango', 
            'isFruit': true, 
            'color': 'yellow', 
            'quant': 3, 
        }, 
        { 
            'name': 'Watermelon', 
            'isFruit': true, 
            'color': 'red', 
            'quant': 2, 
        }, 
        { 
            'name': 'Orange', 
            'isFruit': true, 
            'color': 'orange', 
            'quant': 2, 
        }, 
        { 
            'name': 'Banana', 
            'isFruit': true, 
            'color': 'yellow', 
            'quant': 5, 
        }, 
        { 
            'name': 'Papaya', 
            'isFruit': true, 
            'color': 'orange', 
            'quant': 5, 
        }, 
    ]; 
    item2; 
    item3; 
    model(){ 
        return this.fruits; 
    } 
    setupController(controller,model) { 
        super.setupController(controller, model); 
        controller.set('fruits', this.fruits); 
        controller.set('item1', this.item1); 
        controller.set('item2', this.item2); 
        controller.set('item3', this.item3); 
    } 
}

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


import Ember from "ember"; 
  
export default Ember.Controller.extend({ 
actions: { 
    show_Fruits( ) { 
        let temp = this.fruits.rejectBy('isFruit',false); 
        let foo = temp.mapBy('name'); 
        alert(foo.join('\n')) 
    }, 
    slice_items( idx1, idx2) { 
        let foo = this.fruits.slice(idx1, idx2); 
        let ans = foo.mapBy('name') 
        alert(ans.join('\n')) 
    }, 
    remove() { 
        this.fruits.shiftObject( ); 
          
    }, 
    set_quant(data) { 
        this.fruits.setEach('quant', data ); 
          
    }, 
    select(){ 
        let temp = this.fruits.uniqBy('color'); 
        let ans = temp.mapBy('name'); 
        alert(ans.join('\n')); 
    } 
}, 
});

應用程序/模板/slice1.hbs


{{page-title "slice"}} 
<h3>Here is a list of eatables: </h3> 
<table> 
    <tr> 
        <th>Name</th> 
        <th>Fruit</th> 
        <th>Color</th> 
        <th>Quantity</th> 
    </tr> 
    {{#each @model as |detail|}} 
    <tr> 
        <td>{{detail.name}}</td> 
        <td>{{detail.isFruit}}</td> 
        <td>{{detail.color}}</td> 
        <td>{{detail.quant}}</td> 
    </tr> 
    {{/each}} 
</table> 
<br /><br /> 
<div> 
    <label>Enter first index: </label> 
    {{input value=this.temp}} 
</div> 
<div> 
    <label>Enter last indexs: </label> 
    {{input value=this.temp2}} 
</div> 
<input 
    type="button"
    id="check-atIndex"
    value="Slice list"
    {{action "slice_items" this.temp this.temp2}} 
/> 
<br/> 
<br/> 
<div> 
    <label>Enter Quantity: </label> 
    {{input value=this.temp3}} 
</div> 
<input 
    type="button"
    id="set-quant"
    value="Set Each Quantity"
    {{action "set_quant" this.temp3}} 
/> 
<br/><br/> 
<div> 
    <input type="button"
    id="removeFruit"
    value="Remove 1's Fruit"
    {{action "remove" }}    > 
</div> 
<br/> 
<div> 
    <input type="button"
    id="addFruit"
    value="Show Fruit"
    {{action "show_Fruits"}}    > 
</div> 
<br/> 
<input 
    type="button"
    id="select-fruits"
    value="Select One item of Each color"
    {{action "select"}} 
/> 
{{outlet}}

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

Ember.js ArrayProxy切片方法

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

ember generate route slice2

應用程序/路線/slice2.js


import Route from "@ember/routing/route"; 
  
export default class StudentsRoute extends Route { 
    students = [ 
        { 
            name: "Aaush Somankar", 
            gender: "M", 
            class: 12, 
            grade: "A", 
            mobile: 7897391833, 
        }, 
        { 
            name: "Aman Prakash", 
            gender: "M", 
            class: 10, 
            grade: "A", 
            mobile: 8293839918, 
        }, 
        { 
            name: "Siya guru", 
            gender: "F", 
            class: 11, 
            grade: "B", 
            mobile: 6293839912, 
        }, 
        { 
            name: "Ramesh verma", 
            gender: "M", 
            class: 10, 
            grade: "A", 
            mobile: 7193839932, 
        }, 
        { 
            name: "Esnoor", 
            gender: "M", 
            class: 9, 
            grade: "F", 
            mobile: 8372819383, 
        }, 
        { 
            name: "Balit", 
            gender: "M", 
            class: 10, 
            grade: "A", 
            mobile: 7361927365, 
        }, 
        { 
            name: "Dora", 
            gender: "F", 
            class: 12, 
            grade: "F", 
            mobile: 5634152617, 
        }, 
        { 
            name: "Lalu", 
            gender: "F", 
            class: 8, 
            grade: "A", 
            mobile: 8372618392, 
        }, 
        { 
            name: "Pokhraj", 
            gender: "M", 
            class: 11, 
            grade: "F", 
            mobile: 9302810231, 
        }, 
        { 
            name: "Rahul", 
            gender: "M", 
            class: 9, 
            grade: "A", 
            mobile: 7837919373, 
  
        }, 
        { 
            name: "Ram", 
            gender: "M", 
            class: 12, 
            grade: "A", 
            mobile: 8272112539, 
        }, 
        { 
            name: "Priti", 
            gender: "F", 
            class: 9, 
            grade: "F", 
            mobile: 8836172912, 
        }, 
        { 
            name: "Sita", 
            gender: "F", 
            class: 8, 
            grade: "A", 
            mobile: 9193830293, 
        }, 
    ]; 
  convert(string) { 
    this.item = string.split(",").map((item) => item.trim()); 
    return this.item; 
}  
    
    model() { 
        return this.students; 
    } 
    setupController(controller, model) { 
        super.setupController(controller, model); 
        controller.set("students", this.students); 
    controller.set("convert", this.convert); 
    controller.set("item", this.item); 
    } 
}

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


import Ember from "ember"; 
  
export default Ember.Controller.extend({ 
    actions: { 
        select(){ 
            let temp = this.students.uniqBy('class'); 
            let ans = temp.mapBy('name') 
            alert(ans.join('\n')) 
        }, 
        hide() { 
            this.students.setEach('mobile','**********') 
              }, 
        allPass() { 
            let tempItems = this.students.rejectBy('grade','F'); 
            let ans = tempItems.mapBy('name') 
            alert(ans.join('\n')) 
              
        }, 
        remove() { 
        this.students.shiftObject(); 
        }, 
        check_item(idx) { 
            let temp = this.students.objectAt(JSON.parse(idx)); 
            alert(temp.name) 
        }, 
    check_items(idx1, idx2) { 
      let temp = this.students.slice(idx1, idx2); 
      let ans = temp.mapBy('name') 
      alert(ans.join('\n')) 
        }, 
          
    }, 
});

應用程序/模板/slice2.hbs


{{page-title "slice"}} 
<h3>List of Students: </h3> 
  
<table> 
    <tr> 
        <th>Name</th> 
        <th>Class</th> 
        <th>Gender</th> 
        <th>Grade</th> 
        <th>Mobile No.</th> 
    </tr> 
    {{#each @model as |student|}} 
    <tr> 
        <td>{{student.name}}</td> 
        <td>{{student.class}}</td> 
        <td>{{student.gender}}</td> 
        <td>{{student.grade}}</td> 
        <td>{{student.mobile}}</td> 
    </tr> 
    {{/each}} 
</table> 
<br /><br /> 
<div> 
    <label>Enter first index: </label> 
    {{input value=this.temp}} 
</div> 
<div> 
    <label>Enter last indexs: </label> 
    {{input value=this.temp2}} 
</div> 
<input 
    type="button"
    id="check-atIndex"
    value="Slice list"
    {{action "check_items" this.temp this.temp2}} 
/> 
<br /><br /> 
<input 
    type="button"
    id="remove-last"
    value="Remove First Student"
    {{action "remove"}} 
/> 
<br /><br /> 
<input 
    type="button"
    id="hide-number"
    value="Hide Mobile Numbers"
    {{action "hide"}} 
/> 
<br /><br /> 
<input 
    type="button"
    id="all-pass"
    value="Show All Passed Students"
    {{action "allPass"}} 
/> 
  
<br /><br /> 
<input 
    type="button"
    id="select-student"
    value="Select One Student Of Each class"
    {{action "select"}} 
/> 
{{outlet}}

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

Ember.js ArrayProxy切片方法

參考:https://api.emberjs.com/ember/2.14/classes/Ember.Array/methods/slice?anchor=slice



相關用法


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