Tarea #1534 Paginar consignatarios
gitea/salix/dev This commit looks good Details

This commit is contained in:
Bernat 2019-06-18 09:34:32 +02:00
parent c02d576bb9
commit 82ab9e5ac5
4 changed files with 122 additions and 89 deletions

View File

@ -0,0 +1,53 @@
const ParameterizedSQL = require('loopback-connector').ParameterizedSQL;
module.exports = Self => {
Self.remoteMethod('filter', {
description: 'Find all instances of the model matched by filter from the data source.',
accessType: 'READ',
accepts: [
{
arg: 'filter',
type: 'Object',
description: 'Filter defining where, order, offset, and limit - must be a JSON-encoded string',
http: {source: 'query'}
}
],
returns: {
type: ['Object'],
root: true
},
http: {
path: `/filter`,
verb: 'GET'
}
});
Self.filter = async filter => {
let conn = Self.dataSource.connector;
let stmts = [];
let stmt;
filter.order = [
'c.defaultAddressFk DESC',
'a.isActive DESC',
'a.nickname ASC'
];
stmt = new ParameterizedSQL(
`SELECT a.*
FROM vn.address a
LEFT JOIN vn.client c ON c.defaultAddressFk = a.id`
);
stmt.merge(conn.makeSuffix(filter));
let itemsIndex = stmts.push(stmt) - 1;
let sql = ParameterizedSQL.join(stmts, ';');
let result = await conn.executeStmt(sql);
return itemsIndex === 0 ? result : result[itemsIndex];
};
};

View File

@ -5,6 +5,7 @@ let isMultiple = require('vn-loopback/util/hook').isMultiple;
module.exports = Self => {
// Methods
require('../methods/address/createDefaultAddress')(Self);
require('../methods/address/filter')(Self);
Self.validateAsync('isEqualizated', cannotHaveET, {
message: 'Cannot check Equalization Tax in this NIF/CIF'

View File

@ -1,12 +1,13 @@
<vn-crud-model
vn-id="model"
url="/client/api/Addresses"
filter="::$ctrl.filter"
url="/client/api/Addresses/filter"
limit="10"
link="{clientFk: $ctrl.$stateParams.id}"
data="$ctrl.addresses"
auto-load="true">
auto-load="false">
</vn-crud-model>
<div compact>
<vn-table model="model">
<vn-card pad-large>
<vn-horizontal
ng-repeat="address in $ctrl.addresses"
@ -67,6 +68,7 @@
</vn-horizontal>
</vn-one>
</vn-horizontal>
</vn-table>
</vn-card>
<vn-float-button
vn-bind="+"
@ -76,4 +78,5 @@
icon="add"
label="Add">
</vn-float-button>
<vn-pagination model="model"></vn-pagination>
</div>

View File

@ -5,30 +5,6 @@ class Controller {
this.$http = $http;
this.$scope = $scope;
this.$stateParams = $stateParams;
this.filter = {
include: {
observations: 'observationType'
},
order: ['isActive DESC', 'nickname ASC']
};
}
get client() {
return this._client;
}
set client(value) {
this._client = value;
this.sortAddresses();
}
get addresses() {
return this._addresses;
}
set addresses(value) {
this._addresses = value;
this.sortAddresses();
}
setDefault(address) {
@ -43,7 +19,7 @@ class Controller {
}
isDefaultAddress(address) {
if (!this.client) return;
if (this.client)
return this.client.defaultAddressFk === address.id;
}
@ -52,7 +28,7 @@ class Controller {
*/
sortAddresses() {
if (!this.client || !this.addresses) return;
this.$scope.model.data = this.addresses.sort((a, b) => {
this.addresses = this.addresses.sort((a, b) => {
return this.isDefaultAddress(b) - this.isDefaultAddress(a);
});
}