primeng icon indicating copy to clipboard operation
primeng copied to clipboard

Dropdown and possibility to clear selected option with onChange event fired.

Open sebaiona opened this issue 9 years ago • 4 comments

There is no guarantee in receiving a response in GitHub Issue Tracker, If you'd like to secure our response, you may consider PrimeNG PRO Support where support is provided within 4 business hours

I'm submitting a ... (check one with "x")

[ x ] bug report => Search github for a similar issue or PR before submitting
[ ] feature request => Please check if request is not on the roadmap already https://github.com/primefaces/primeng/wiki/Roadmap
[ ] support request => Please do not submit support request here, instead see http://forum.primefaces.org/viewforum.php?f=35

Current behavior

I don't have a button to clear the selected option in the dropdown component. I tried to make a button executing on click something like "myDropdown.updateSelectedOption(null)" but it doesn't fire the onChange event of 'myDropdown'.

Expected behavior

2 possibilities :

  • a button inside the dropdown component (like in p-calendar) to clear the selected option
  • a method doing it (or fire 'onChange' event in the function 'updateSelectedOption')

The important thing is that I'd need the possibility to clear the selected option AND execute the behavior I put on the 'onChange' event of the dropdown.

  • Angular version: 4.4.6
  • PrimeNG version: 4.3.0
  • Browser: all
  • Language: TypeScript

sebaiona avatar Nov 17 '17 11:11 sebaiona

Since you have access to the variable that you use in the [(ngModel)] property why don't you clear it instead? Here I put together a simple Plunkr example. And in that clear method you can also execute other code as well.

dzhavat avatar Nov 17 '17 20:11 dzhavat

I can hear your solution, but it's not really the best one in my case... Every time I change the selected option of the dropdown, I filter a p-datatable.

With your solution I'd have to put the same code on the onChange and in the clear method.

I have multiple dropdowns in the page and the case will be reproduced in my application, so every time I will be in this situation I'll have to do the same calls twice (on the onChange and on the clear)...

A solution would be to watch changes on the ngModel and execute the filtering at this time instead of doing it on the onChange event... Not the solution I prefer ...

sebaiona avatar Nov 20 '17 08:11 sebaiona

I'd go with the solution of @dzhavat

cagataycivici avatar Dec 18 '17 15:12 cagataycivici

This is kind of what I did...

To have this behavior everywhere I need I made a component with the p-dropdown and a "clear button". When I click on the "clear button", I update the selected option with null and I fire programmatically the onChange event otherwise (what was very important in my case because I have an Ajax call on this event).

sebaiona avatar Dec 18 '17 15:12 sebaiona