Angular reset dropdown values primeng
Hello comunity I need some help with Angular and Primeng components, the scenario is the next.
I have a model:
export class Properties {
public IdFittingConnector: number;
public IdProperty: number;
public IdGroup: number;
public OrderAparitionNumber: number;
public OrderCodePos: number;
public ConnectorDescription: string;
public PropertyDescription;
public Values: FittingPropertiesValues;
public Disabled: boolean;
public SelectedData: FittingPropertiesValues;
}
This model is stored in an array so foreach item in the array I create an select/dropdown
<div *ngFor="let item of arrFittingsCombos; let i = index">
<h4>{{item.PropertyDescription}}</h4>
<p-dropdown [(disabled)]="item.Disabled" [options]="item.Values" optionLabel="IrredutibleFractionValue" [(ngModel)]="item.SelectedData" [showClear]="true" (onChange)="OnFittingComboChange($event)" placeholder="Select Item" [style]="{'width':'100%'}"></p-dropdown>
</div>
Like you can see guys, in the model I have a prop that store the dropdown values called "Values" and on the same model I store the selected option on the prop called "SeloectedData".
Now the big deal is the next.
I have a couple of validations and the problem is that I have two dropdowns but if the second value is > the first drop value I need to reset the second dropdown. I show you the logic code...
if (selectedOption.IdProperty == 5) {
let diam: number = this.arrFittingsCombos.filter(x => x.IdProperty == 1)[0].SelectedData.NumberValue;
if (selectedOption.NumberValue > diam) {
alert("Seleccion invalida, la selección es mayor que el primer diametro");
this.arrFittingsCombos.filter(x => x.IdProperty == 5)[0].SelectedData = null;
this.searchBtnDisabled = true;
}
}
Like you can see I'm resetting the [(ngModel)] and in effect it does but in the dropdown the showed label still exist. What I'm doing wrong? If the first time the validations are ok
First DropDown Value < Second DropDown Value and after that I change the second dropdown value to > first DropDown value the behavior is the desired one but after that the showed label never reset anymore.
Thanks a lot for you time and I hope you can give me a bit of light.
javascript angular typescript primeng
add a comment |
Hello comunity I need some help with Angular and Primeng components, the scenario is the next.
I have a model:
export class Properties {
public IdFittingConnector: number;
public IdProperty: number;
public IdGroup: number;
public OrderAparitionNumber: number;
public OrderCodePos: number;
public ConnectorDescription: string;
public PropertyDescription;
public Values: FittingPropertiesValues;
public Disabled: boolean;
public SelectedData: FittingPropertiesValues;
}
This model is stored in an array so foreach item in the array I create an select/dropdown
<div *ngFor="let item of arrFittingsCombos; let i = index">
<h4>{{item.PropertyDescription}}</h4>
<p-dropdown [(disabled)]="item.Disabled" [options]="item.Values" optionLabel="IrredutibleFractionValue" [(ngModel)]="item.SelectedData" [showClear]="true" (onChange)="OnFittingComboChange($event)" placeholder="Select Item" [style]="{'width':'100%'}"></p-dropdown>
</div>
Like you can see guys, in the model I have a prop that store the dropdown values called "Values" and on the same model I store the selected option on the prop called "SeloectedData".
Now the big deal is the next.
I have a couple of validations and the problem is that I have two dropdowns but if the second value is > the first drop value I need to reset the second dropdown. I show you the logic code...
if (selectedOption.IdProperty == 5) {
let diam: number = this.arrFittingsCombos.filter(x => x.IdProperty == 1)[0].SelectedData.NumberValue;
if (selectedOption.NumberValue > diam) {
alert("Seleccion invalida, la selección es mayor que el primer diametro");
this.arrFittingsCombos.filter(x => x.IdProperty == 5)[0].SelectedData = null;
this.searchBtnDisabled = true;
}
}
Like you can see I'm resetting the [(ngModel)] and in effect it does but in the dropdown the showed label still exist. What I'm doing wrong? If the first time the validations are ok
First DropDown Value < Second DropDown Value and after that I change the second dropdown value to > first DropDown value the behavior is the desired one but after that the showed label never reset anymore.
Thanks a lot for you time and I hope you can give me a bit of light.
javascript angular typescript primeng
add a comment |
Hello comunity I need some help with Angular and Primeng components, the scenario is the next.
I have a model:
export class Properties {
public IdFittingConnector: number;
public IdProperty: number;
public IdGroup: number;
public OrderAparitionNumber: number;
public OrderCodePos: number;
public ConnectorDescription: string;
public PropertyDescription;
public Values: FittingPropertiesValues;
public Disabled: boolean;
public SelectedData: FittingPropertiesValues;
}
This model is stored in an array so foreach item in the array I create an select/dropdown
<div *ngFor="let item of arrFittingsCombos; let i = index">
<h4>{{item.PropertyDescription}}</h4>
<p-dropdown [(disabled)]="item.Disabled" [options]="item.Values" optionLabel="IrredutibleFractionValue" [(ngModel)]="item.SelectedData" [showClear]="true" (onChange)="OnFittingComboChange($event)" placeholder="Select Item" [style]="{'width':'100%'}"></p-dropdown>
</div>
Like you can see guys, in the model I have a prop that store the dropdown values called "Values" and on the same model I store the selected option on the prop called "SeloectedData".
Now the big deal is the next.
I have a couple of validations and the problem is that I have two dropdowns but if the second value is > the first drop value I need to reset the second dropdown. I show you the logic code...
if (selectedOption.IdProperty == 5) {
let diam: number = this.arrFittingsCombos.filter(x => x.IdProperty == 1)[0].SelectedData.NumberValue;
if (selectedOption.NumberValue > diam) {
alert("Seleccion invalida, la selección es mayor que el primer diametro");
this.arrFittingsCombos.filter(x => x.IdProperty == 5)[0].SelectedData = null;
this.searchBtnDisabled = true;
}
}
Like you can see I'm resetting the [(ngModel)] and in effect it does but in the dropdown the showed label still exist. What I'm doing wrong? If the first time the validations are ok
First DropDown Value < Second DropDown Value and after that I change the second dropdown value to > first DropDown value the behavior is the desired one but after that the showed label never reset anymore.
Thanks a lot for you time and I hope you can give me a bit of light.
javascript angular typescript primeng
Hello comunity I need some help with Angular and Primeng components, the scenario is the next.
I have a model:
export class Properties {
public IdFittingConnector: number;
public IdProperty: number;
public IdGroup: number;
public OrderAparitionNumber: number;
public OrderCodePos: number;
public ConnectorDescription: string;
public PropertyDescription;
public Values: FittingPropertiesValues;
public Disabled: boolean;
public SelectedData: FittingPropertiesValues;
}
This model is stored in an array so foreach item in the array I create an select/dropdown
<div *ngFor="let item of arrFittingsCombos; let i = index">
<h4>{{item.PropertyDescription}}</h4>
<p-dropdown [(disabled)]="item.Disabled" [options]="item.Values" optionLabel="IrredutibleFractionValue" [(ngModel)]="item.SelectedData" [showClear]="true" (onChange)="OnFittingComboChange($event)" placeholder="Select Item" [style]="{'width':'100%'}"></p-dropdown>
</div>
Like you can see guys, in the model I have a prop that store the dropdown values called "Values" and on the same model I store the selected option on the prop called "SeloectedData".
Now the big deal is the next.
I have a couple of validations and the problem is that I have two dropdowns but if the second value is > the first drop value I need to reset the second dropdown. I show you the logic code...
if (selectedOption.IdProperty == 5) {
let diam: number = this.arrFittingsCombos.filter(x => x.IdProperty == 1)[0].SelectedData.NumberValue;
if (selectedOption.NumberValue > diam) {
alert("Seleccion invalida, la selección es mayor que el primer diametro");
this.arrFittingsCombos.filter(x => x.IdProperty == 5)[0].SelectedData = null;
this.searchBtnDisabled = true;
}
}
Like you can see I'm resetting the [(ngModel)] and in effect it does but in the dropdown the showed label still exist. What I'm doing wrong? If the first time the validations are ok
First DropDown Value < Second DropDown Value and after that I change the second dropdown value to > first DropDown value the behavior is the desired one but after that the showed label never reset anymore.
Thanks a lot for you time and I hope you can give me a bit of light.
javascript angular typescript primeng
javascript angular typescript primeng
asked Nov 22 at 23:35
Fernando Aguilar
884716
884716
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
why this doesn't work
This is because you are mutation the property SelectedData
on a new array
.
Calling .filter
on an array always returns a filtered copy of the array. So when you mutate this, the ngModel isn't aware of it.
possible solution
I work mostly in react so I'm not sure if mutating a property on your class instance is "accepted" by angular. But lets assume it is...
What you can do is replacing the arrFittingsCombos
with your mutated array, like so:
const newArray = this.arrFittingsCombos.filter(x => x.IdProperty == 5)
newArray[0].SelectedData = null
this.arrFittingsCombos = newArray
add a comment |
I have the solution in the HTML I've added the angular identifier:
#dropdown_i
<div *ngFor="let item of arrFittingsCombos; let i = index">
<h4>{{item.PropertyDescription}}</h4>
<p-dropdown #dropdown_i [(disabled)]="item.Disabled" [options]="item.Values" optionLabel="IrredutibleFractionValue" [(ngModel)]="item.SelectedData" [showClear]="true" (onChange)="OnFittingComboChange($event, drop_i)" placeholder="Select Item" [style]="{'width':'100%'}"></p-dropdown>
</div>
then in the typescript code I receive the dropdown in the parameter.
OnFittingComboChange(event, dropdown: Dropdown) {
//TODO logic code here...
}
finally using a method of the dropdown called dropdown.clear()
I can reset the picked option.
if (drop.value != null && selectedOption.IdProperty == 5) {
let diam: number = this.arrFittingsCombos.filter(x => x.IdProperty == 1)[0].SelectedData.NumberValue;
if (selectedOption.NumberValue > diam) {
this.messageService.add({ severity: 'warn', summary: 'Validación', detail: 'Seleccion invalida, la selección es mayor que el primer diametro' });
drop.clear(null);
this.searchBtnDisabled = true;
}
}
now when the condition is ok the element is cleared correctly and the behavior is the desired.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53439091%2fangular-reset-dropdown-values-primeng%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
why this doesn't work
This is because you are mutation the property SelectedData
on a new array
.
Calling .filter
on an array always returns a filtered copy of the array. So when you mutate this, the ngModel isn't aware of it.
possible solution
I work mostly in react so I'm not sure if mutating a property on your class instance is "accepted" by angular. But lets assume it is...
What you can do is replacing the arrFittingsCombos
with your mutated array, like so:
const newArray = this.arrFittingsCombos.filter(x => x.IdProperty == 5)
newArray[0].SelectedData = null
this.arrFittingsCombos = newArray
add a comment |
why this doesn't work
This is because you are mutation the property SelectedData
on a new array
.
Calling .filter
on an array always returns a filtered copy of the array. So when you mutate this, the ngModel isn't aware of it.
possible solution
I work mostly in react so I'm not sure if mutating a property on your class instance is "accepted" by angular. But lets assume it is...
What you can do is replacing the arrFittingsCombos
with your mutated array, like so:
const newArray = this.arrFittingsCombos.filter(x => x.IdProperty == 5)
newArray[0].SelectedData = null
this.arrFittingsCombos = newArray
add a comment |
why this doesn't work
This is because you are mutation the property SelectedData
on a new array
.
Calling .filter
on an array always returns a filtered copy of the array. So when you mutate this, the ngModel isn't aware of it.
possible solution
I work mostly in react so I'm not sure if mutating a property on your class instance is "accepted" by angular. But lets assume it is...
What you can do is replacing the arrFittingsCombos
with your mutated array, like so:
const newArray = this.arrFittingsCombos.filter(x => x.IdProperty == 5)
newArray[0].SelectedData = null
this.arrFittingsCombos = newArray
why this doesn't work
This is because you are mutation the property SelectedData
on a new array
.
Calling .filter
on an array always returns a filtered copy of the array. So when you mutate this, the ngModel isn't aware of it.
possible solution
I work mostly in react so I'm not sure if mutating a property on your class instance is "accepted" by angular. But lets assume it is...
What you can do is replacing the arrFittingsCombos
with your mutated array, like so:
const newArray = this.arrFittingsCombos.filter(x => x.IdProperty == 5)
newArray[0].SelectedData = null
this.arrFittingsCombos = newArray
edited Nov 22 at 23:48
answered Nov 22 at 23:42
Y. Gherbi
18218
18218
add a comment |
add a comment |
I have the solution in the HTML I've added the angular identifier:
#dropdown_i
<div *ngFor="let item of arrFittingsCombos; let i = index">
<h4>{{item.PropertyDescription}}</h4>
<p-dropdown #dropdown_i [(disabled)]="item.Disabled" [options]="item.Values" optionLabel="IrredutibleFractionValue" [(ngModel)]="item.SelectedData" [showClear]="true" (onChange)="OnFittingComboChange($event, drop_i)" placeholder="Select Item" [style]="{'width':'100%'}"></p-dropdown>
</div>
then in the typescript code I receive the dropdown in the parameter.
OnFittingComboChange(event, dropdown: Dropdown) {
//TODO logic code here...
}
finally using a method of the dropdown called dropdown.clear()
I can reset the picked option.
if (drop.value != null && selectedOption.IdProperty == 5) {
let diam: number = this.arrFittingsCombos.filter(x => x.IdProperty == 1)[0].SelectedData.NumberValue;
if (selectedOption.NumberValue > diam) {
this.messageService.add({ severity: 'warn', summary: 'Validación', detail: 'Seleccion invalida, la selección es mayor que el primer diametro' });
drop.clear(null);
this.searchBtnDisabled = true;
}
}
now when the condition is ok the element is cleared correctly and the behavior is the desired.
add a comment |
I have the solution in the HTML I've added the angular identifier:
#dropdown_i
<div *ngFor="let item of arrFittingsCombos; let i = index">
<h4>{{item.PropertyDescription}}</h4>
<p-dropdown #dropdown_i [(disabled)]="item.Disabled" [options]="item.Values" optionLabel="IrredutibleFractionValue" [(ngModel)]="item.SelectedData" [showClear]="true" (onChange)="OnFittingComboChange($event, drop_i)" placeholder="Select Item" [style]="{'width':'100%'}"></p-dropdown>
</div>
then in the typescript code I receive the dropdown in the parameter.
OnFittingComboChange(event, dropdown: Dropdown) {
//TODO logic code here...
}
finally using a method of the dropdown called dropdown.clear()
I can reset the picked option.
if (drop.value != null && selectedOption.IdProperty == 5) {
let diam: number = this.arrFittingsCombos.filter(x => x.IdProperty == 1)[0].SelectedData.NumberValue;
if (selectedOption.NumberValue > diam) {
this.messageService.add({ severity: 'warn', summary: 'Validación', detail: 'Seleccion invalida, la selección es mayor que el primer diametro' });
drop.clear(null);
this.searchBtnDisabled = true;
}
}
now when the condition is ok the element is cleared correctly and the behavior is the desired.
add a comment |
I have the solution in the HTML I've added the angular identifier:
#dropdown_i
<div *ngFor="let item of arrFittingsCombos; let i = index">
<h4>{{item.PropertyDescription}}</h4>
<p-dropdown #dropdown_i [(disabled)]="item.Disabled" [options]="item.Values" optionLabel="IrredutibleFractionValue" [(ngModel)]="item.SelectedData" [showClear]="true" (onChange)="OnFittingComboChange($event, drop_i)" placeholder="Select Item" [style]="{'width':'100%'}"></p-dropdown>
</div>
then in the typescript code I receive the dropdown in the parameter.
OnFittingComboChange(event, dropdown: Dropdown) {
//TODO logic code here...
}
finally using a method of the dropdown called dropdown.clear()
I can reset the picked option.
if (drop.value != null && selectedOption.IdProperty == 5) {
let diam: number = this.arrFittingsCombos.filter(x => x.IdProperty == 1)[0].SelectedData.NumberValue;
if (selectedOption.NumberValue > diam) {
this.messageService.add({ severity: 'warn', summary: 'Validación', detail: 'Seleccion invalida, la selección es mayor que el primer diametro' });
drop.clear(null);
this.searchBtnDisabled = true;
}
}
now when the condition is ok the element is cleared correctly and the behavior is the desired.
I have the solution in the HTML I've added the angular identifier:
#dropdown_i
<div *ngFor="let item of arrFittingsCombos; let i = index">
<h4>{{item.PropertyDescription}}</h4>
<p-dropdown #dropdown_i [(disabled)]="item.Disabled" [options]="item.Values" optionLabel="IrredutibleFractionValue" [(ngModel)]="item.SelectedData" [showClear]="true" (onChange)="OnFittingComboChange($event, drop_i)" placeholder="Select Item" [style]="{'width':'100%'}"></p-dropdown>
</div>
then in the typescript code I receive the dropdown in the parameter.
OnFittingComboChange(event, dropdown: Dropdown) {
//TODO logic code here...
}
finally using a method of the dropdown called dropdown.clear()
I can reset the picked option.
if (drop.value != null && selectedOption.IdProperty == 5) {
let diam: number = this.arrFittingsCombos.filter(x => x.IdProperty == 1)[0].SelectedData.NumberValue;
if (selectedOption.NumberValue > diam) {
this.messageService.add({ severity: 'warn', summary: 'Validación', detail: 'Seleccion invalida, la selección es mayor que el primer diametro' });
drop.clear(null);
this.searchBtnDisabled = true;
}
}
now when the condition is ok the element is cleared correctly and the behavior is the desired.
answered Nov 23 at 18:46
Fernando Aguilar
884716
884716
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53439091%2fangular-reset-dropdown-values-primeng%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown