eakoriakin / ionic-selectable

Ionic Selectable is an Ionic versatile and highly customizable component that serves as a replacement to Ionic Select, and allows to search items, including async search, create items, customize the layout with templates and much more. It provides an intuitive API and is easy to set up and use.
MIT License
549 stars 125 forks source link

confirm select when select an item #401

Closed zeropy009 closed 1 year ago

zeropy009 commented 1 year ago

how can confirm an item when I click it before add it to selected list of ionic-selectable ? I was try with : onSelect(event: { component: IonicSelectableComponent, isSelected: boolean, item: Service }) { if (event.isSelected) { let userResult : boolean; userResult = ..... // ask user to select it or not select event.isSelected = userResult; } }

But it not working, please help me Thank you.

eakoriakin commented 1 year ago

Is this what you are looking for? https://github.com/ionic-selectable/ionic-selectable/wiki#hasconfirmbutton

zeropy009 commented 1 year ago

Is this what you are looking for? https://github.com/ionic-selectable/ionic-selectable/wiki#hasconfirmbutton

my project isMultiple = "true" so hasConfirmButton always true. I just want check a condition when click an item to control the item "selected" or "not selected" I was try change event.isSelected = false; in onSelect() but it still "selected"

eakoriakin commented 1 year ago

So what exactly do you want to happen when you click an item?

zeropy009 commented 1 year ago

So what exactly do you want to happen when you click an item?

I want to control its selection state according to my selection

eakoriakin commented 1 year ago

You can use (onSelect)="onSelect($event)"> event and update a related item (sync its selection state) as shown below.

public onSelect(event): void {
  event.item.isSelected = event.isSelected;
}

Note that item.isSelected is a custom property of your item, you can call it what you like, whereas event.isSelected is a read-only property of the ionic-selectable event.

eakoriakin commented 1 year ago

Also, if you wish to exclude some items from the selection, but keep them visible in the list you can disable those items. Disabled items cannot be selected. https://github.com/ionic-selectable/ionic-selectable/wiki#disableditems image

zeropy009 commented 1 year ago

vent.isSelected is a read-only property of the ionic-selectable event.

my project need check condition then ask user to confirm check or not check so can't disable it. Thank for help.

eakoriakin commented 1 year ago

Is the issue solved?

zeropy009 commented 1 year ago

Is the issue solved?

I think i must custom ionicSelectableItemTemplate no more choice, I don't know why event.isSelected is a read-only property, it should can set value xD

eakoriakin commented 1 year ago

It's a read-only property for a reason. It doesn't belong to an item but simply tells whether the item has been selected or unselected. If it was allowed to set event.isSelected property then the component would need to automatically update its value ([(ngModel)] or [formControl]), which kind of smells.

So in your case, if you want to unselect the selected item based on a condition then you should change the component's value via [(ngModel)] or [formControl] manually.

In addition, it looks like a UX issue to unselect an item that has just been selected. I would try to avoid it and instead apply a validation so that users can select any items but a validation would show a message saying that the selected item isn't correct because it violates some business rules of the app.