Giter VIP home page Giter VIP logo

allmighty-autocomplete's Introduction

allmighty-autocomplete

🚧 Warning! This code is not maintained anymore! 🚧

Simple to use autocomplete directive in a module for AngularJS! Supports arrow keys to traverse suggestions as well as mouse input. You can load the suggestions from a remote REST API, it also supports promises.

Checkout the demo to see what it does.

Setup

To use it you need of course AngularJS, so make sure it is loaded first. I always like to use Google's CDN for that:

  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.12/angular.min.js"></script>

Also you should load the stylesheet of the autocomplete:

  <link rel="stylesheet" href="style/autocomplete.css">

Then in your HTML you should load it before the script of your main app. Like this:

<script type="text/javascript" src="script/autocomplete.js"></script>
<script type="text/javascript" src="script/app.js"></script>

In your main script file you should add it as dependency:

var app = angular.module('app', ['autocomplete']);

Usage

If you now want an autocomplete you can just use the tag <autocomplete> tag in your HTML. With the data parameter you can pass in an array that will be used for autocompletion. You need to pass there something which is available in the $scope of your controller.

You can also pass a function that receives changes with the on-type attribute. This is useful if you need to load external resources from a REST API, for example, you can then upload the array you passed into data and it will automatically use the changed array.

Attributes

data : Pass an array to the autocomplete directive. Should be accessible in the $scope of your controller.

on-type : (optional) Pass a function that will receive changes, when somebody types something. It passes the full string for any character typed or deleted. You can use that for example to update the array that you passed in data.

on-select : (optional) Pass a function that will receive changes, when a suggestion is selected. It passes the full string of the suggestion.

click-activation : (optional) When true, the suggestion box opens on click (unfortunately onfoucs is not implemented properly in most browsers right now). By default it is only activated, when you start typing something.

ng-model: What you typed in will be in this variable and accessible in the $scope of the controller.

attr-placeholder: (optional) Sets desired text as placeholder into the input element of autocomplete directive. By default it's "start typing..."

attr-class: (optional) Change the class of the div containing the input and suggestions elements, allowing you to change their style according to your needs.

attr-id: (optional) Change the id of the containing div element, see attrs-class.

attr-input-class: (optional) Set the class of the input element, allowing you to style the input field directly.

attr-input-id: (optional) Change the id of the input element, see attrs-input-class.

autocomplete-required: (optional) This attribute provides value for an ng-required attribute on the directive's input field.

no-auto-sort: (optional) This attribute prevent the library to sort the data by alphabetical order.

Example

HTML:

    <div ng-controller="MyCtrl">
      <autocomplete ng-model="yourchoice" data="movies" on-type="updateMovies"></autocomplete>
    </div>

JavaScript:

	var app = angular.module('app', ['autocomplete']);

	app.controller('MyCtrl', function($scope, MovieRetriever){
		$scope.movies = ["Lord of the Rings",
		 				"Drive",
		 				"Science of Sleep",
		 				"Back to the Future",
		 				"Oldboy"];

		// gives another movie array on change
		$scope.updateMovies = function(typed){
			// MovieRetriever could be some service returning a promise
		    $scope.newmovies = MovieRetriever.getmovies(typed);
		    $scope.newmovies.then(function(data){
		      $scope.movies = data;
		    });
		}
	});

Change log

07.03.2014

  • attr-input-class & attr-input-id allow you to choose class and id of the input field. Handy when wanting to add bootstrap styles to the input field

25.02.2014

  • attr-class & attr-id allow you to choose class and id of the div where the autocomplete is contained, makes it more customizable
  • placeholder renamed into attr-placeholder

14.02.2014

  • got rid of jQuery dependency

13.02.2014

  • ng-model can now be used outside to obtain the current search parameter
  • hiding suggestions on blur

31.01.2014

  • Stop showing suggestions on pushing escape
  • select if suggestions should be shown after clicking on input with new parameter click-activation
  • Added customizable placeholders for input line
  • FIXED multiple autocomplete directives in one controller are now possible and behave as expected

allmighty-autocomplete's People

Contributors

chemisus avatar chesleybrown avatar dancrew32 avatar davidmh avatar fsi avatar grabcode avatar justgoscha avatar klaudiux avatar michalmikolajczyk avatar nadeesha avatar peachscript avatar raymondmuller avatar seriousmanual avatar takayuki-suzuki avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

allmighty-autocomplete's Issues

Bug when not setting ng-model

I have now a bug, when I don't set ng-model and start typing I get the error:

Error: Non-assignable model expression: undefined (directive: autocomplete)
    at Error (native)
    at i (https://ajax.googleapis.com/ajax/libs/angularjs/1.1.5/angular.min.js:43:354)
    at Object.<anonymous> (https://ajax.googleapis.com/ajax/libs/angularjs/1.1.5/angular.min.js:43:467)
    at Object.e.$digest (https://ajax.googleapis.com/ajax/libs/angularjs/1.1.5/angular.min.js:90:233)
    at Object.e.$apply (https://ajax.googleapis.com/ajax/libs/angularjs/1.1.5/angular.min.js:92:431)
    at HTMLInputElement.Va.i (https://ajax.googleapis.com/ajax/libs/angularjs/1.1.5/angular.min.js:120:156)
    at https://ajax.googleapis.com/ajax/libs/angularjs/1.1.5/angular.min.js:24:49
    at Array.forEach (native)
    at n (https://ajax.googleapis.com/ajax/libs/angularjs/1.1.5/angular.min.js:6:470)
    at HTMLInputElement.c (https://ajax.googleapis.com/ajax/libs/angularjs/1.1.5/angular.min.js:24:20) 

suggestions list is automatically sorted

there is no way to specify a custom order because the suggestions list is automatically sorted.
would be nice to have a possibility to specify this behaviour.

Feature Request: Adding up down key scrolling for ARIA support

Is it possible to add up down key scrolling of ng repeat list? If you limit the size of of un ordered list block to height of say 300px with overflow set to auto it does add a vertical scroll bar to drop down list.
Mouse scrolling works fine and list gets updated as you scroll mouse but if you try with keyboard up or down arrow key it does not scroll list view past the end boundaries.

Implement auto-select option

It would be nice if we could have auto-select option.

If it is set to true, the first result would always be selected while typing.

Extra parameter for on-type function

Hi,

I have multiple autocomplete fields in a view, so I would like to send an extra parameter for the on-type function so I can know which autocomplete has changed. Something like this:

<autocomplete
        ng-model="selectedCountry"
        data="contries"
        on-type="typed('country')">
</autocomplete>

Is this possible? Is there another way to know which autocomplete has changed?

Thanks!

ac_template.html not in bower

$ bower install allmighty-autocomplete
bower allmighty-autocomplete#* cached git://github.com/JustGoscha/allmighty-autocomplete.git#54c2c6f193
bower allmighty-autocomplete#* validate 54c2c6f against git://github.com/JustGoscha/allmighty-autocomplete.git#*

$ cat bower_components/allmighty-autocomplete/script/ac_template.html
cat: bower_components/allmighty-autocomplete/script/ac_template.html: No such file or directory

Big array slows things to a halt

Hello there! I've had success with the module using a small dataset. But when I've got 10000+ items it gets too slow to render the input changes (+20 seconds on a modern hardware).

Suggestions amount should be limited so that there's less painting for the browser once the input field value is changed.

As a quickfix you modify row 223:
'<li suggestion ng-repeat="suggestion in suggestions | filter:searchFilter | orderBy:\'toString()\' | limitTo: 10" '+

I feel that limitTo should be added as a parameter to the autocomplete-directive.

Cheers,
Sampo

Release a version

Hi can you create a version tag for the project. I am trying to use the library via rails-assets.org which is a proxy for Bower and it needs versions :)

Thanks!! Ref: tenex/rails-assets#105

Bower: No tag found that was able to satisfy 1.0.140706

I copy+pasted from this repo's bower.json into my project's:

{ "dependencies": { "allmighty-autocomplete": "1.0.140706" } }

But bower complains it can't find the requested version:

$ bower install allmighty-autocomplete
bower not-cached    git://github.com/JustGoscha/allmighty-autocomplete.git#1.0.140706
bower resolve       git://github.com/JustGoscha/allmighty-autocomplete.git#1.0.140706
bower not-cached    git://github.com/JustGoscha/allmighty-autocomplete.git#*
bower resolve       git://github.com/JustGoscha/allmighty-autocomplete.git#*
bower checkout      allmighty-autocomplete#master
bower resolved      git://github.com/JustGoscha/allmighty-autocomplete.git#b9431a68b1
bower ENORESTARGET  No tag found that was able to satisfy 1.0.140706

However, specifying "allmighty-autocomplete": "*" works.

I'm not terribly familiar with bower, but from the error message, it sounds like it's looking for a git tag named 1.0.140706 (which does not exist).

onBlur

So I made a version of this with an onBlur selector. I've also made a lot of other customizations that are very specific to my project which may or may not make sense to put into a PR.

The modification which makes the most sense imo to include is an on-blur attribute which runs a function when the input field loses focus. Similar to on-select.

I made a directive that you can pass a function/value into autocomplete so that when it is true it focuses on the input field.

Let me know if you'd be interested in either of these. Would just submit a PR but it'd be some work to make a version that only has specific modifications so I wanted to pass it by you first.

Data attribut with accents seems not working

Hi thanks for your code,

I use your code like that :

<autocomplete ng-model="query" data="suggestions" on-type="tapedQuery" attr-placeholder=""></autocomplete>

In "Sugestions", I have the folling values :

["Le Chevalier noir", "Leeroy Jenkins", "Légendaire", "Léviathan des flammes"]

But the directive is not showing "Légendaire" and "Léviathan des flammes". Do you have an idea ?

Thanks in advance.

ReferenceError: onType is not defined

Hi, I simply downloaded your repo and opened index.html and I get the following error:

ReferenceError: onType is not defined
    at Object.fn (file:///home/christian/Downloads/allmighty-autocomplete-master/script/autocomplete.js:53:12)
    at Object.e.$digest (https://ajax.googleapis.com/ajax/libs/angularjs/1.1.5/angular.min.js:90:356)
    at Object.e.$apply (https://ajax.googleapis.com/ajax/libs/angularjs/1.1.5/angular.min.js:92:431)
    at https://ajax.googleapis.com/ajax/libs/angularjs/1.1.5/angular.min.js:16:236
    at Object.d [as invoke] (https://ajax.googleapis.com/ajax/libs/angularjs/1.1.5/angular.min.js:27:474)
    at c (https://ajax.googleapis.com/ajax/libs/angularjs/1.1.5/angular.min.js:16:143)
    at xb (https://ajax.googleapis.com/ajax/libs/angularjs/1.1.5/angular.min.js:16:367)
    at rc (https://ajax.googleapis.com/ajax/libs/angularjs/1.1.5/angular.min.js:15:491)
    at HTMLDocument.<anonymous> (https://ajax.googleapis.com/ajax/libs/angularjs/1.1.5/angular.min.js:178:350)
    at c (https://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js:4:26051) 

Bower.json contains no css files

Following values of bower.json are incorrect:

"main": ["./script/autocomplete.js"]

Reason: I use grunt wiredep module and mentioned settings used for dependencies infection. As a result my result index.html don't have link to css

Solution: Will be good to replace mentioned settings of bower.json to

"main": ["./script/autocomplete.js", "./styles/autocomplete.css"]

"this" in Class Controller Event Handlers does not point to controller instance

If you use classes as controllers, which is usual for people using TypeScript, when handling events fired by autocomplete such as "on-select" or "on-type" the keyword "this" no longer refers to the handling controller. ie:

var TestApp;
(function (TestApp) {
var Controllers;
(function (Controllers) {
var TestController = (function () {
function TestController($scope) {
var _this = this;
this.testVar = false;
this.items = ['a', 'b'];
this.item = '';
}
TestController.prototype.select = function () {
this.testVar = true; //this does not set testVar on the controller
};
TestController.prototype.type = function (var typed) {
this.testVar = true; //this does not set testVar on the controller
};

        TestController.$inject = ['$scope'];
        return TestController;
    })();
    Controllers.TestController = TestController;
    angular.module("TestApp.controllers").controller("TestApp.Controllers.TestController", TestController);
})(Controllers = TestApp.Controllers || (TestApp.Controllers = {}));

})(TestApp || (TestApp = {}));

Autocomplete components do not appear on top

Greetings,

I am trying to display the results of the autocomplete, meaning the list of suggestion. I inspect element and find that they are always behind something. I tried adjusting the z index to maximum but no luck. Please let me know what I can do.

Invalid regex for "[" input

Latest version with highlights (not on the demo link), typing [ in the input leads to this error:
SyntaxError: Invalid regular expression: /([|[)/: Unterminated character class

I need pass an array of objects to the control

I need pass an array of objects, and be able to choose the property as the search field.

var data = [

{
name: 'Json',
id: 1
},

{
name: 'Peter',
id: 2
}
]

data = data, searchField=name

doSomething function not executed

I try to execute the demo app, but doSomething() function looks like mute.
My console.log('test'); inside is not triggered.

The html :

<autocomplete ng-model="result" attr-placeholder="type to search movies..." click-activation="true" data="movies" on-type="doSomething" on-select="doSomethingElse"></autocomplete>

The JS inside my controller :

  $scope.doSomething = function(input) {
    console.log('test');
  }

EDIT:

After debugging test, in autocomplete.js, $scope.onType is undefined

On select Customize input value

I have a one to many relationship where a product belongs to a vendor. In my new product form I have a vendor_id which I would like to autocomplete allowing the user to search for a vender name. When that vendor name is selected, instead of the name inserted in the field, I would like the id in the field. Either that or a way to insert the vendor_id into a hidden field. Is there a way to do that with this directive?

How to clear the selected value in the inputtext

Hi, I need to clear the selected value of inputtext once a button is clicked. Can you please suggest how to do this. I have tried few approach but not working. Everything else is working fine. I'm using below code for autocompletion


Change the input class dinamically

Hi,

I can use the attr-input-class to set the input class, but it only accepts a string. Is there a way to set this class dinamically?

For example, I want a different the background color when the input has content and when the input is empty.

Thanks!

$sce:itype error message

I'm getting the following error when trying to use allmighty-autocomplete:

Error: [$sce:itype] http://errors.angularjs.org/1.2.16/$sce/itype?p0=html

If I mess with the template html file and remove lines that contain $index, the error goes away, so I'm assuming that's the invalid HTML, but I'm unsure how to make it valid (or why this is happening for me but not for others). Any suggestions?

Thanks!

option to disable automatic suggestion filtering

Hello,
i'd like to ask if there will be possibility to disable filtering of autocomplete suggestions. I mean when I type for exampple "physics" but I populate the suggestions "data" with terms not containing exact string "physics", these are not visible despite the fact the "data" are populated with them.
Thanks,
Daniel

ng-model?

This is probably a dumb question and maybe I'm blind but the directive doesn't seem to be updating ng-model. How do I get the final selected out of the autocomplete?

with requirejs

how do i register the autocomplete.js properly.. i'm getting error:

Uncaught ReferenceError: angular is not defined app/bower_components/allmighty-autocomplete/script/autocomplete.js:3

I'm using the angular + requirejs seed (https://github.com/tnajdek/angular-requirejs-seed) and my registration as follow:

require( [
'angular',
'app',
'autocomplete',
'routes'
], function(angular, app, autocomplete, routes) {
var $html = angular.element(document.getElementsByTagName('html')[0]);

angular.element().ready(function() {
    angular.resumeBootstrap([app['name']]);
});

});

.bower.json has 2 autocomplete entries on "main" property

It's causing a duplicated script tag using grunt. Can you fix it?
And about the CSS. Can add it automatically?

Edit: About the data itself: do you guys plan to have a "parse" function for data? For instance: I have an array of objects containing the id and name of a person and I'd like to use the autocomplete to search it. But when I select the name, I'd like to have access to the person's id.

Chained suggestions

Is there or will there be a way to chain suggestions? Let's say that my data is ['Mango', 'Pineapple', 'Banana'], and I would write M to get Mango as a suggestion, and if I select that, enter a space, and then start typing B I would get Banana as a new suggestion. Selecting Banana would not remove Mango from the search, but would leave Mango Banana in the search bar.

I thought I would be able to do this by changing $scope.searchParam, but I can't seem to reach that variable for some reason.

on-select doesn't work

Hi,

on-select directive doesn't work, and i couldn't figure out why. Here is my code.

HTML:

 <autocomplete attr-class="autocomplete" on-select="showProduct" ng-model="product"
    placeholder="Arama yapın"
    click-activation="true" data="products" on-type="typed"></autocomplete>

Javascript :

$subscope.showProduct = function () {
    console.log('test');
};

$subscope.typed = function(typedString) {
    console.log('typed :  '  + typedString);
 };

Here typed function works but when i selected the suggested item(hit enter) showProduct function doesn't work.

Where does the attributes class and id should go?

Currently, they are rendered in the .autocomplete div, but I was thinking that maybe they could be more useful in the input within the .autocomplete.

Case in point:

<autocomplete ... attr-class="form-control"></autocomplete>

renders:

<div class="autocomplete form-control">
  <input ...>
</div>

wrong element

Working with bootstrap or another css framework requires adding the class to the input itself. Currently there's no way of doing that.

TL;DR: move the class and id attributes to the input inside .autocomplete

Match autocomplete only with the beginning of words

imagine that i have this three phrases on the scope of autocomplete

"cambiar a prepago"
"cambiar de empresa"
"chat con representante"

If i start typing in my search input the three letters "pre" this three results will match with the phrase "pre", but I think that the only correct is the first one.
I never want to match with something in the middle of the word, this is not how people searchs.

so, is there any way to match my search phrase only with the beginning of my suggestions?

Working with array of strings is limiting

Hi there,

I'm trying this out, since I was looking for an autocomplete directive to use in my app.

One problem I have (design wise) is that the array your directive expects is of type string.

I think it would be more flexible if this was an array of objects, and then you could provide options for what property within the array is the label and what property is the value.

A very common use case for this is to get the id of the thingy inside the auto-complete box. The string is often not unique and not all that useful.

Is there a way to do this with your directive? And if not, I suggest adding such a feature.

Thanks for listening.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.