vue3 click event interferes with bootstrap js processing of dropdown - user-interface

This is my first question asked here, so please be forgiving.
Asked the same question on dev.to, got no comment or answer after 1 week...
I'm using vue3, vite, bootstrap.
I'm working on a small app. One page loads some data from my server and passes it to a component to render a bootstrap dropdown. If the user selects an item from dropdown, the button type/color should change.
The first time I open the dropdown and select something, the color changes but the dropdown does not close. If I click the main dropdown button again, it closes, after that the dropdown works as intended.
I nailed down the problem to line 20 in the child component,
selectOption(optionId, event) {
this.bSelectedOption = true;
where vue listens for the #click event and sets a boolean value to true, which changes classes in the template (line 29)
<button
:class="bSelectedOption ? 'btn-success' : 'btn-danger'"
class="btn dropdown-toggle"
Commenting out line 20 or removing the dynamic
:class
makes bootstrap dropdown close on clik, but obviously does not change the color anymore.
example code on codesandbox.io
I guess the first time around something happens in the event chain of click, maybe bootstrap get's some unfinished data while vue is working on the classes? But why opening the dropdown a second and nth time works flawlessly?
I hope to have some sort of feedback to this problem in order to find a solution :)

Related

Reinitialize bootstrap modals after loading ajax content

I'm using Bootstrap Modals, with normal usage cases it works well, but when adding new content via AJAX I need to make it remap the newly added elements.
I need that function like bootstrap_modal() or bootstrap_modal.init() to be fired after the new content was added.
I have a table of elements, each element has a modal that contains more information about it, these elements are added using AJAX (with a click of a button) so I want that when I click on that button, the new content loads and the the modal remapping is triggered to take into account the new elements.
I don't have enough rep. to leave a comment on your question, but can you paste some code and let me know what programming language you're using? I have examples in both c#/ASP.NET and php.

How to add data in popup using primefaces and ajax call

I am new in JSF, facing a problem while executing GET AJAX request.
I have a icon with a counter (number), once I will do mouse over to the icon it shows a kind of small popup with small list (3 items), kind of same behavior as we have in Social networking sites (Notification icon). Till here All good. Now in my pop up at bottom side, I added a text says "Show more". This should get 3 more items/ notification from the DB via Ajax call and add the response in the popup (without closing the popup), then in total there should be 6 items.
I am not sure how exactly I can achieve this, Please help.
Using <h:outputText value="show more}"> in my xhtml.
In my bean I have a method to getMoreNotification().
Recently I tried with <p:remoteCommand>, but not sure how I can add responce/ data in popup.
Thanks in advance.
Not that difficult
Create a list which is initially populated with 3 items
Create a <p:overlay> with IN that <p:overlay> e.g. a <p:dataList> that shows these list mentioned in 1. Give this component an id, e.g. 'notifications'
When clicking on the 'show more' commandLink, execute the getMoreNotification() via an actionListener and IN that method update the list mentioned in 1. Also make sure you have an update attribute that contains the value of the <p:dataList>.

Kendo UI TabStrip e.preventDefault() doesn't work

Two problems:
1.)
e.preventDefault() doesn't work correctly with Kendo UI TabStrip when somewhere
$("#tabstrip").kendoTabStrip().data('kendoTabStrip');
appears.
2.)
Imagine the User clicks on another tab, but has unsaved changes.
A dialog pops up and ask if he wants to discard the changes and go to the tab or
if he wants to stay on the active tab to save his changes.
My solution doesn't work. Because of the 1. Problem I guess and because
.data() somehow reinitialises the TabStrip?! What is wrong?
Here is a (not) working example
http://jsfiddle.net/Nakkvarr/w9586/
Any ideas on this issue?
The reason it doesn't work for the first tab is because you initialized the tab strip twice on the same element $('#tabstrip'). Since you bound the select event on the first initialization, the subsequent initialization overwrote it (the select event isn't handled anymore). You even answered the problem yourself by stating that it works if you comment the second initialization line out.
I'm not entirely sure what you're trying to accomplish with the setTimeout() function in the second example. It's unnecessary.
Using e.preventDefault() works as expected. JSFiddle: http://jsfiddle.net/w9586/6/

How to preserve bootstrap dropdown state on Meteor

I have a bootstrap dropdown button for each table row.
When the dropdown is clicked it shows a small form with some input fields.
When the user submits this form data or the row gets redrawn the dropdown closes.
I tried preserving the dropdown state using the Template.preserve() method, similar to inputs but with no success.
Some suggested using {{#constant}} directive surrounding the dropdown I also have some reactive content inside the form that needs re-rendering, so this options is not for me.
I have noticed this issue with reactive templates and there is no good solution that I know of. This is somewhat of a hack but it works.
Option 1
Manually add and remove a css class to your dropdown. Use session variables to save the state of the dropdown.
This should be the default state of your dropdown... class="dropdown"
When a user clicks on the dropdown use a session variable to save its state and add 'open' to the HTML class attribute. class="dropdown open"
When a user closes the dropdown remove 'open'
Option 2:
1. User clicks dropdown button.
2. Save dropdown button ID as active using a session variable.
3. Use the following callback to reopen your dropdown... Template.myTemplate.rendered = function ( ) {
if (dropdown = active) {
$().dropdown('toggle'); //instead of toggle try 'open'
}
}
I haven't tried this with dropdowns yet but I was having the same issue with modals disappearing every time the template kept rendering. I tried the second option and it didn't work that great. I ended up using something like the first option to solve the issue and it worked great.

Why some time jquery click event fails?

My page is divided into two parts vertically.Left part is like a menu section. Clicking on
any menu brings the proper data related to that menu in the right part of the page. I am
doing it using ajax call and the only div on the right part get refreshed. I am using jquery click event for that ..
say:
$("#left_part").click(function() { // ajax call here });
Now I have some more jquery action on the right part. (say Show hide some div which also work on click event.)
But when new div reloads on the right part those click event on the right part is not working.
But when I bind the click event it works.
say:
$("#some_right_part").click(function() {/ some hide show here )}; Not working
$("#some_right_part").bind('click', function(event){ // some hide show here )}; works fine
Important: When I am on fresh page (no ajax call yet to bring right part) $("#some_right_part").click(function() {/ some hide show here )}; works fine.
But what I know is: $().click(function) calls $().bind('click',function)
So, What is the reason behind this? Or What is the perfect way to solve such problem
Thanks
When you assign a click event via $().click when the page loads, the click event is bound to all matching elements. However, when you do the ajax call and recieve new markup - the new elements are not bound to the click event because they did not exist when you first called $().click.
There is a way to get a click event to be bound to all elements and all future elements that may be created. You must use the live method like so:
$('#elements').live('click', function() {
// do logic
});

Resources