Select list option with dynamically added data
I am trying to added data dynamically in "UL LI" structure using ajax, when trying to select the dynamically added content "li" is not getting selected and display in textbox. Thanks in Advance..
http://www.marcorpsa.com/ee/t1161.html Maybe help you
Facing same issue, when using with Angular ng-options. The names are not displayed.
I have found that the exact syntax at http://www.marcorpsa.com/ee/t1161.html did not work for me. I have adjusted the javascript slightly to look like this:
$(function() {
$('.selectpicker.additems')
.append(
$('<option/>')
.addClass('divider')
.data('divider', true)
)
.append(
$('<option/>')
.addClass('additem')
.data('content',
"<input type=text onKeyDown='event.stopPropagation();' onKeyPress='addSelectInpKeyPress(this,event)' onClick='event.stopPropagation()' placeholder='Add item'> <span class='glyphicon glyphicon-plus addnewicon' onClick='addSelectItem(this,event,1);'></span>"
)
)
.selectpicker('refresh');
});
function addSelectItem(t, ev) {
ev.stopPropagation();
var txt = $(t).closest('.bootstrap-select').find('input').val().replace(/[|]/g, '');
if ($.trim(txt) == '') return;
var p = $(t).closest('.bootstrap-select').find('.selectpicker');
var o = $('option', p).eq(-2);
o.before(
$("<option>", {
"selected": true,
"text": txt
})
);
p.selectpicker('refresh');
}
function addSelectInpKeyPress(t, ev) {
ev.stopPropagation();
// do not allow pipe character
if (ev.which == 124) ev.preventDefault();
// enter character adds the option
if (ev.which == 13) {
ev.preventDefault();
addSelectItem($(t).next(), ev);
}
}
It would be great if this kind of functionality were able to be included by default in bootstrap-select
I ended up rewriting the above to merge with live search instead. The idea is if you can't find what you're looking for you can add it.
$(document).on('DOMNodeInserted', function(e) {
if ($(e.target).is('.dropdown-menu .no-results') && $(e.target).closest('.bootstrap-select').find('.selectpicker').data('live-search-add') == true) {
$(e.target).closest('.bootstrap-select').find('.bs-searchbox').addClass('input-group')
.append(!$(e.target).closest('.bootstrap-select').find('.bs-searchbox').find('.glyphicon-plus').length ?
'<span class="input-group-btn"><button type="button" class="btn btn-default" onClick="addSelectItemFromSearch(this,event,1);"><span class="glyphicon glyphicon-plus"></span></button></span>' :
''
);
}
})
.on('DOMNodeRemoved', function(e) {
if ($(e.target).is('.dropdown-menu .no-results')) {
$(e.target).closest('.bootstrap-select').find('.bs-searchbox').removeClass('input-group').find('.input-group-btn').remove();
}
});
function addSelectItemFromSearch(t, ev) {
ev.stopPropagation();
var txt = $(t).closest('.bootstrap-select').find('input').val().replace(/[|]/g, '');
if ($.trim(txt) == '') return;
var p = $(t).closest('.bootstrap-select').find('.selectpicker');
var o = $('option', p).eq(1);
o.before(
$('<option>', {
'selected': true,
'text': txt
})
);
p.selectpicker('refresh');
}
(updated the above to be more consistent with the use of quotes vs apostrophes and to use find so the behavior is more consistent across multiple versions of the various libraries)
The select for this would need to look like:
<select class="form-control selectpicker" data-live-search="true" data-live-search-add="true">
<option>1</option>
<option>2</option>
</select>
Again I think this would be great functionality to be included by default in bootstrap-select (hopefully a bit less hack-ish than how I'm doing it)!
The code from http://www.marcorpsa.com/ee/t1161.html is very picky about which versions of jQuery and bootstrap-select you use. It won't work with jQuery 3, but I got it working with jQuery 2.2.4. It also doesn't work with bootstrap-select versions greater than 1.7.7 - the "Add item" option shows, but the item will not actually get added.
I too would like to see this feature natively in bootstrap-select. This is a huge workaround for what seems like such a simple thing.
The example page http://www.marcorpsa.com/ee/t1161.html uses jQuery v1.11.1, Bootstrap v3.2.0, and Bootstrap-select v1.7.2.
I tested using jQuery v1.11.1, Bootstrap v3.3.7, and Bootstrap-select v1.12.2, and the original JavaScript did not work but my modification did work. I also tested jQuery v3.1.1, Bootstrap v3.3.7, and Bootstrap-select v1.12.2 and the original code does not work with this combo either, but my modification does.
Also just tested jQuery v3.2.1 with the same results as above.
I've read that DOMNodeInserted is deprecated to I've done something else now:
var observer = new MutationObserver(function(mutations) {
for (var i = 0; i < mutations.length; i++) {
var mutation = mutations[i];
for (var j = 0; j < mutation.addedNodes.length; j++) {
var addedNode = mutation.addedNodes[j];
if (addedNode.className == "no-results" && $(addedNode).closest('.bootstrap-select').find('.selectpicker').data('live-search-add') == true) {
$(addedNode).closest('.bootstrap-select').find('.bs-searchbox').addClass('input-group')
.append(!$(addedNode).closest('.bootstrap-select').find('.bs-searchbox').find('.glyphicon-plus').length ?
'<span class="input-group-btn"><button type="button" class="btn btn-default" onClick="addSelectItemFromSearch(this,event,1);"><span class="glyphicon glyphicon-plus"></span></button></span>' :
''
);
}
}
for (var k = 0; k < mutation.removedNodes.length; k++) {
var removedNode = mutation.removedNodes[k];
if (removedNode.className == "no-results") {
$('.bootstrap-select').find('.bs-searchbox').removeClass('input-group').find('.input-group-btn').remove();
}
}
}
});
observer.observe(document.body, { attributes: false, childList: true, characterData: true, subtree: true });
function addSelectItemFromSearch(t, ev) {
ev.stopPropagation();
var txt = $(t).closest('.bootstrap-select').find('input').val().replace(/[|]/g, '');
if ($.trim(txt) == '') return;
var p = $(t).closest('.bootstrap-select').find('.selectpicker');
var o = $('option', p).eq(1);
o.before(
$('<option>', {
'selected': true,
'text': txt
})
);
p.selectpicker('refresh');
}
@aliasav If using Angular, check out https://github.com/caseyjhol/ng-bootstrap-select.
I have updated the sample at http://www.marcorpsa.com/ee/t1161.html The reason for it not working was that the older bootstrap-select created the control with the original select located outside the .bootstrap-select container whereas the newer versions put the select inside this container.
This required a minor change to the selector in the addSelectItem() function to target the select control relative to the .bootstrap-select class instead of using .prev() as per the previous example.
I am working on the jQuery 3.x issue as well.
Thanks to Eric Nemchick for alerting me to the issue and suggesting some of the code changes.
UPDATE Tested with jQuery 3.0 and it appears to be working.
Why not use this: https://github.com/truckingsim/Ajax-Bootstrap-Select ?
Please 👍 and follow #899.
Eric Nemchik, thank you for your contribution commented on 13 Jul 2017. I am new to programming and i want to use the code to insert data in my mysql database. So while adding data through a form into a 'many to many' mysql table so you add the data if its not already in the table.
Can you help?
Eric Nemchik, thank you for your contribution commented on 13 Jul 2017. I am new to programming and i want to use the code to insert data in my mysql database. So while adding data through a form into a 'many to many' mysql table so you add the data if its not already in the table.
Can you help?
I don't really think so. I've moved on to using other libraries like select2