bug fixed in autocomplete

This commit is contained in:
Daniel Herrero 2017-11-09 10:11:59 +01:00
parent 3d33c21cc8
commit 865d106d4f
1 changed files with 61 additions and 46 deletions

View File

@ -1,14 +1,16 @@
import {module} from '../module';
import Component from '../lib/component';
import copyObject from '../lib/copy';
import './style.scss';
class Autocomplete extends Component {
constructor($element, $scope, $http, $timeout) {
constructor($element, $scope, $http, $timeout, $filter) {
super($element);
this.$element = $element;
this.$scope = $scope;
this.$http = $http;
this.$timeout = $timeout;
this.$filter = $filter;
this._showDropDown = false;
this.finding = false;
@ -20,7 +22,7 @@ class Autocomplete extends Component {
this.showField = this.showField || 'name';
this.valueField = this.valueField || 'id';
this.order = this.order || 'name ASC';
this.items = this.data || [];
this.items = copyObject(this.data) || [];
this.displayValueMultiCheck = [];
this._multiField = [];
this.readonly = true;
@ -187,10 +189,7 @@ class Autocomplete extends Component {
}
findItems(search) {
if (!this.url)
return this.items ? this.items : [];
if (search && !this.finding) {
if (this.url && search && !this.finding) {
this.maxRow = false;
let filter = {where: {name: {regexp: search}}};
if (this.filter && this.filter.where) {
@ -215,6 +214,8 @@ class Autocomplete extends Component {
this.finding = false;
}
);
} else if (search && !this.url && this.data) {
this.items = this.$filter('filter')(this.data, search);
} else if (!search && !this.finding) {
this.maxRow = 10;
this.items = [];
@ -222,48 +223,54 @@ class Autocomplete extends Component {
}
}
getItems() {
let filter = {};
if (!this.finding) {
this.finding = true;
if (this.url === undefined) {
this.items = copyObject(this.data);
this.maxRow = false;
this.removeLoadMore = true;
} else {
let filter = {};
if (!this.finding) {
this.finding = true;
if (this.maxRow) {
if (this.items) {
filter.skip = this.items.length;
}
filter.limit = this.maxRow;
filter.order = this.order;
}
if (this.filter) {
Object.assign(filter, this.filter);
}
let json = JSON.stringify(filter);
this.removeLoadMore = false;
this.$http.get(`${this.url}?filter=${json}`).then(
json => {
if (json.data.length) {
json.data.forEach(
el => {
if (this.multiple) {
el.checked = this.field.indexOf(el[this.valueField]) !== -1;
}
this.items.push(el);
}
);
if (filter.skip === 0 && this.maxRow && json.data.length < this.maxRow) {
this.removeLoadMore = true;
}
} else {
this.maxRow = false;
if (this.maxRow) {
if (this.items) {
filter.skip = this.items.length;
}
this.finding = false;
},
() => {
this.finding = false;
filter.limit = this.maxRow;
filter.order = this.order;
}
);
if (this.filter) {
Object.assign(filter, this.filter);
}
let json = JSON.stringify(filter);
this.removeLoadMore = false;
this.$http.get(`${this.url}?filter=${json}`).then(
json => {
if (json.data.length) {
json.data.forEach(
el => {
if (this.multiple) {
el.checked = this.field.indexOf(el[this.valueField]) !== -1;
}
this.items.push(el);
}
);
if (filter.skip === 0 && this.maxRow && json.data.length < this.maxRow) {
this.removeLoadMore = true;
}
} else {
this.maxRow = false;
}
this.finding = false;
},
() => {
this.finding = false;
}
);
}
}
}
$onInit() {
@ -305,9 +312,17 @@ class Autocomplete extends Component {
this.$element.unbind('focusout');
}
$onChanges(objectChange) {
if (objectChange.data && objectChange.data.currentValue && objectChange.data.currentValue.length) {
this.items = copyObject(objectChange.data.currentValue);
this.maxRow = false;
this.removeLoadMore = true;
}
}
}
Autocomplete.$inject = ['$element', '$scope', '$http', '$timeout'];
Autocomplete.$inject = ['$element', '$scope', '$http', '$timeout', '$filter'];
module.component('vnAutocomplete', {
template: require('./autocomplete.html'),