簡體   English   中英

使用Ember.js自動完成

[英]Autocomplete using Ember.js

我一直無法想出這個......

我想構建一個自動完成框,根據搜索顯示聯系人列表。 我目前為用戶提供此控制器:

App.UsersController = Ember.ArrayController.extend({
        sortProperties: ['firstName'],
        sortAscending: true,
        isCreateUser: false,
        sortByDigital: false,
        sortByPhysical: false,
        sortDisplayAll: true,
        createUser: function() {
            this.set('isCreateUser', true);
        },

        motoDigital: function() {
            this.set('sortByDigital', true);
            this.set('sortByPhysical', false);
            this.set('sortDisplayAll', false);
        },
        motoPhysical: function() {
            this.set('sortByDigital', false);
            this.set('sortDisplayAll', false);
            this.set('sortByPhysical', true);
        },
        displayAll: function() {
            this.set('sortByDigital', false);
            this.set('sortDisplayAll', true);
            this.set('sortByPhysical', false);
        },
        searchText: null,

        searchResults: function() {
            var searchText = this.get('searchText');
            if(!searchText) { return; }

            var regex = new RegExp(searchText, 'i');
            return this.get('firstName').filter(function(firstName) {
                return firstName.match(regex);
            });
        }.property('searchText')
    });
  • 相關的部分是searchTextsearchResults

然后我將此作為模板(希望它不會太長):

<script type="text/x-handlebars" data-template-name="users"> 
  <div class="span10 tableContainer">
  {{#linkTo 'users.new' class="btn btn-primary createUser"}}<i class="icon-plus icon-white"></i> New Contact{{/linkTo}}
    <div class="btn-group">
        <a class="btn dropdown-toggle" data-toggle="dropdown" href="#">Display&nbsp&nbsp<span class="caret"></span></a>
        <ul class="dropdown-menu">
            <a {{action displayAll}}>All Contacts</a>
            <a {{action motoDigital}}>Digital Subscriber</a>
            <a {{action motoPhysical}}>Physical Subscriber</a>
        </ul>
        {{input type="text" value=searchText placeholder="Search..."}}
            {{#each searchResults}}
                <a>{{firstName}}</a>
            {{/each}}
    </div>
    <div class="tableScrollable">
    <table class="table table-striped">
      <thead>
        <tr>
          <th class="nameHead">Name</th>
          <th class="companyHead">Company</th>
          <th class="emailHead">Email</th>
        </tr>
      </thead>
      <tbody>
      <tr>
          <td class="name">&nbsp</td>
          <td class="company">&nbsp</td>
          <td class="email">&nbsp</td>
      </tr>
          {{#if sortByDigital}}
            {{#each model}}
                {{#if motoDigital}}
                    <tr>
                      <td class="name"><strong>{{#linkTo 'user' this }}<i class="icon-user"></i> {{firstName}} {{lastName}}{{/linkTo}}</strong></td>
                      <td class="company">{{company}}</td>
                      <td class="email"><i class="icon-envelope"></i> <a {{bindAttr mailto="email"}}>{{email}}</a></td>
                    </tr>
                {{/if}}
            {{/each}}
          {{/if}}

          {{#if sortDisplayAll}}
            {{#each model}}
                 <tr>
                      <td class="name"><strong>{{#linkTo 'user' this }}<i class="icon-user"></i> {{firstName}} {{lastName}}{{/linkTo}}</strong></td>
                      <td class="company">{{company}}</td>
                      <td class="email"><i class="icon-envelope"></i> <a {{bindAttr mailto="email"}}>{{email}}</a></td>
                 </tr>
            {{/each}}
          {{/if}}

          {{#if sortByPhysical}}
            {{#each model}}
                {{#if motoPhysical}}
                     <tr>
                          <td class="name"><strong>{{#linkTo 'user' this }}<i class="icon-user"></i> {{firstName}} {{lastName}}{{/linkTo}}</strong></td>
                          <td class="company">{{company}}</td>
                          <td class="email"><i class="icon-envelope"></i> <a {{bindAttr mailto="email"}}>{{email}}</a></td>
                     </tr>
                 {{/if}}
            {{/each}}
          {{/if}}
      </tbody>
    </table>
   </div>
  </div>
  <div class="span3">
        {{outlet}}
  </div>
  </script>

我希望使用此模板顯示的列表根據搜索過濾自身...並使用一些代碼進行自動完成: http//www.embercasts.com/episodes/building-an-autocomplete-widget-part- 1

任何幫助是極大的贊賞!

弄清楚什么是錯的有點困難。 也許嘗試發布一個jsbin。

我注意到的一件事是, searchText沒有綁定value 您可能想要將其更改為,

{{input type="text" valueBinding=searchText placeholder="Search..."}}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM