Removing selected option in multiple select2 clears all options - jquery-select2

Using Laravel livewire, bootstrap 5 modal and select 2.
Select2 (s2) without multiple works fine. It's populated, selections save to the component, clearing is also fine.
When an s2 has multiple attribute, removing a selected option clears all the options and nothing is displayed in the results drop down. When adding a selection, this doesn't happen.
If I do a dispatchBrowserEvent and re-init the s2 it does work again.
My question is why does removing an s2 option require a re-init but an add doesn't.
Seems to be something to do with the rendering in render().
Some code...
Blade:
<div wire:ignore>
<select id="selectedTrades" wire:model.defer="selectedTrades" class="select-2" multiple="multiple" data-field="selectedTrades" data-allow-clear="false">
#foreach ($contractor_trades as $trade)
<option value="{{ $trade['id'] }}">{{ $trade['name'] }}</option>
#endforeach
</div>
Standard stuff. wire.ignore around the s2. The modal has wire:ignore.self.
JS:
$('.select-2').on('change', function (e)
{
var $this = $(this);
livewire.emit('setSelect2Property', $this.attr('data-field'), $this.val());
});
Component:
public function setSelect2Property($property, $value)
{
$this->$property = $value;
}
Just sets $this->selectedTrades to the array sent.
Adding:
The drop down has the options as expected.
Removing:
The thin light line under it is the container for the results that's now empty. The original select element does still have its options.
If I remove the livewire.emit from the js to test, the s2 behaves correctly.
I think it has to do with the components render method. I can't figure out what is happening different when I add versus remove.

I got this to work. The problem was the dropdownParent element I was using. I had it set to the modal. Setting it to the immediate parent element and it worked as expected.

Related

Issues with using Select2 to jump to page anchors

I have a long page organized by state. I have a Select2 element at the top of the page that I would like to use to let visitors select a state and jump to that state further down the page via an anchor.
I’ll post my code in a bit, but let me first describe the issue. My solution works in most browsers, but I’ve discovered that it fails in IE11 and Edge. In those browsers, I see the desired content location flicker briefly into view before the page gets pulled back to having the Select2 element at the top of the page. It’s like Select2 really wants to be in view.
Here is my (simplified) code:
HTML
<select class="jumpmenu">
<option value="">Jump to a State</option>
<option value="#alabama">Alabama</option>
<option value="#alaska">Alaska</option>
[ ... ]
</select>
<a name="alabama" id="alabama"> </a>
<h2>Alabama</h2>
<a name="alaska" id="alaska"> </a>
<h2>Alaska</h2>
[ ... ]
JQUERY
$(".jumpmenu").select2();
$('.jumpmenu').on('select2:select', function (e) {
window.location.hash = this.options[this.selectedIndex].value;
});
Is there a better way for me to construct this jump menu so that it will jump properly in IE and Edge?
I got a few suggestions from John30013 on the Select2 forums, and one of them worked well for me. I ended up setting a short setTimeout, which allowed the select to close before jumping to the anchor. Here's the code that worked for me:
$('.jumpmenu').on('select2:select', function (e) {
newHash = this.options[this.selectedIndex].value;
setTimeout(function(){ window.location.hash = newHash;},100);
});

Imposible to use core-a11y-keys with Inputs

Dart, Polymer 0.5, Dartium.
In a page I have some div element with core-a11y-keys inside, keys are "up down left right". It works perfectly, some actions are happened after key down.
Also I have input field on the page. And problem is I can't use arrow keys in it because of core-a11y-keys.
Question is: how to avoid destruction behavior?
HTML:
<body>
<div id="widgetContainer">
<core-a11y-keys target="{{body}}" keys="up down left right"
on-keys-pressed="{{widgetContainer_on_move_keys}}">
</core-a11y-keys>
</div>
<input id="txtInput">
</body>
Make sure that the target attribute of core-a11y-keys is in fact present and targeting the div, otherwise it will apply to the whole page including your input. See here for more detail on how to do that: https://groups.google.com/a/dartlang.org/forum/m/#!topic/web/k8Wzj6KCfgM
If your input was a child of the div that your core-a11y-keys was targeting, it would be doing what you instructed it to do anywhere in that div: intercepting keystrokes. In that case, you would need to handle the onKeyPress event in the input like <input on-keypress="{{handleInputKeyStrokes}}">:
handleInputKeyStrokes(Event e) {
// You'll need one or both of these; not sure which.
e.preventDefault();
e.stopPropagation();
}
I haven't tried this, and it may be that you need onKeyUp and onKeyDown instead, as in https://stackoverflow.com/a/13746419.

Autocomplete in JQUERY MOBILE text input

I searched a lot on net but couldnt find any solution. I am making a webapp in which I want 2 textbox to get data input from user. I want autocomplete feature in this textbox. The list of tags for autocomplete is available locally. I tried listview but what I want is that after user select some option from autocomplete hints, the textbox should have the selected value, and through some object, i should get the value of textbox to be used by javascript/php. This is a very basic thing, but I'm not able to do. Please help me out
I tried this jsfiddle.net/ULXbb/48/ . But the problem in this is that both listview gets same value after I select something in 1 listview.
In order not to add the same value to both search input, you need to target them using .closest(), .next(), .prev() and .find(). jQuery-Mobile, enhances list-view with data filter in a different way.
Demo
<form>
<input>
</form>
<ul data-role="listview">
<li>
<a>text</a>
</li>
</ul>
The form where the input is located, is on the same level of the ul. To target the input box, you need to use .prev('form').find('input'). Check the demo and the new code below.
$("input[data-type='search']").keyup(function () {
if ($(this).val() === '') {
$(this).closest('form').next("[data-role=listview]").children().addClass('ui-screen-hidden');
}
});
$('a.ui-input-clear').click(function () {
$(this).closest('input').val('');
$(this).closest('input').trigger('keyup');
});
$("li").click(function () {
var text = $(this).find('.ui-link-inherit').text();
$(this).closest('[data-role=listview]').prev('form').find('input').val(text);
$(this).closest('[data-role=listview]').children().addClass('ui-screen-hidden');
});
Thanks #user714852 I have extended your answer just add this line in the script tag:
$("#mylist li" ).addClass('ui-screen-hidden');
It will do wonders.
A working example: Listview Autocomplete - Enhanced

JQuery change event for select not working

I have tried all the options i found on the forum, but could not get it to work.
I even tried click event but that is also not getting called.
I wonder what is wrong with my code.
<script type="text/javascript">`enter code here`
$('#test.role.list').change(function(){
alert("here");
});
<div>
<form:select id="test.role.list" path="name">
<form:option value="none" label="none"/>
<form:option value="my none" label="none"/>
</form:select>
</div>
I have a list of permissions which are keys from the messageresource properties file, so the same thing i am using as id as well. The reason it is not working is correct as mentioned by the Lee. It considers the dots as different properties of the element with id "test" which is first string before dot and the next string as probably class. So i used the onchange event as
var Ele = document.geteElementByID("test.role.list");
ele.onchange = function(){
alert("here");
}
it is working fine now.
Yes the problesms are with dots in id.
You can use it as below,
$('#test\\.role\\.list').change(function(){
alert("here");
});

Form value second time not visible when displayed with jQuery ui dialog

I'm displaying some form data in a jquery dialog. Everything works fine when I do this the first time. I can see the "my value" string in the dialog. If I reopen the dialog again for the second time the form value is no longer visible. Check out this jsfiddle to try it out yourself. This is the code:
var dialog;
$("#b1").click(function(){
dialog = $("<div></div>").html("<p><input id='input1' type='text'></p>").dialog({
autoOpen:false,
});
$("#input1").val("my value");
dialog.dialog("open");
});
This bug only happens when I add the html tags dynamically. If I use a static html block everything works fine. Any idea what is wrong here? Thanks!
That is because you are not destroying the old
<input id='input1' type='text'>
so when you call
$("#input1").val("my value");
it sets the value of first
<input id='input1' type='text'>
it finds in the DOM.

Resources