Skip to content
This repository was archived by the owner on May 21, 2025. It is now read-only.

Commit 1af1741

Browse files
committed
Merge branch 'change-required-behavior'
2 parents 4ea5d64 + dd4cfb8 commit 1af1741

7 files changed

Lines changed: 101 additions & 80 deletions

File tree

README.md

Lines changed: 41 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ To see a demo go here: http://ghiden.github.io/angucomplete-alt
4040
* Show all items.
4141
* Custom remote API handler which allows you to fully control how to communicate with your remote API. Thanks to @jbuquet
4242

43+
### Angular 1.2
44+
45+
From v2.0.0, I have dropped the support for angular 1.2.
46+
Please use [angucomplete-ie8](https://github.com/ghiden/angucomplete-ie8) which still supports 1.2.
47+
4348
### Getting Started
4449
Download the package, and include the dist/angucomplete-alt.min.js file in your page.
4550

@@ -91,43 +96,43 @@ var app = angular.module('app', ["angucomplete-alt"]);
9196
It expects the returned results from remote API to have a root object. In the above example, 'results' is an array of search results.
9297

9398
### Description of attributes
94-
| Attribute | Description | Required | Example |
95-
| :------------- |:-------------| :-----:| :-----|
96-
| id | A unique ID for the field. [example](http://ghiden.github.io/angucomplete-alt/#example1) | Yes | members |
97-
| placeholder | Placeholder text for the search field. [example](http://ghiden.github.io/angucomplete-alt/#example1) | No | Search members |
98-
| maxlength | Maxlength attribute for the search field. [example](http://ghiden.github.io/angucomplete-alt/#example1) | No | 25 |
99-
| pause | The time to wait (in milliseconds) before searching when the user enters new characters. [example](http://ghiden.github.io/angucomplete-alt/#example1) | No | 400 |
100-
| 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](http://ghiden.github.io/angucomplete-alt/#example1) | Yes | selectedObject or objectSelectedCallback |
101-
| 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](http://ghiden.github.io/angucomplete-alt/#example5) | No | http://myserver.com/api/users/find?searchstr= |
102-
| 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](http://ghiden.github.io/angucomplete-alt/#example5) | No | results |
103-
| 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](http://ghiden.github.io/angucomplete-alt/#example1) | Yes | firstName,lastName |
104-
| 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](http://ghiden.github.io/angucomplete-alt/#example6) | No | twitterUsername |
105-
| 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](http://ghiden.github.io/angucomplete-alt/#example2) | No | pic |
106-
| minlength | The minimum length of string required before searching. [example](http://ghiden.github.io/angucomplete-alt/#example1). 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 |
107-
| input-name | Name for input field | No | |
108-
| input-class | The classes to use for styling the input box. [example](http://ghiden.github.io/angucomplete-alt/#example1) | No | form-control |
109-
| match-class | If it is assigned, matching part of title is highlighted with given class style. [example](http://ghiden.github.io/angucomplete-alt/#example6) | No | highlight |
110-
| local-data | The local data variable to use from your controller. Should be an array of objects. [example](http://ghiden.github.io/angucomplete-alt/#example1) | No | countriesList |
111-
| search-fields | The fields from your local data to search on (comma separate them). Each field can contain dots for accessing nested attribute. [example](http://ghiden.github.io/angucomplete-alt/#example1) | No | title,description |
112-
| remote-url-request-formatter | A function that takes a query string and returns parameter(s) for GET. It should take the query string as argument and returns a key-value object. [example](http://ghiden.github.io/angucomplete-alt/#example5) | No | Suppose if you need to send a query keyword and a timestamp to search API, you can write a function like this in the parent scope. $scope.dataFormatFn = function(str) { return {q: str, timestamp: +new Date()}; } |
113-
| remote-url-request-with-credentials | A boolean that accepts parameters with credentials. | No | true or false |
114-
| remote-url-response-formatter | A function on the scope that will modify raw response from remote API before it is rendered in the drop-down. Useful for adding data that may not be available from the API. The specified function must return the object in the format that angucomplete understands. | No | addImageUrlToObject |
115-
| remote-url-error-callback | A callback funciton to handle error response from $http.get | No | httpErrorCallbackFn |
116-
| remote-api-handler | This gives a way to fully delegate handling of remote search API. This function takes user input string and timeout promise, and it needs to return a promise. For example, if your search API is based on POST, you can use this function to create your own http handler. See example below | No | - |
117-
| clear-selected | To clear out input field upon selecting an item, set this attribute to true. [example](http://ghiden.github.io/angucomplete-alt/#example3) | No | true |
99+
| Attribute | Description | Required | Binding | Example |
100+
| :------------- |:-------------| :-----:| :-----:| :-----|
101+
| id | A unique ID for the field. [example](http://ghiden.github.io/angucomplete-alt/#example1) | Yes | @ | members |
102+
| placeholder | Placeholder text for the search field. [example](http://ghiden.github.io/angucomplete-alt/#example1) | No | @ | Search members |
103+
| maxlength | Maxlength attribute for the search field. [example](http://ghiden.github.io/angucomplete-alt/#example1) | No | attribute | 25 |
104+
| pause | The time to wait (in milliseconds) before searching when the user enters new characters. [example](http://ghiden.github.io/angucomplete-alt/#example1) | No | @ | 400 |
105+
| 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](http://ghiden.github.io/angucomplete-alt/#example1) | Yes | = | selectedObject or objectSelectedCallback |
106+
| 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](http://ghiden.github.io/angucomplete-alt/#example5) | No | @ | http://myserver.com/api/users/find?searchstr= |
107+
| 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](http://ghiden.github.io/angucomplete-alt/#example5) | No | @ | results |
108+
| 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](http://ghiden.github.io/angucomplete-alt/#example1) | Yes | @ | firstName,lastName |
109+
| 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](http://ghiden.github.io/angucomplete-alt/#example6) | No | @ | twitterUsername |
110+
| 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](http://ghiden.github.io/angucomplete-alt/#example2) | No | @ | pic |
111+
| minlength | The minimum length of string required before searching. [example](http://ghiden.github.io/angucomplete-alt/#example1). 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 |
112+
| input-name | Name for input field | No | @ | |
113+
| input-class | The classes to use for styling the input box. [example](http://ghiden.github.io/angucomplete-alt/#example1) | No | @ | form-control |
114+
| match-class | If it is assigned, matching part of title is highlighted with given class style. [example](http://ghiden.github.io/angucomplete-alt/#example6) | No | @ | highlight |
115+
| local-data | The local data variable to use from your controller. Should be an array of objects. [example](http://ghiden.github.io/angucomplete-alt/#example1) | No | = | countriesList |
116+
| search-fields | The fields from your local data to search on (comma separate them). Each field can contain dots for accessing nested attribute. [example](http://ghiden.github.io/angucomplete-alt/#example1) | No | @ | title,description |
117+
| remote-url-request-formatter | A function that takes a query string and returns parameter(s) for GET. It should take the query string as argument and returns a key-value object. [example](http://ghiden.github.io/angucomplete-alt/#example5) | No | = | Suppose if you need to send a query keyword and a timestamp to search API, you can write a function like this in the parent scope. $scope.dataFormatFn = function(str) { return {q: str, timestamp: +new Date()}; } |
118+
| remote-url-request-with-credentials | A boolean that accepts parameters with credentials. | No | @ | true or false |
119+
| remote-url-response-formatter | A function on the scope that will modify raw response from remote API before it is rendered in the drop-down. Useful for adding data that may not be available from the API. The specified function must return the object in the format that angucomplete understands. | No | = | addImageUrlToObject |
120+
| remote-url-error-callback | A callback funciton to handle error response from $http.get | No | = | httpErrorCallbackFn |
121+
| remote-api-handler | This gives a way to fully delegate handling of remote search API. This function takes user input string and timeout promise, and it needs to return a promise. For example, if your search API is based on POST, you can use this function to create your own http handler. See example below | No | = | |
122+
| clear-selected | To clear out input field upon selecting an item, set this attribute to true. [example](http://ghiden.github.io/angucomplete-alt/#example3) | No | @ | true |
118123
| override-suggestions | To override suggestions and set the value in input field to selectedObject. [example](http://ghiden.github.io/angucomplete-alt/#example4) | No | true |
119-
| field-required | Set field to be required. Requirement for this to work is that this directive needs to be in a form. Default class name is "autocomplete-required". [example](http://ghiden.github.io/angucomplete-alt/#example8) If you need to validate more than one directives, you have to provide unique field-required-class for each directive. | No | true |
120-
| field-required-class | Set custom class name for required. Unique class names need to be set when you have multiple directives to validate. | No | "match" |
121-
| text-searching | Custom string to show when search is in progress. Set this to 'false' prevents text to show up. | No | "Searching for items..." |
122-
| text-no-results | Custom string to show when there is no match. Set this to 'false' prevents text to show up. | No | "Not found" |
123-
| initial-value | Initial value for component. If string, the internal model is set to the string value, if an object, the title-field attribute is used to parse the correct title for the view, and the internal model is set to the object. [example](http://ghiden.github.io/angucomplete-alt/#example9) | No | myInitialValue (object/string) |
124-
| input-changed | A callback function that is called when input field is changed. To get attributes of the input from which the assignment was made, use this.$parent.$index within your function. [example](http://ghiden.github.io/angucomplete-alt/#example10) | No | inputChangedFn |
125-
| auto-match | Allows for auto selecting an item if the search text matches a search results attributes exactly. [example](http://ghiden.github.io/angucomplete-alt/#example11) | No | true |
126-
| focus-in | A function or expression to be called when input field gets focused. [example](http://ghiden.github.io/angucomplete-alt/#example12) | No | focusIn() |
127-
| focus-out | A function or expression to be called when input field lose focus. [example](http://ghiden.github.io/angucomplete-alt/#example12) | No | focusOut() |
128-
| disable-input | A model to control disable/enable of input field. [example page](http://ghiden.github.io/angucomplete-alt/#example13) | No | disableInput |
129-
| template-url | Customize the markup of the autocomplete template. [example page](http://ghiden.github.io/angucomplete-alt/#example14) | No | "/my-custom-template.html" |
130-
| focus-first | Automatically select the first match from the result list. | No | true |
124+
| field-required | Set field to be required. Requirement for this to work is that this directive needs to be in a form. Default class name is "autocomplete-required". [example](http://ghiden.github.io/angucomplete-alt/#example8). | No | = | a variable holding true/false |
125+
| field-required-class | Set custom class name for required. | No | @ | "match" |
126+
| text-searching | Custom string to show when search is in progress. Set this to 'false' prevents text to show up. | No | attribute | "Searching for items..." |
127+
| text-no-results | Custom string to show when there is no match. Set this to 'false' prevents text to show up. | No | attribute | "Not found" |
128+
| initial-value | Initial value for component. If string, the internal model is set to the string value, if an object, the title-field attribute is used to parse the correct title for the view, and the internal model is set to the object. [example](http://ghiden.github.io/angucomplete-alt/#example9) | No | = | myInitialValue (object/string) |
129+
| input-changed | A callback function that is called when input field is changed. To get attributes of the input from which the assignment was made, use this.$parent.$index within your function. [example](http://ghiden.github.io/angucomplete-alt/#example10) | No | = | inputChangedFn |
130+
| auto-match | Allows for auto selecting an item if the search text matches a search results attributes exactly. [example](http://ghiden.github.io/angucomplete-alt/#example11) | No | @ | true |
131+
| focus-in | A function or expression to be called when input field gets focused. [example](http://ghiden.github.io/angucomplete-alt/#example12) | No | & | focusIn() |
132+
| focus-out | A function or expression to be called when input field lose focus. [example](http://ghiden.github.io/angucomplete-alt/#example12) | No | & | focusOut() |
133+
| disable-input | A model to control disable/enable of input field. [example page](http://ghiden.github.io/angucomplete-alt/#example13) | No | = | disableInput |
134+
| template-url | Customize the markup of the autocomplete template. [example page](http://ghiden.github.io/angucomplete-alt/#example14) | No | attribute | "/my-custom-template.html" |
135+
| focus-first | Automatically select the first match from the result list. | No | @ | true |
131136

132137
### Scrollbar
133138

@@ -203,27 +208,6 @@ cd examples
203208
python -m SimpleHTTPServer
204209
```
205210

206-
### IE8
207-
208-
To use angucomplete-alt on IE8, take these steps:
209-
210-
1. Use angular 1.2 version.
211-
2. Include polyfills es5-shim and JSON3
212-
3. Comment out the promise chain and use bracket notation
213-
214-
```js
215-
scope.remoteApiHandler(str, httpCanceller.promise)
216-
.then(httpSuccessCallbackGen(str))
217-
.catch(httpErrorCallback);
218-
```
219-
220-
```js
221-
/* IE8 compatible */
222-
scope.remoteApiHandler(str, httpCanceller.promise)
223-
['then'](httpSuccessCallbackGen(str))
224-
['catch'](httpErrorCallback);
225-
```
226-
227211
### Contributors
228212

229213
Here is the list of [contributors](CONTRIBUTORS.md).

angucomplete-alt.js

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,20 @@
105105
}
106106
});
107107

108+
scope.$watch('fieldRequired', function(newval, oldval) {
109+
if (newval !== oldval) {
110+
if (!newval) {
111+
ctrl[scope.inputName].$setValidity(requiredClassName, true);
112+
}
113+
else if (!validState || scope.currentIndex === -1) {
114+
handleRequired(false);
115+
}
116+
else {
117+
handleRequired(true);
118+
}
119+
}
120+
});
121+
108122
scope.$on('angucomplete-alt:clearInput', function (event, elementId) {
109123
if (!elementId || elementId === scope.id) {
110124
scope.searchStr = null;
@@ -225,8 +239,8 @@
225239
function handleRequired(valid) {
226240
scope.notEmpty = valid;
227241
validState = scope.searchStr;
228-
if (scope.fieldRequired && ctrl) {
229-
ctrl.$setValidity(requiredClassName, valid);
242+
if (scope.fieldRequired && ctrl && scope.inputName) {
243+
ctrl[scope.inputName].$setValidity(requiredClassName, valid);
230244
}
231245
}
232246

@@ -783,7 +797,7 @@
783797
matchClass: '@',
784798
clearSelected: '@',
785799
overrideSuggestions: '@',
786-
fieldRequired: '@',
800+
fieldRequired: '=',
787801
fieldRequiredClass: '@',
788802
inputChanged: '=',
789803
autoMatch: '@',

bower.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@
2525
"tests"
2626
],
2727
"dependencies": {
28-
"angular": ">=1.2.0"
28+
"angular": ">=1.4.0"
2929
},
3030
"devDependencies": {
31-
"angular-mocks": ">=1.2.0",
31+
"angular-mocks": ">=1.4.0",
3232
"jquery": "~2.1.0"
3333
}
3434
}

example/css/structure.css

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1014,10 +1014,23 @@ td {
10141014
color: #ff0000;
10151015
}
10161016

1017-
form.ng-invalid-autocomplete-required input {
1017+
form input.ng-invalid-autocomplete-required {
10181018
border-color: red;
10191019
}
10201020

1021+
.valid-status {
1022+
display: inline-block;
1023+
margin-left: 10px;
1024+
}
1025+
1026+
.valid-status.valid {
1027+
color: green;
1028+
}
1029+
1030+
.valid-status.invalid {
1031+
color: red;
1032+
}
1033+
10211034
.console {
10221035
margin-left: 30px;
10231036
font-family: Monaco;

0 commit comments

Comments
 (0)