Comments (8)
Options shouldn't be overwritten unless one coming in has an identical value as an existing option (i.e. defined by the valueField
option). It's important to overwrite to prevent duplicates and erratic behavior.
from selectize.js.
Mine are - I have a select
with some default options within the HTML. I then override load
and my options have now gone missing. The value field (which should be the value
attribute from the option
) is definitely different to others.
I'll write up a quick demo of it tomorrow.
from selectize.js.
@brianreavis Sorry it's taken a few days, it was my birthday the weekend just gone, so I've been a little busy. Here we go.
My select
element is setup statically to contain a couple of default options:
select(type='text', placeholder='Enter a hotel name', tabindex=9, name='hotel')
option(data-value="")
option(data-value="00") 01
option(data-value="01") 03
option(data-value="08") 05
option(data-value="02") 07
option(data-value="03") 09
option(data-value="04") 11
option(data-value="05") 13
option(data-value="06") 15
option(data-value="07") 17
Then I'm using Selectize to load in more options afterwards:
$quoteForm.find('select[name=hotel]').selectize({
diacritics: false,
create: true,
hideSelected: true,
labelField: 'name',
valueField: 'name',
searchField: ['name', 'id'],
preload: true,
maxItems: 1,
render: {
item: function(data) {
return "<div data-value='"+data.value+"' data-hotelid='"+data.id+"' class='item'>"+data.name+"</div>";
},
option: function(data) {
return '<div data-value="'+data.name+'">' +
'<img src="http://2cw.co.uk/img/'+data.country+'/'+data.id+'" alt="" class="thumb">' +
'<span class="hotel_name">' +
'<span class="name">' + data.name + '</span>' +
'</span>' +
'</div>';
}
},
load: function(query, callback) {
if(!query.length) return callback();
var destinationISO = $('input[name=iso]').val();
if(query == "SELECTIZE") query = ""; // Mini fix
$.ajax({
async: false,
cache: false,
url: '/api/hotels/' + destinationISO || "",
dataType: 'json',
data: {
q: query
},
success: function(data) {
callback(data);
}
});
},
// Set the hidden ISO input
onItemAdd: function(value, $item) {
var itemData = $item.data();
$('input[name=hotel_id]').val(itemData.hotelid);
}
});
Nothing out of the ordinary, and yet my default options are removed.
from selectize.js.
When reading <option>
elements from the HTML, it reads the value from the value
attribute, not data-value
.
option(value="")
option(value="00") 01
option(value="01") 03
option(value="08") 05
option(value="02") 07
option(value="03") 09
option(value="04") 11
option(value="05") 13
option(value="06") 15
option(value="07") 17
Mind giving that a shot?
from selectize.js.
Okay, now it's coming through as undefined
, but that'll be down to me using different field names in the initialization of Selectize. This is similar to the problem I face in #27 I think?
from selectize.js.
A few things:
- If you're using rich data, you need to make sure to bootstrap all options with their data. You should choose one of the following:
- Declare the options in JS in the selectize settings instead of HTML.
- Attach JSON data as an attribute on each
<option>
. By default, the library will try to read the "data-data" attribute. If you'd rather use another, specify the attribute name with thedataAttr
setting.
option(value="..." data-data="{...}") label
- When rendering options and items, there's no need to manually add the "data-value" attribute. This will be added by the library. Providing your own could lead to erratic behavior. Furthermore, in your sample
data.name
is used in one place, anddata.value
is used in another.
from selectize.js.
Brilliant! I shall give these a go - thanks Brian!
from selectize.js.
Worked a treat! Thanks Brian!
from selectize.js.
Related Issues (20)
- data-* attribute regression/bug since 0.15 to 0.15.2 HOT 4
- Searching with underscore doesn't work correctly HOT 1
- Incorrect role is being assigned to combo box; it is impacting accessibility of the control. HOT 2
- remove_button uses a hardcoded 2px border radius HOT 1
- Can't add items to a (multi) select after updating inputs (in order) HOT 1
- Couldn't find the way to use a custom plugin without adding it to the Selectize fork HOT 1
- remove_button plugin bug: append expects html_container to be a string HOT 1
- Documentation for `selectOnTab`'s default is out of date HOT 2
- Unable to load options when triggering load for the second time HOT 1
- Quotation marks in selected option value result in error when selecting another value HOT 2
- Cursor disappears after selecting option in select multiple when using CDN provided version HOT 6
- jQuery deprecation warning: .trim() HOT 1
- Wrong autocompleate type
- Add link to Selectize options list in API documentation of Selectize()
- selectize.destroy and 'blur' event on other selectized objectects
- Selectize with Laravel full text search not working HOT 1
- Hovered option is automatically selected on blur HOT 3
- Cannot be able to retrieve data attribute when selectize is rendering
- converting from boostrap-select: encountering several issues...
- Incorrect role is being assigned to combo box; it is impacting accessibility of the control. HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from selectize.js.