Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a TextField with autocomplete

Tags:

ember.js

I have to create a TextField with autocomplete in Ember to fetch data from Database on each key press based on match.

Is their any built-in widget in Ember for this ?

like image 962
fortm Avatar asked Apr 21 '13 23:04

fortm


People also ask

Which attribute provides autocomplete feature for textfield?

The HTML autocomplete attribute lets web developers specify what if any permission the user agent has to provide automated assistance in filling out form field values, as well as guidance to the browser as to the type of information expected in the field.

How do you auto fill a textbox in HTML?

The autocomplete attribute specifies whether or not an input field should have autocomplete enabled. Autocomplete allows the browser to predict the value. When a user starts to type in a field, the browser should display options to fill in the field, based on earlier typed values.


3 Answers

You can use events of Ember.Textfield to do this. (Coffeescript and Jade)

Search View

App.SearchView = Ember.View.extend

    templateName: 'search'

    searchTerm: null

    searchTextField: Ember.TextField.extend

      insertNewline: ->
        # if the user hits the enter key, you can do something different or call the same function
        @get('controller').search(@get('searchTerm'))

      keyUp: (e) ->
        # validate the item on every keypress
        if (e.currentTarget.value.length > 0)
          @get('controller').search(@get('searchTerm'))

Search Template

script(type='text/x-handlebars', data-template-name='search')

{{view view.searchTextField valueBinding="view.searchTerm" placeholder="search"}}

<button {{action "search"}}>search</button>

Search Controller

App.SearchController = Ember.ObjectController.extend

  search: (searchTerm) ->
    # do your search

** added missing parenthesis

like image 193
WallMobile Avatar answered Oct 30 '22 19:10

WallMobile


EmberCasts has a good video on how to build an auto complete widget.

I asked them about the millisecond delay on filtering and I was told they will cover that in the next episode they do on it.

like image 36
ootoovak Avatar answered Oct 30 '22 20:10

ootoovak


There is no built-in component in Ember.js, but from experience such component would be very easy to write yourself. On EmberCamp Trek Glowacki said he hopes no widget library will be ever needed.

You could also use Typeahead from Twitter Bootstrap or AutoComplete from jQuery UI which can be made to work together.

like image 25
Myslik Avatar answered Oct 30 '22 18:10

Myslik