select2: show placeholder when value is empty

This commit is contained in:
vitalets 2013-11-02 22:28:45 +04:00
parent ef3378d274
commit c77ebf6cc7
3 changed files with 72 additions and 46 deletions
CHANGELOG.txt
src/inputs/select2
test/unit

@ -4,6 +4,7 @@ X-editable changelog
Version 1.5.1 wip
----------------------------
[bug] select2: placeholder not shown if value initially empty (vitalets)
[enh #400] allow `validate` to change submitted value, also fix #354 (vitalets)
[enh #396] bs3 popover: placement `auto` (vitalets)
[bug #357] select2: tags mode with space separator (vitalets)

@ -38,7 +38,11 @@ $(function(){
});
//remote source (simple)
$('#country').editable({
source: '/getCountries'
source: '/getCountries',
select2: {
placeholder: 'Select Country',
minimumInputLength: 1
}
});
//remote source (advanced)
$('#country').editable({
@ -219,18 +223,22 @@ $(function(){
// if defined remote source AND no multiple mode AND no user's initSelection provided -->
// we should somehow get text for provided id.
// The solution is to use element's text as text for that id
// The solution is to use element's text as text for that id (exclude empty)
if(this.isRemote && !this.isMultiple && !this.options.select2.initSelection) {
// customId and customText are methods to extract `id` and `text` from data object
// we can use this workaround only if user did not define these methods
// otherwise we cant construct data object
var customId = this.options.select2.id,
customText = this.options.select2.formatSelection;
if(!customId && !customText) {
var data = {id: value, text: $(this.options.scope).text()};
var $el = $(this.options.scope);
if (!$el.data('editable').isEmpty) {
var data = {id: value, text: $el.text()};
this.$input.select2('data', data);
}
}
}
},
input2value: function() {

@ -319,6 +319,23 @@ $(function () {
}, timeout);
});
test("remote: initially empty", function () {
var s = 2, text = groups[s],
newVal = 0, newText = groups[newVal],
e = $('<a href="#" data-type="select2" data-name="select2"></a>').appendTo(sfx).editable({
source: 'groupsArr2',
select2: {
placeholder: 'placeholder'
}
});
e.click();
var p = tip(e);
ok(p.is(':visible'), 'popover visible');
equal(p.find('.select2-choice span').text(), 'placeholder', 'placeholder shown in select2');
});
asyncTest("remote: custom id, custom text, init selection (not multiple)", function () {
var s = 2,
data = [