5244-component_workerAutocomplete #1679
|
@ -1,23 +1,8 @@
|
||||||
<vn-autocomplete
|
<tpl-item>
|
||||||
vn-one
|
|
||||||
url="Workers/search"
|
|
||||||
search-function="{or: [
|
|
||||||
{name: $search},
|
|
||||||
{nickname: {like: '%'+ $search +'%'}},
|
|
||||||
{code: {like: $search +'%'}}
|
|
||||||
]}"
|
|
||||||
label="Worker">
|
|
||||||
<tpl-item>
|
|
||||||
<div>
|
<div>
|
||||||
{{name}}
|
{{name}}
|
||||||
</div>
|
</div>
|
||||||
<div class="text-caption text-secondary">
|
<div class="text-caption text-secondary">
|
||||||
{{nickname}}, {{code}}
|
{{nickname}}, {{code}}
|
||||||
</div>
|
</div>
|
||||||
</vn-autocomplete>
|
</tpl-item>
|
||||||
<vn-drop-down
|
|
||||||
vn-id="drop-down"
|
|
||||||
on-select="$ctrl.onDropDownSelect(item)"
|
|
||||||
on-data-ready="$ctrl.onDataReady()"
|
|
||||||
on-close-start="$ctrl.onDropDownClose()">
|
|
||||||
</vn-drop-down>
|
|
||||||
|
|
|
@ -1,26 +1,28 @@
|
||||||
import ngModule from '../../module';
|
import ngModule from '../../module';
|
||||||
import Autocomplete from '../autocomplete';
|
import Autocomplete from '../autocomplete';
|
||||||
|
|
||||||
/**
|
|
||||||
* Input with option selector.
|
|
||||||
*
|
|
||||||
* @property {String} showFiled The data field name that should be shown
|
|
||||||
* @property {String} valueField The data field name that should be used as value
|
|
||||||
* @property {Array} data Static data for the autocomplete
|
|
||||||
* @property {Object} intialData An initial data to avoid the server request used to get the selection
|
|
||||||
* @property {Boolean} multiple Whether to allow multiple selection
|
|
||||||
* @property {Object} selection Current object selected
|
|
||||||
*
|
|
||||||
* @event change Thrown when value is changed
|
|
||||||
*/
|
|
||||||
export default class WorkerAutocomplete extends Autocomplete {
|
export default class WorkerAutocomplete extends Autocomplete {
|
||||||
constructor($element, $, $compile, $transclude) {
|
constructor(...args) {
|
||||||
super($element, $, $compile, $transclude);
|
super(...args);
|
||||||
|
Object.assign(this, {
|
||||||
|
label: 'Worker',
|
||||||
|
url: 'Workers/search',
|
||||||
|
fields: ['id', 'name', 'nickname'],
|
||||||
|
searchFunction: function($search) {
|
||||||
|
return {and: [
|
||||||
|
{active: {neq: false}},
|
||||||
|
{or: [
|
||||||
|
{name: $search},
|
||||||
|
{nickname: {like: '%' + $search + '%'}},
|
||||||
|
{code: {like: $search + '%'}}
|
||||||
|
]}
|
||||||
|
]};
|
||||||
vicent marked this conversation as resolved
Outdated
|
|||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
WorkerAutocomplete.$inject = ['$element', '$scope', '$compile', '$transclude'];
|
|
||||||
|
|
||||||
ngModule.vnComponent('vnWorkerAutocomplete', {
|
ngModule.vnComponent('vnWorkerAutocomplete', {
|
||||||
template: require('./index.html'),
|
slotTemplate: require('./index.html'),
|
||||||
controller: WorkerAutocomplete
|
controller: WorkerAutocomplete
|
||||||
});
|
});
|
||||||
|
|
|
@ -18,44 +18,7 @@ module.exports = Self => {
|
||||||
});
|
});
|
||||||
|
|
||||||
Self.search = async filter => {
|
Self.search = async filter => {
|
||||||
const models = Self.app.models;
|
const $ = Self.app.models;
|
||||||
|
return await $.VnUser.find(filter);
|
||||||
delete filter.order;
|
|
||||||
delete filter.fields;
|
|
||||||
const users = await models.VnUser.find(filter);
|
|
||||||
|
|
||||||
if (filter.where) {
|
|
||||||
const indexToDelete = 2;
|
|
||||||
const filterByCode = filter.where.or.splice(indexToDelete, 1)[0];
|
|
||||||
|
|
||||||
const ids = users.map(user => parseInt(user.id));
|
|
||||||
filter.where.or = [
|
|
||||||
{id: {inq: ids}},
|
|
||||||
filterByCode
|
|
||||||
];
|
|
||||||
}
|
|
||||||
|
|
||||||
filter.include = {
|
|
||||||
relation: 'user',
|
|
||||||
scope: {
|
|
||||||
fields: ['name', 'nickname']
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
const workers = await models.Worker.find(filter);
|
|
||||||
|
|
||||||
const result = workers.map(worker => {
|
|
||||||
return {
|
|
||||||
id: worker.id,
|
|
||||||
code: worker.code,
|
|
||||||
name: worker.user().name,
|
|
||||||
nickname: worker.user().nickname
|
|
||||||
};
|
|
||||||
});
|
|
||||||
result.sort((a, b) => {
|
|
||||||
return a.name.localeCompare(b.name);
|
|
||||||
});
|
|
||||||
|
|
||||||
return result;
|
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in New Issue
url,