2574 - ItemTag refactor sourceTable
gitea/salix/pipeline/head There was a failure building this commit Details

This commit is contained in:
Joan Sanchez 2020-11-04 11:29:10 +01:00
parent fc957eda6c
commit a325ef2a78
5 changed files with 66 additions and 43 deletions

View File

@ -718,11 +718,13 @@ INSERT INTO `vn`.`itemType`(`id`, `code`, `name`, `categoryFk`, `life`,`workerFk
INSERT INTO `vn`.`ink`(`id`, `name`, `picture`, `showOrder`, `hex`) INSERT INTO `vn`.`ink`(`id`, `name`, `picture`, `showOrder`, `hex`)
VALUES VALUES
('YEL', 'Yellow', 1, 1, 'F4D03F'), ('YEL', 'Yellow', 1, 1, 'F4D03F'),
('BLU', 'Blue', 1, 2, '5DADE2'), ('BLU', 'Blue', 1, 2, '5DADE2'),
('RED', 'Red', 1, 3, 'EC7063'), ('RED', 'Red', 1, 3, 'EC7063'),
('SLV', 'Silver', 1, 4, 'CACFD2'), ('SLV', 'Silver', 1, 4, 'CACFD2'),
('BRW', 'Brown', 1, 5, 'DC7633'); ('BRW', 'Brown', 1, 5, 'DC7633'),
('BLK', 'Black', 1, 6, '000000'),
('BAS', 'Blue/Silver', 1, 7, '5DADE2');
INSERT INTO `vn`.`origin`(`id`,`code`, `name`) INSERT INTO `vn`.`origin`(`id`,`code`, `name`)
VALUES VALUES

View File

@ -0,0 +1,52 @@
const ParameterizedSQL = require('loopback-connector').ParameterizedSQL;
const mergeFilters = require('vn-loopback/util/filter').mergeFilters;
module.exports = Self => {
Self.remoteMethod('filterValue', {
description: 'Returns a list of tag values',
accepts: [{
arg: 'id',
type: 'Number',
required: true,
description: 'The tag id',
http: {source: 'path'}
}, {
arg: 'filter',
type: 'Object',
description: `Filter defining where, order, offset, and limit - must be a JSON-encoded string`
}],
returns: {
type: ['object'],
root: true
},
http: {
path: `/:id/filterValue`,
verb: 'GET'
}
});
Self.filterValue = async(id, filter) => {
const conn = Self.dataSource.connector;
const tag = await Self.findById(id);
let stmt;
if (!tag.isFree && tag.sourceTable) {
stmt = new ParameterizedSQL(
`SELECT value FROM (
SELECT name AS value FROM ${tag.sourceTable}) v`);
const where = filter.where;
if (where && where.value) {
filter.order = `value LIKE '${where.value}' DESC,
value LIKE '${where.value}%' DESC`;
filter.where = {value: {like: `%${where.value}%`}};
}
} else
stmt = new ParameterizedSQL(`SELECT value FROM itemTag`);
stmt.merge(conn.makeSuffix(filter));
return conn.executeStmt(stmt);
};
};

View File

@ -0,0 +1,3 @@
module.exports = Self => {
require('../methods/tag/filterValue')(Self);
};

View File

@ -40,12 +40,12 @@
</vn-textfield> </vn-textfield>
<vn-autocomplete vn-three <vn-autocomplete vn-three
ng-show="tag.selection.isFree === false" ng-show="tag.selection.isFree === false"
url="{{$ctrl.sourceTables[itemTag.id].url}}" url="{{'Tags/' + itemTag.tagFk + '/filterValue'}}"
search-function="{name: {like: $search +'%'}}" search-function="{value: $search}"
label="Value" label="Value"
ng-model="itemTag.value" ng-model="itemTag.value"
show-field="{{$ctrl.sourceTables[itemTag.id].field}}" show-field="value"
value-field="{{$ctrl.sourceTables[itemTag.id].field}}" value-field="value"
rule> rule>
</vn-autocomplete> </vn-autocomplete>
<vn-input-number vn-one <vn-input-number vn-one

View File

@ -10,40 +10,6 @@ class Controller extends Section {
fields: ['id', 'name', 'isFree', 'sourceTable'] fields: ['id', 'name', 'isFree', 'sourceTable']
} }
}; };
this.sourceTables = {};
}
set itemTags(value) {
if (value) {
value.forEach(tag => {
this.getSourceTable(tag);
});
this._itemTags = value;
}
}
get itemTags() {
return this._itemTags;
}
getSourceTable(obj) {
let sourceTable;
this.sourceTables[obj.id] = {};
let tag = obj.tag || obj.selection;
if (!tag || !tag.sourceTable && (tag.isFree === true || tag.isFree === undefined))
sourceTable = null;
else if (tag.sourceTable) {
sourceTable = '' + tag.sourceTable.charAt(0).toUpperCase() +
tag.sourceTable.substring(1) + 's';
this.sourceTables[obj.id].field = 'name';
} else {
sourceTable = `ItemTags/filterItemTags/${tag.id}`;
this.sourceTables[obj.id].field = 'value';
}
this.sourceTables[obj.id].url = sourceTable;
} }
add() { add() {