Angucomplete Alt
Autocomplete Directive for AngularJS. A fork of Daryl Rowland's angucomplete (https://github.com/darylrowland/angucomplete) with some extra features.
Install / Use
npx skills add ghiden/angucomplete-altInstalls into whichever agent you are using.
README
angucomplete-alt
This is a fork of Daryl Rowland's angucomplete (https://github.com/darylrowland/angucomplete) with a bit of tweaks such as:
- change long attribute names to hyphenated ones
- coding style similar to angular standard
- refactored in general
- jshint
- more test coverage
To see a demo go here: https://ghiden.github.io/angucomplete-alt
###Key Features
- Show just a title, a title and a description or a title, description and image in your autocomplete list
- Deliberately minimally styled so you can customise it to your heart's content!
- Reads JSON data and allows you to specify which fields to use for display
- Simple setup - e.g. to pull data from a server, just set the url parameter
Extra Features
- Request format function: if you need to tweak data before you send to your search API, you can set your own format function. Search query goes through your function and gets sent to your API.
- Response format function: if you need to tweak response from the server before it gets processed by the directive, you can set your own format function. Raw HTTP response goes through your function. Thanks to @nekcih for proposing this feature.
- Clear on selection: when you select an item, input field is cleared.
- Blur event handling, thanks to @leejsinclair
- Override suggestions
- You can either bind an object or callback function
- bind an object: it works as one-way-data-binding. It gets set when a selection is made.
- callback function: when a selection is made by user, this callback is called with the selected object. When the selection is deselected, the callback is called with undefined. Thanks to @nekcih for proposing this feature.
- Required support: It is a bit different from ng-required which becomes valid when there is any character in input field. This required becomes valid when a selection is made. Class name is "autocomplete-required" and customizable. Thanks to @alindber for the initial idea.
- Custom texts for "Searching..." and "No results found", thanks to @vhuerta for this idea.
- Be able to set initial value. This becomes handy if you use this directive for updating existing model.
- Be able to set a error callback for ajax request
- Add a callback for tracking input changes. Thanks to @urecio for the initial idea.
- Auto match
- Add callbacks for tracking focus in/out.
- Enable/disable input field
- Show scrollbar. See example #1
- Clear input by sending $broadcast from parent scope. Thanks to @Leocrest for #61.
- Override template with your own. When you use this feature, test throughly as it might break other features. Thanks to @sdbondi for #74.
- Show all items.
- Custom remote API handler which allows you to fully control how to communicate with your remote API. Thanks to @jbuquet
- Custom search function for handling local data
Angular 1.2
From v2.0.0, I have dropped the support for angular 1.2. Please use angucomplete-ie8 which still supports 1.2.
Getting Started
Download the package, and include the dist/angucomplete-alt.min.js file in your page.
bower install angucomplete-alt --save
Or
npm install angucomplete-alt --save
Then add the angucomplete-alt module to your Angular App file, e.g.
var app = angular.module('app', ["angucomplete-alt"]);
Using local data
<angucomplete-alt id="ex1"
placeholder="Search countries"
pause="100"
selected-object="selectedCountry"
local-data="countries"
search-fields="name"
title-field="name"
minlength="1"
input-class="form-control form-control-small"/>
Using local data with custom search function
<angucomplete-alt id="ex2"
placeholder="Search people"
pause="300"
selected-object="selectedPerson"
local-data="people"
local-search="localSearch"
title-field="firstName,surname"
description-field="twitter"
image-field="pic"
minlength="1"
input-class="form-control form-control-small"
match-class="highlight" />
Local search function takes a string and returns an array of matched items.
// Here is a naive implementation for matching first name, last name, or full name
$scope.localSearch = function(str) {
var matches = [];
$scope.people.forEach(function(person) {
var fullName = person.firstName + ' ' + person.surname;
if ((person.firstName.toLowerCase().indexOf(str.toString().toLowerCase()) >= 0) ||
(person.surname.toLowerCase().indexOf(str.toString().toLowerCase()) >= 0) ||
(fullName.toLowerCase().indexOf(str.toString().toLowerCase()) >= 0)) {
matches.push(person);
}
});
return matches;
};
Using remote API
<angucomplete-alt id="members"
placeholder="Search members"
pause="400"
selected-object="testObj"
remote-url="http://myserver.com/api/user/find?s="
remote-url-data-field="results"
title-field="firstName,surname"
description-field="email"
image-field="profilePic"
input-class="form-control form-control-small"/>
It expects the returned results from remote API to have a root object. In the above example, 'results' is an array of search results.
Description of attributes
| Attribute | Description | Required | Binding | Example | | :------------- |:-------------| :-----:| :-----:| :-----| | id | A unique ID for the field. example | Yes | @ | members | | placeholder | Placeholder text for the search field. example | No | @ | Search members | | maxlength | Maxlength attribute for the search field. example | No | attribute | 25 | | pause | The time to wait (in milliseconds) before searching when the user enters new characters. example | No | @ | 400 | | selected-object | Either an object in your scope or callback function. If you set an object, it will be passed to the directive with '=' sign but it is actually one-way-bound data. So, setting it from your scope has no effect on input string. If you set a callback, it gets called when selection is made. To get attributes of the input from which the assignment was made, use this.$parent.$index within your function. example | Yes | = | selectedObject or objectSelectedCallback | | selected-object-data | A second parameter which will be passed to selected-object. Only works when using selected-object. | No | = | row | | remote-url | The remote URL to hit to query for results in JSON. angucomplete will automatically append the search string on the end of this, so it must be a GET request. example | No | @ | http://myserver.com/api/users/find?searchstr= | | remote-url-data-field | The name of the field in the JSON object returned back that holds the Array of objects to be used for the autocomplete list. example | No | @ | results | | title-field | The name of the field in the JSON objects returned back that should be used for displaying the title in the autocomplete list. Note, if you want to combine fields together, you can comma separate them here (e.g. for a first and last name combined). If you want to access nested field, use dot to connect attributes (e.g. name.first). example | Yes | @ | firstName,lastName | | description-field | The name of the field in the JSON objects returned back that should be used for displaying the description in the autocomplete list. example | No | @ | twitterUsername | | image-field | The name of the field in the JSON objects returned back that should be used for displaying an image in the autocomplete list. example | No | @ | pic | | minlength | The minimum length of string required before searching. example. If set to 0, it shows all items. It works both local and remote but is intended to use with local data. If used with remote API, it needs to return all items when query parameter is empty string. | No | @ | 3 | | input-name | Name for input field. This is required when you use field-required. | No | @ | | | input-class | The classes to use for styling the input box. example | No | @ | form-control | | match-class | If it is assigned, matching part of title is highlighted with given class style. example | No | @ | highlight | | local-data | The local data variable to use from your controller. Should be an array of objects. example | No | = | countriesList | | local-search | A function that search local data. It should take a input string and an array of items as arguments and returns an array of matched items. example | No | & | localSearch | | search-fields | The fields from your local data to search on (comma separate them). Each field can contain dots for accessing nested attribute. example | No | @ | title,description | | remote-url-request-formatter | A function th
Related Skills
node-connect
385.5kDiagnose OpenClaw Android, iOS, or macOS node pairing, QR/setup code, route, auth, and connection failures.
blender-python-addon
40.5kBlender Python add-on rules for operators, panels, properties, registration, testing, and API-safe scripting
flutter-development-guidelines-cursorrules-prompt-file
40.5kCursor rules for Flutter development with MVVM architecture, Riverpod state management, Material widgets, and Dart style guidelines.
commit-push-pr
140.6kCommit, push, and open a PR
