Bind and Unbind from Multiselect Event

1 Answer 13 Views
MultiSelect
Lee
Top achievements
Rank 2
Bronze
Bronze
Bronze
Lee asked on 16 May 2024, 09:08 PM

I would like to conditionally bind and unbind events from a kendo UI multiselect's select event. The component was initialized with a select event that must always be fired regardless of the conditions being met. In standard JQuery, I can give an event a namespace such as select.myconditionalevent. Later I can unbind that using that same namespace. Unfortunately, I tried this with the Kendo Multiselect and it didn't work. How would you suggest I add an event that I will later take away, without affecting the initialized event? I can't use the enable disable because I need them to be able to open the dropdown and see what they selected (it is single tag mode and there isn't room on the UI to display a bunch of tags)

Example: 
If (!canSelectStuff) { bind event }

else { unbind myconditionalevent }

1 Answer, 1 is accepted

Sort by
-1
Neli
Telerik team
answered on 21 May 2024, 10:17 AM

Hi Lee,

I would suggest either adding a condition in the select event handler depending on the scenario. Or you can try using the bind and unbind methods that will attach/detach a handler to an event:

https://docs.telerik.com/kendo-ui/api/javascript/observable/methods/bind

https://docs.telerik.com/kendo-ui/api/javascript/observable/methods/unbind

In the dojo example linked here the select event can be bound/unbound on a button click: https://dojo.telerik.com/@NeliK/Onugefox

I hope this helps. 

Regards,
Neli
Progress Telerik

Stay tuned by visiting our public roadmap and feedback portal pages! Or perhaps, if you are new to our Kendo family, check out our getting started resources
Lee
Top achievements
Rank 2
Bronze
Bronze
Bronze
commented on 21 May 2024, 12:26 PM

I think you misunderstood. To use your example dojo, my select event is initialized with a function that needs to always be run. I later need to add / remove a second function to the select event while leaving the initial one in tact. 
Nikolay
Telerik team
commented on 24 May 2024, 10:24 AM

Hi Lee,

I think the best approach here would be setting a condition directly in the select event handler:

     function onSelect(e) {
        if (!canSelectStuff) { 
        // your logic
        }

        else { 
        // logic
        }
      }

Alternatively, you could use the setOptions() method to dynamically update the select event reference:

      multiselect.setOptions({
          select: onSelectNew
        })

Dojo demo: https://dojo.telerik.com/UqOSIFeQ

Regards,

Nikolay

Lee
Top achievements
Rank 2
Bronze
Bronze
Bronze
commented on 24 May 2024, 10:43 AM

Thank you. The set options method sounds like a good idea. 
Tags
MultiSelect
Asked by
Lee
Top achievements
Rank 2
Bronze
Bronze
Bronze
Answers by
Neli
Telerik team
Share this question
or