Added datalist auto completion
gitea/salix/2034-postcode_autocomplete There was a failure building this commit Details

This commit is contained in:
Joan Sanchez 2020-02-24 11:27:36 +01:00
parent 75d7125426
commit 3643709a80
11 changed files with 210 additions and 135 deletions

View File

@ -27,7 +27,7 @@ export default class Datalist extends Textfield {
value = value == '' || value == null ? null : value;
oldValue = oldValue == '' || oldValue == null ? null : oldValue;
this.refreshSelection();
if (oldValue === undefined) this.refreshSelection();
if (!value || value === oldValue && this.modelData != null) return;

View File

@ -39,61 +39,53 @@
</vn-textfield>
</vn-horizontal>
<vn-horizontal>
<vn-autocomplete
vn-one
vn-id="province"
<vn-autocomplete vn-id="province" vn-one
label="Province"
ng-model="$ctrl.address.provinceId"
url="Provinces"
url="Provinces/location"
fields="['id', 'name', 'countryFk']"
show-field="name"
value-field="id"
label="Province">
rule>
<tpl-item>{{name}} ({{country.country}})</tpl-item>
</vn-autocomplete>
<vn-textfield
vn-one
<vn-datalist vn-id="town" vn-one
label="City"
ng-model="$ctrl.address.city"
rule>
</vn-textfield>
<!-- <vn-autocomplete vn-id="town" vn-one
label="City"
url="Towns"
fields="['id', 'name']"
selection="$ctrl.town"
url="Towns/location"
fields="['id', 'name', 'provinceFk']"
where="{provinceFk: province.selection.id}"
show-field="name"
value-field="name"
ng-model="$ctrl.address.city">
</vn-autocomplete>
<vn-autocomplete vn-one
url="Postcodes/location"
fields="['code', 'townFk']"
ng-model="$ctrl.address.postalCode"
selection="$ctrl.postcodeSelection"
search-function="{code: $search}"
where="{townFk: town.selection.id}"
order="code, townFk"
show-field="code"
value-field="code"
label="Postcode">
value-field="name">
<tpl-item>
{{code}}, {{town.name}} - {{town.province.name}}
({{town.province.country.country}})
{{name}}, {{province.name}}
({{province.country.country}})
</tpl-item>
</vn-autocomplete> -->
<vn-textfield
vn-one
</vn-datalist>
<vn-datalist vn-one
label="Postcode"
ng-model="$ctrl.address.postalCode"
selection="$ctrl.postcode"
url="Postcodes/location"
fields="['code','townFk']"
where="{townFk: town.selection.id}"
order="code, townFk"
value-field="code"
show-field="code"
rule>
</vn-textfield>
<!--
<vn-icon-button
vn-auto
class="vn-my-md"
icon="add_circle"
vn-tooltip="New postcode"
ng-click="postcode.open()">
</vn-icon-button>
-->
<tpl-item>
{{code}} - {{town.name}} ({{town.province.name}},
{{town.province.country.country}})
</tpl-item>
<append>
<vn-icon-button
icon="add_circle"
vn-tooltip="New postcode"
ng-click="postcode.open()">
</vn-icon-button>
</append>
</vn-datalist>
</vn-horizontal>
<vn-horizontal>
<vn-autocomplete

View File

@ -11,26 +11,6 @@ export default class Controller extends Component {
};
}
get postcodeSelection() {
return this._postcodeSelection;
}
set postcodeSelection(selection) {
this._postcodeSelection = selection;
if (!selection) return;
const town = selection.town;
const province = town.province;
this.address.city = town.name;
this.address.provinceFk = province.id;
}
onResponse(response) {
this.address.postalCode = response.code;
}
onSubmit() {
this.$.watcher.submit().then(res => {
if (this.address.isDefaultAddress)
@ -51,6 +31,46 @@ export default class Controller extends Component {
return this.$http.post(`CustomsAgents`, this.newCustomsAgent)
.then(res => this.address.customsAgentFk = res.data.id);
}
get town() {
return this._town;
}
// Town auto complete
set town(selection) {
this._town = selection;
if (!selection) return;
const province = selection.province;
const postcodes = selection.postcodes;
this.address.provinceId = province.id;
if (postcodes.length === 1)
this.address.postalCode = postcodes[0].code;
}
get postcode() {
return this._postcode;
}
// Postcode auto complete
set postcode(selection) {
this._postcode = selection;
if (!selection) return;
const town = selection.town;
const province = town.province;
this.address.city = town.name;
this.address.provinceId = province.id;
}
onResponse(response) {
this.address.postalCode = response.code;
}
}
Controller.$inject = ['$element', '$scope'];

View File

@ -65,49 +65,43 @@
value-field="id"
label="Province">
</vn-autocomplete>
<vn-textfield
vn-one
<vn-datalist vn-id="town" vn-one
label="City"
ng-model="$ctrl.address.city"
rule>
</vn-textfield>
<!--
<vn-autocomplete vn-id="town" vn-one
label="City"
url="Towns"
fields="['id', 'name']"
selection="$ctrl.town"
url="Towns/location"
fields="['id', 'name', 'provinceFk']"
where="{provinceFk: province.selection.id}"
show-field="name"
value-field="name"
ng-model="$ctrl.address.city">
</vn-autocomplete>
<vn-autocomplete vn-one
url="Postcodes/location"
fields="['code', 'townFk']"
ng-model="$ctrl.address.postalCode"
where="{townFk: town.selection.id}"
search-function="{code: $search}"
order="code, townFk"
show-field="code"
value-field="code"
label="Postcode">
</vn-autocomplete>
-->
<vn-textfield
vn-one
value-field="name">
<tpl-item>
{{name}}, {{province.name}}
({{province.country.country}})
</tpl-item>
</vn-datalist>
<vn-datalist vn-one
label="Postcode"
ng-model="$ctrl.address.postalCode"
selection="$ctrl.postcode"
url="Postcodes/location"
fields="['code','townFk']"
where="{townFk: town.selection.id}"
order="code, townFk"
value-field="code"
show-field="code"
rule>
</vn-textfield>
<!-- <vn-icon-button
vn-auto
class="vn-my-md"
icon="add_circle"
vn-tooltip="New postcode"
ng-click="postcode.open()">
</vn-icon-button>
-->
<tpl-item>
{{code}} - {{town.name}} ({{town.province.name}},
{{town.province.country.country}})
</tpl-item>
<append>
<vn-icon-button
icon="add_circle"
vn-tooltip="New postcode"
ng-click="postcode.open()">
</vn-icon-button>
</append>
</vn-datalist>
</vn-horizontal>
<vn-horizontal>
<vn-autocomplete vn-one

View File

@ -15,10 +15,6 @@ export default class Controller extends Component {
this.$state.go('client.card.address.index');
}
onResponse(response) {
this.address.postalCode = response.code;
}
onSubmit() {
this.$.watcher.submit()
.then(() => this.$.model.save(true))
@ -39,6 +35,50 @@ export default class Controller extends Component {
return this.$http.post(`CustomsAgents`, this.newCustomsAgent)
.then(res => this.address.customsAgentFk = res.data.id);
}
get town() {
return this._town;
}
// Town auto complete
set town(selection) {
const oldValue = this._town;
this._town = selection;
if (!selection || oldValue === null || oldValue === undefined)
return;
const province = selection.province;
const postcodes = selection.postcodes;
this.address.provinceFk = province.id;
if (postcodes.length === 1)
this.address.postalCode = postcodes[0].code;
}
get postcode() {
return this._postcode;
}
// Postcode auto complete
set postcode(selection) {
const oldValue = this._postcode;
this._postcode = selection;
if (!selection || oldValue === null || oldValue === undefined)
return;
const town = selection.town;
const province = town.province;
this.address.city = town.name;
this.address.provinceFk = province.id;
}
onResponse(response) {
this.address.postalCode = response.code;
}
}
ngModule.component('vnClientAddressEdit', {

View File

@ -96,9 +96,16 @@
show-field="code"
rule>
<tpl-item>
{{code}}, {{town.name}} - {{town.province.name}}
({{town.province.country.country}})
{{code}} - {{town.name}} ({{town.province.name}},
{{town.province.country.country}})
</tpl-item>
<append>
<vn-icon-button
icon="add_circle"
vn-tooltip="New postcode"
ng-click="postcode.open()">
</vn-icon-button>
</append>
</vn-datalist>
</vn-horizontal>
<vn-horizontal>

View File

@ -12,10 +12,6 @@ export default class Controller {
};
}
onResponse(response) {
this.client.postcode = response.code;
}
onSubmit() {
return this.$.watcher.submit().then(
json => this.$state.go('client.card.basicData', {id: json.data.id})
@ -76,6 +72,10 @@ export default class Controller {
this.client.provinceFk = province.id;
this.client.countryFk = country.id;
}
onResponse(response) {
this.client.postcode = response.code;
}
}
Controller.$inject = ['$scope', '$state', '$http', '$translate', 'vnApp'];

View File

@ -64,7 +64,10 @@
where="{provinceFk: province.selection.id}"
show-field="name"
value-field="name">
<tpl-item>{{name}} ({{province.name}}, {{province.country.country}})</tpl-item>
<tpl-item>
{{name}}, {{province.name}}
({{province.country.country}})
</tpl-item>
</vn-datalist>
<vn-datalist vn-one
label="Postcode"
@ -77,7 +80,17 @@
value-field="code"
show-field="code"
rule>
<tpl-item>{{code}} - {{town.name}} ({{town.province.name}}, {{town.province.country.country}})</tpl-item>
<tpl-item>
{{code}} - {{town.name}} ({{town.province.name}},
{{town.province.country.country}})
</tpl-item>
<append>
<vn-icon-button
icon="add_circle"
vn-tooltip="New postcode"
ng-click="postcode.open()">
</vn-icon-button>
</append>
</vn-datalist>
</vn-horizontal>
<vn-horizontal>
@ -148,3 +161,8 @@
message="Found a client with this data"
on-accept="$ctrl.onAcceptDuplication()">
</vn-confirm>
<!-- New postcode dialog -->
<vn-client-postcode
vn-id="postcode"
on-response="$ctrl.onResponse($response)">
</vn-client-postcode>

View File

@ -86,7 +86,8 @@ export default class Controller extends Component {
const oldValue = this._province;
this._province = selection;
if (!selection || !oldValue) return;
if (!selection || oldValue === undefined)
return;
const country = selection.country;
@ -102,7 +103,8 @@ export default class Controller extends Component {
const oldValue = this._town;
this._town = selection;
if (!selection || !oldValue) return;
if (!selection || oldValue === undefined)
return;
const province = selection.province;
const country = province.country;
@ -123,17 +125,24 @@ export default class Controller extends Component {
set postcode(selection) {
const oldValue = this._postcode;
this._postcode = selection;
console.log(selection);
if (!selection || !oldValue) return;
console.log(oldValue);
if (!selection || oldValue === undefined)
return;
console.log('setter');
const town = selection.town;
const province = town.province;
const country = province.country;
console.log(province.id);
this.client.city = town.name;
this.client.provinceFk = province.id;
this.client.countryFk = country.id;
}
onResponse(response) {
this.client.postcode = response.code;
}
}
ngModule.component('vnClientFiscalData', {

View File

@ -1,7 +1,7 @@
<vn-dialog class="edit"
vn-id="postcodeDialog"
on-open="$ctrl.onOpen()"
on-response="$ctrl.onResponse($response)">
on-accept="$ctrl.onAccept()">
<tpl-body>
<h5 class="vn-py-sm" translate>New postcode</h5>
<p translate>Please, ensure you put the correct data!</p>

View File

@ -35,25 +35,20 @@ class Controller extends Component {
this.$.postcode.focus();
}
onResponse(response) {
if (response == 'accept') {
try {
if (!this.data.code)
throw new Error(`The postcode can't be empty`);
if (!this.data.townFk)
throw new Error(`The town can't be empty`);
onAccept() {
try {
if (!this.data.code)
throw new Error(`The postcode can't be empty`);
if (!this.data.townFk)
throw new Error(`The town can't be empty`);
this.$http.patch(`postcodes`, this.data).then(response => {
if (response.data) {
this.vnApp.showMessage(this.$translate.instant('The postcode has been saved'));
this.emit('response', {response: response.data});
}
});
} catch (e) {
this.vnApp.showError(this.$translate.instant(e.message));
return false;
}
this.$http.patch(`postcodes`, this.data).then(res => {
this.vnApp.showMessage(this.$translate.instant('The postcode has been saved'));
this.emit('response', {$response: res.data});
});
} catch (e) {
this.vnApp.showError(this.$translate.instant(e.message));
return false;
}
return true;
}