I'm currently using the most up-to-date version of Select2. The select2 box is used to display a list of skills. I am trying to set the default values (for when it's opened) to contain the current skills. I have the information in proper format, I just cannot understand how to set it. The closest I've gotten is using:
//b is array containing list of user selected options
//#e1 is select2 id
for($i=0;$i<b.length;$i++) $('#e1').val(b[$i]).trigger("change");
However, this only displays the LAST option. Which I think is because I'm not allowed to set it in a loop. However I'm unsure how else to do it??
Any current solutions are invalid as of Select v2.4.0 as initSelection and .select2("val", data etc.) was removed.
Any help is appreciated.
My solution was actually very simple and avoided for loops all together.
$('#e1').select2({
//info
}).val(b).trigger("change");
val() in jQuery also accepts arrays.
Related
Magento's admin Product Grid has <select> filters for searching products.
Some of those are notoriously unsorted (ie. attribute_sets).
Because I keep discovering this unsorted behaviour in too many locations to fix on the php side, I'd like to use a js approach to fix it.
I got the sorting part finished, that part is done.
But after .append()ing the resulting set back the pull down menu loses the previously selected option. (ie. the filter that was used to load the grid)
I'm missing something basic, but I don't know what.
Here's my code in jsfiddle
*Cleaner code w/o my debug msgs jsfiddle
like I said, it was something basic... I don't know why I could not figure it out yesterday, but anywho:
before sorting:
var whichSelectedVal = $(elid).val(); // stores the current option
after sorting:
$(elid).val(whichSelectedVal); // re-selects the stored option
This works even for the blank filter option, yay for me, no extra work needed to account for that ;)
With a standard select2 dropdown box, populated with a list of names from a database call, is there a way to search on hidden items within the search area?
Example:
Select2 box shows to end user "Charlie Watts" but actually the options value holds "Charlie Watts (22)". I want the use to be able to search for 22, but not show it by default to the end user.
TIA
Yep, you can achieve that using the formatResult and/or formatSelection methods. There's a great example of using them in the Select2 Docs: Templating.
In your format function, filter out the " (22)" part of your value and return everything before it.
On a UX note, it could be strange to see matches appear that don't give any indication as to why they match. If that doesn't matter for your use-case, carry on.
(note: edited since I've just realized the question are somehow correlated, at least in my mind!)
I want to create a multi-filter page in which the result will be animated...
I'm trying with 2 different plugin (quicksand and Isotope) and with both solution I'm having problems...
---ISOTOPE--- (original)
With Isotope I need to filter data based on active class, or based on IDs of filters, which I've already stored in JS, does anyone know how can I do that?
I set up a page with 2 different filter like 'color' (red, blue, orange...) and 'type' (square, round...)
I already have a Javascript that assign class active to the 2 filtering lu based on selection, if all color are selected shift the 'active' class to 'all', and more than one sub-filter can be activated. And this also save the list of the id of the active li items in a string for color filter and another string for shape filter
I also already set up the page like the combination filter Isotope demo at this link: http://isotope.metafizzy.co/demos/combination-filters.html and it is working fine but doesn't allow to select more than one sub-filter at the same time.
I saw the demo at this link http://fiddle.jshell.net/desandro/JB45z/ with filtering combination, but it is based on radio button that I'd like to avoid.
I'm not sure if what I'm trying to do is easy or not... is like, how to tell to Isotope to filter based on the sub-filter that have active class or based on the sum of the li with the ID saved in my two string?
Thanks for any help, as you can easily understand I'm not skilled in js at all and english is not my first language!
--- QUICKSAND --- (edited)
I've just realized that I didn't explain why I stored the IDs of the selected items in the js string. And this is also about the different js question.
I was trying to set up the same system with Quicksand instead of Isotope.
But since quicksand need to have a starting li and a destination li to display the animation I set up the js to pass an array to a different temporary php page that use the array to display a destination li.
All until here is working fine but I'm not able to get back the li data in the original page to let Quicksand perform the animation. The last part of my js appear to have problems that I'm not able to fix (too many days trying with no success), the last part of the js is:
$.post( 'destination_li_filtered.php', {
colorString,
shapeString,
$('#ids').attr('val')
},
function(data) { // should contains the resulting data from the request (?)
$('.list').quicksand( $(data).find('li'),
{ adjustHeight: 'auto' },
function() {
callbackCode();
}
);
e.preventDefault();
});
the external page destination-li-filtered is displaying the results but the original page is not able to take back the data...
Obviously I need to set op the page with isotope or quicksand, not both.
but I'm also wondering witch is the best plugin to display 100's of results with about 20 filters (without considering the combinations). And of course, which is the easiest to use!
You should see the radio button and the change in view as separate things.
It's a common misconception that you should store all your state in the DOM (ie. which checkbox is checked). The DOM is a view, you don't keep state in a view.
You should have a state that lives in your JavaScript.
Like:
var state = "all_selected"; // green, red, blue
Then, when you check the radio button, it will set the appropriate state and update the list (show/hide elements based on state).
This allows you to make any control you want, be it a radio button, a checkbox or something completely custom.
I am trying to update the selected value in one of the select boxes using knockout. But I am not able to do so. Here is the jsfiddle link - http://jsfiddle.net/5MauG/1/
When I click on the click me span, I expect that the selected value in the select box should change.
You're problem is that trying to set the selected option to a new object won't work. Even with the same values, the new object is not the same as the old object. You can see that in this slightly modified fiddle; it works when actually choosing from the objects in the options array.
With an array of values, like strings or ints, you can select by value. With an array of objects, you need to select with the actual object. This will be easier if your view is using the bindings everywhere, because the bindings will represent the actual objects.
I'm writing my first jquery UI widget: a date range control composed of two text boxes (start and end date) both of which will be using the jQuery UI DatePicker.
All the widget code examples I've seen online have you applying the widget behavior to a generic selector. In my case, I need exactly two elements in my selector (the two aforementioned text boxes) and will be applying different logic to each. For example, the start date text box will have different onblur events/behavior than the end date text box.
Is there a way to create a widget without a selector and instead have two named constructor arguments, one for start date and one for end date? If not, is there a better way I should be approaching this widget? Perhaps I should create a POJSO (plain old JS object) instead?
what's it matter what the selector is? widgets use selectors because most of the time it applies to the selected elements. but it's up to the widget to use the selected elements. technically, it could just ignore them and continue processing whatever. so why don't you just select the document (or like the parent container of the textboxes), and pass in an object as the parameter to your widget with values for "textbox1" and "textbox2" and then get those values on initialization. does that make sense?
for example:
$("#parent_container").yourWidget({
txtbox1: $("#txt_id1"),
txtbox2: $("#txt_id2"),
otherParam: true
});
and in your widget code, just ignore selected elements. process everything based on the parameter's values.