SelectBoxIt & ReactJS support needed - javascript

SelectBoxIt allows you to replace ugly and hard-to-use HTML select boxes with gorgeous and feature rich drop downs.
I implemented the SelectBoxIt and the <select> inside my render(return()) method didn't recognize it.
I searched the web and found almost only one source on Github that I didn't understand its recommendation, you can help me on this point.
In brief, I concluded that SelectBoxIt naturally doesn't support ReactJS.
Hence we need a solution or trick for that.
Below is sample code from their website and used it completely in my project:
<!doctype html>
<html>
<head>
<meta charset=utf-8>
<title>SelectBoxIt demo</title>
<link rel="stylesheet" href="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.2.2/css/bootstrap-combined.min.css" />
<link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.9.2/themes/base/jquery-ui.css" />
<link type="text/css" rel="stylesheet" href="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.css" />
<link rel="stylesheet" href="http://gregfranko.com/jquery.selectBoxIt.js/css/jquery.selectBoxIt.css" />
</head>
<body>
<select name="test">
<option value="SelectBoxIt is:">SelectBoxIt is:</option>
<option value="a jQuery Plugin">a jQuery Plugin</option>
<option value="a Select Box Replacement">a Select Box Replacement</option>
<option value="a Stateful UI Widget">a Stateful UI Widget</option>
</select>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.9.2/jquery-ui.min.js"></script>
<script src="http://gregfranko.com/jquery.selectBoxIt.js/js/jquery.selectBoxIt.min.js"></script>
<script>
$(function() {
var selectBox = $("select").selectBoxIt();
});
</script>
</body>
</html>
The following select was not affected:
<select
id="sel1"
// ref={s => { this.selectBox = s; }}
onChange={ event => this.onSelectChange(event.target.value) }
>
{options}
</select>
The <select name="test"> has worked when put it inside render(return()) but the <select id="sel1" > inside render(return()) has not been changed to be a SelectBoxIt shape. Note that I commented the ref={s => { this.selectBox = s; }} just to prevent the error to display.

Since this is a jquery plugin, you need to make sure to have it installed:
npm install -S jquery
Then you need to import this in your script :
import * as $ from 'jquery';
Now in your react component, apply the jquery initialization in componentDidMount. The only catch is that you need to use ref to find the node.
render() {
return <select ref={s => { this.selectBox = s; }} />
}
componentDidMount() {
$(this.selectBox).selectBoxIt();
}
Some reference here.

I found that reactJS didn't "catch" onChange of the select that is replaced with SelectBoxIt but it "catch" onClick. So for one project that use SelectBoxIt + ReactJS I've made onClick trigger from jQuery when select value is changed combined with react onChange event.
In script tag /index.html/:
$("select").on('change',function(){
$("select").trigger('click');
});
In reactJS /SelectBox.js/:
onClickHandled that check if value is changed.
Example here: https://github.com/gshishkov/selectBoxIt-reactjs
I know it is not very elegant solution, but it was fastest and working one.

Related

Changing style and position of created multi-select drop-down checkbox

I am trying to create the front end for an web application. I already have the backend developed in python. I am new to this territory of web design/development
I have some html and javascript code which uses jQuery multiselect.css and jQuery multiselect.js to create a dropdown multiselect checklist. However I am confused with how i can change the style or position of the dropdown, as I am already using jQuery multiselect.css
I added the css and the js under the head tag
<link href="jquery.multiselect.css" rel="stylesheet">
<script src="jQuery.js"></script>
<script src="jQuery-MultiSelect-master/jquery.multiselect.js"></script>
And then I am creating the dropdown list in html and using jQuery to initialize it
<select id='testSelect1' multiple>
<option value='1'>Item 1</option>
<option value='2'>Item 2</option>
<option value='3'>Item 3</option>
<option value='4'>Item 4</option>
<option value='5'>Item 5</option>
</select>
<script src="./test.js"></script>
<script>
$('#testSelect1').multiselect({
columns: 4,
placeholder: 'Select Countries',
selectAll: true
});
</script>
This puts the multiselect on the left and makes the dropdown width as wide as the page width and height longer then needed. I am trying to modify it so that it would appear in the middle and the size of the dropdown isn't as bit and colour of the dropdown array is different
When you are adding styles and scripts to project it should look like this
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title></title>
<!-- style goes here -->
</head>
<body>
<!-- scripts go here -->
</body>
</html>
Style always go on top of document, between <title></title> tags. It is recommended to put scripts just before <body> tag closes. Here you can read why
There is that..
If you want to style that dropdown firstly we need to see how does it look like right because we can't help you blindly. But you could do this
select {
<!-- style for select goes here -->
}
select option {
<!-- style for option goes here -->
if you want your select to be 300px wide you can add this
select {
width: 300px;
}
Again. Post some screenshots and update the question with it if this didn't help

Select2 tags limit input characters in IE browser

I try to use the newest select2 v4.0.3 library in a web page. I used the given example on this page for tagging and tokenization.
I tested it in different browsers. It works fine, but in Internet Explorer v.11 it behaves strangely:
I tried to add a new element which was not among the options.
After typing a few characters, the cursor was taken away and I was not able to finish the word I was typing in. When I clicked into the select box to gain back the cursor, the half-entered word disappeared. So it seems to be impossible to type in more then 3-4 characters. I experienced the same on select2.github.io/examples page when opening it in IE.
<html>
<head>
<link rel="stylesheet" href="css/select2.css" type="text/css" />
<script src="js/jquery-2.1.0.js" type="text/javascript"></script>
<script src="js/select2.full.js" type="text/javascript"></script>
</head>
<body>
<script type="text/javascript">
$(document).ready(function() {
$(".js-example-tokenizer").select2({
tags: true,
tokenSeparators: [',', ' ']
});
});
</script>
<select class="js-example-tokenizer" multiple="multiple" style="width: 600px;">
<option value="AL">Alabama</option>
<option value="WY">Wyoming</option>
</select>
</body>
</html>
Without using the tagging, select2 works fine in IE as well. But I would like to use a multi-select, tagged control where the user can enter free text as selected option also.
Is there a workaround for select2 to use it with tagging and tokenization in IE as well?
Fiddle example to use on IE11 for testing.
The issue on GitHub related to the problem.
These two classes : select2-container--focus select2-container--open are on the input's container respectively when the input element has the focus and when the options dropdown menu is open.
When we begin to type, the opening of the dropdown menu make the input element lost the focus (blur). Select2 needs to re-focus on the element. In fact, in the Select2 source code, we can see that the update method finish by re-focus on the element : this.$search.focus() (check here).
This code works fine in most browsers, but not in IE v11 because IE doesn't arrive to re-focus on the input with this Select2 source code. The class select2-container--focus disappears and the focus stay on the dropdown menu, which creates the bug.
In more recent version of Select2, it looks like they tried a fix : check here.
Sadly, after test it, it is still not working. The only solution that i see here is to override the Select2 source code until a fix is released. Otherwise, you need to use a version where the bug is not present (e.g. 4.0.2 as mentionned in comments).
Based on GitHub issues (in particular this one : GitHub issue), a solution could be :
JS Fiddle
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.6-rc.0/css/select2.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.6-rc.0/js/select2.min.js"></script>
<script type="text/javascript">
// Override update method
$.fn.select2.amd.require._defined['select2/selection/search'].prototype.update = function(a, b) {
var c = this.$search[0] == document.activeElement;
this.$search.attr("placeholder", "");
a.call(this, b);
this.$selection.find(".select2-selection__rendered").append(this.$searchContainer);
this.resizeSearch();
if (c) {
var self = this;
window.setTimeout(function() {
self.$search.focus();
}, 0);
}
};
$(document).ready(function() {
$(".js-example-tokenizer").select2({
tags: true,
tokenSeparators: [',', ' ']
});
});
</script>
<select class="js-example-tokenizer" multiple="multiple" style="width: 600px;">
<option value="AL">Alabama</option>
<option value="WY">Wyoming</option>
</select>

Initialize Javascript Select2 controller using HTML5 data-* attributes

I'm trying to manage select2 library in order to augment the capabilities of natives select html controls.
According to the official documentation (very compressed) it says that for using select2 you only need to download they CSS and JavaScript files and that is (and also, implicitly assumed, that you need jQuery).
So, there is a way of initializing select using HTML5 data attributes, but I am not getting it; I'm trying to follow their example. Perhaps I am missing some important note here.
Here is the HTML I am trying to do
<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-3.1.0.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/css/select2.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/js/select2.min.js"></script>
</head>
<body>
<h1>Hello Select2</h1>
<div>
<select multiple id="a_select" data-tags="true" data-placeholder="Select an option" data-allow-clear="true" style="width:100%">
<option value="AL">Alabama</option>
<option value="WY">Wyoming</option>
</select>
</div>
</body>
</html>
Also a link to plunker, to have a live example ready to go. https://plnkr.co/edit/Xk101K3sowAYQwbRCFez?p=preview
PS: I can achieve the desired result thought JavaScript, but is quite annoying to don't know what is not working when i just followed the example on the official site.
You're not initializing Select2 anywhere... data-* attributes let you override default configuration, but you still need to kick off the plugin:
<script>$('select').select2();</script>
https://plnkr.co/edit/RPObSPVl8jlZgtedsK38?p=preview

Why are bootstrap multiselect options disabled by default?

I am new to bootstrap. I do not understand why these simple multiselect options are not clickable.
EDIT
The first multiselect on the test-site below is the one in question. I have tested using Chrome and Firefox.
EDIT-2
I am also using angularjs and think there may be a conflict somewhere. I am in the process of testing this hypothesis out.
HTML
<head>
<link rel="stylesheet" href="css/bootstrap-multiselect.css" type="text/css"/>
<script src="js/bootstrap-multiselect.js"></script>
</head>
<label for="work-type" class="fixed50">Type:</label>
<select id="work-type" name="work-type[]" multiple >
<option value="fiction">Fiction</option>
<option value="non-fiction">Non-Fiction</option>
</select>
The only javascript referense:
JS
$("#work-type").multiselect();
Here is a link to my test-site.
You also need to introduce a js file and a css file.
See details:
http://davidstutz.github.io/bootstrap-multiselect/

jQuery Chosen plugin - activate (i.e. focus) not working?

I'm instantiating the Chosen plugin within a Bootstrap Modal. I'm calling the Chosen plugin after I've launched the modal.
All works as expected apart from being able to auto focus the element to allow typing without first clicking into the element. Strangely I'm getting a blue border around the element which would indicate it has focus but no keypress actually registers until I click into the object.
I only need this to work for Chrome, I'm currently running version 39.0.2171.95.
I'm following instructions from the docs:
http://harvesthq.github.io/chosen/options.html#triggerable-events
Here's the code:
var $modal = $('#' + modalId); // This element contains relevant modal markup along with <select> element
$modal.modal();
var $mySelect = $('#mySelectElement'); // id of the <select> element
$mySelect.chosen();
$mySelect.trigger('chosen:activate'); // This doesn't work...
Have also tried the following but to no avail:
$mySelect.trigger('chosen:open'); // This doesn't work...
Any pointers appreciated.
Cheers,
Lee
I found an answer, or a workaround at least.
When stepping through the code I noticed that the trigger('chosen:activate') method is called before the $modal.modal() has completed i.e. the modal has not yet displayed.
I simply added an asynchronous delay before calling chosen:activate which appears to fix it and the Chosen element can be typed into immediately:
// Enable chosen plugin...
var $mySelect= $('#mySelect');
$mySelect.chosen();
var enableChosen = setTimeout(function() {
$mySelect.trigger('chosen:activate');
}, 250);
Anything lower than 250ms and it stops working again. I guess it's something to do with having to wait for the modal fade animation to complete.
** UPDATE **
Alternative and better method, hook into the shown.bs.modal callback for the Bootstrap modal, like so:
$modal.on('shown.bs.modal', function(e) {
// Enable chosen plugin...
var $mySelect = $('#mySelect');
$mySelect.chosen();
$mySelect.trigger('chosen:activate');
});
Your code must work. Here is a simple example base on official documentation. I don't know if your are doing something more in your code that is doing your code don't work.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Chosen: A jQuery Plugin by Harvest to Tame Unwieldy Select Boxes</title>
<link rel="stylesheet" href="docsupport/style.css">
<link rel="stylesheet" href="docsupport/prism.css">
<link rel="stylesheet" href="chosen.css">
<style type="text/css" media="all">
/* fix rtl for demo */
.chosen-rtl .chosen-drop { left: -9000px; }
</style>
</head>
<body>
<form>
<div id="container">
<div id="content">
<header>
<h1>Chosen <small>(<span id="latest-version">v1.3.0</span>)</small></h1>
</header>
<em>Into This</em>
<select data-placeholder="Choose a Country..." class="chosen-select" style="width:350px;" tabindex="2">
<option value=""></option>
<option value="United States">United States</option>
<option value="United Kingdom">United Kingdom</option>
<option value="Afghanistan">Afghanistan</option>
<option value="Aland Islands">Aland Islands</option>
<option value="Albania">Albania</option>
<option value="Algeria">Algeria</option>
</select>
</div>
</div>
</div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js" type="text/javascript"></script>
<script src="chosen.jquery.js" type="text/javascript"></script>
<script type="text/javascript">
var $mySelect = $('.chosen-select');
$mySelect.chosen();
$mySelect.trigger('chosen:open');
</script>
</body>
</html>

Categories