Added datalist
gitea/salix/845-order_catalog_datalist This commit looks good Details

This commit is contained in:
Joan Sanchez 2020-02-11 14:41:59 +01:00
parent e8ae2f5767
commit c78c154ab0
6 changed files with 102 additions and 44 deletions

View File

@ -157,8 +157,6 @@ export default class Datalist extends Textfield {
this.destroyList();
} else
this.buildList();
this.emit('select', {selection});
});
}

View File

@ -162,6 +162,7 @@ module.exports = Self => {
`SELECT
it.tagFk,
it.itemFk,
it.value,
t.name
FROM tmp.ticketCalculateItem tci
JOIN vn.itemTag it ON it.itemFk = tci.itemFk

View File

@ -85,12 +85,15 @@
<vn-icon icon="icon-item"></vn-icon>
</prepend>
</vn-textfield>
<vn-textfield
vn-one
<vn-datalist vn-one
vn-id="search"
data="$ctrl.tagValues"
ng-model="$ctrl.value"
ng-keyUp="$ctrl.onSearchByTag($event)"
label="Search tag"
ng-model="$ctrl.value">
show-field="value"
value-field="value"
label="Search tag">
<prepend>
<vn-icon icon="search"></vn-icon>
</prepend>
@ -101,7 +104,7 @@
style="cursor: pointer;">
</vn-icon>
</append>
</vn-textfield>
</vn-datalist>
</vn-vertical>
<vn-popover
vn-id="popover"

View File

@ -19,7 +19,7 @@ class Controller {
];
this.defaultOrderFields = [
{field: 'relevancy DESC, name', name: 'Relevancy'},
{field: 'showOrder, price', name: 'Color'},
{field: 'showOrder, price', name: 'Color and price'},
{field: 'name', name: 'Name'},
{field: 'price', name: 'Price'}
];
@ -69,37 +69,8 @@ class Controller {
if (!value) return;
const newFilterList = [];
value.forEach(item => {
// Add new tag filters
item.tags.forEach(itemTag => {
const alreadyAdded = newFilterList.findIndex(filter => {
return filter.field == itemTag.tagFk;
});
if (alreadyAdded == -1) {
newFilterList.push({
name: itemTag.name,
field: itemTag.tagFk,
isTag: true
});
}
});
});
// Add default filters - Replaces tags with same name
this.defaultOrderFields.forEach(orderField => {
const index = newFilterList.findIndex(newfield => {
return newfield.name == orderField.name;
});
if (index > -1)
newFilterList[index] = orderField;
else
newFilterList.push(orderField);
});
this.orderFields = newFilterList;
this.buildTagsFilter(value);
this.buildOrderFilter(value);
}
get categoryId() {
@ -273,6 +244,45 @@ class Controller {
this.$state.go(this.$state.current.name, params);
}
// Builds the tags filter
buildTagsFilter(items) {
const tagValues = [];
items.forEach(item => {
item.tags.forEach(itemTag => {
const alreadyAdded = tagValues.findIndex(tag => {
return tag.value == itemTag.value;
});
if (alreadyAdded == -1)
tagValues.push(itemTag);
});
});
this.tagValues = tagValues;
}
// Builds the order filter
buildOrderFilter(items) {
const tags = [];
items.forEach(item => {
item.tags.forEach(itemTag => {
const alreadyAdded = tags.findIndex(tag => {
return tag.field == itemTag.tagFk;
});
if (alreadyAdded == -1) {
tags.push({
name: itemTag.name,
field: itemTag.tagFk,
isTag: true
});
}
});
});
let newFilterList = [].concat(this.defaultOrderFields);
newFilterList = newFilterList.concat(tags);
this.orderFields = newFilterList;
}
}
Controller.$inject = ['$http', '$scope', '$state', '$compile', '$transitions'];

View File

@ -37,16 +37,19 @@ describe('Order', () => {
describe('items() setter', () => {
it(`should return an object with order params`, () => {
let expectedResult = [{field: 'showOrder, price', name: 'Color'}];
let unexpectedResult = [{tagFk: 5, name: 'Color'}];
controller.items = [{id: 1, name: 'My Item', tags: [
spyOn(controller, 'buildTagsFilter');
spyOn(controller, 'buildOrderFilter').and.callThrough();
const expectedResult = [{field: 'showOrder, price', name: 'Color and price'}];
const items = [{id: 1, name: 'My Item', tags: [
{tagFk: 4, name: 'Length'},
{tagFk: 5, name: 'Color'}
]}];
controller.items = items;
expect(controller.orderFields.length).toEqual(5);
expect(controller.orderFields.length).toEqual(6);
expect(controller.orderFields).toEqual(jasmine.arrayContaining(expectedResult));
expect(controller.orderFields).not.toEqual(jasmine.arrayContaining(unexpectedResult));
expect(controller.buildTagsFilter).toHaveBeenCalledWith(items);
expect(controller.buildOrderFilter).toHaveBeenCalledWith(items);
});
});
@ -222,6 +225,48 @@ describe('Order', () => {
expect(controller.$.model.addFilter).toHaveBeenCalledWith(null, expectedOrder);
});
});
describe('buildTagsFilter()', () => {
it(`should create an array of non repeated tag values and then set the tagValues property`, () => {
const items = [
{
id: 1, name: 'My Item 1', tags: [
{tagFk: 4, name: 'Length', value: 1},
{tagFk: 5, name: 'Color', value: 'red'}
]
},
{
id: 2, name: 'My Item 2', tags: [
{tagFk: 4, name: 'Length', value: 1},
{tagFk: 5, name: 'Color', value: 'blue'}
]
}];
controller.buildTagsFilter(items);
expect(controller.tagValues.length).toEqual(3);
});
});
describe('buildOrderFilter()', () => {
it(`should create an array of non repeated tags plus default filters and then set the orderFields property`, () => {
const items = [
{
id: 1, name: 'My Item 1', tags: [
{tagFk: 4, name: 'Length'},
{tagFk: 5, name: 'Color'}
]
},
{
id: 2, name: 'My Item 2', tags: [
{tagFk: 5, name: 'Color'},
{tagFk: 6, name: 'Relevancy'}
]
}];
controller.buildOrderFilter(items);
expect(controller.orderFields.length).toEqual(7);
});
});
});
});

View File

@ -16,6 +16,7 @@ Item id: Id de artículo
Order by: Ordenar por
Order: Orden
Price: Precio
Color and price: Color y precio
Ascendant: Ascendente
Descendant: Descendente
Created from: Creado desde