Skip to content

Instantly share code, notes, and snippets.

@potar
Last active August 29, 2015 14:00
Show Gist options
  • Save potar/11320213 to your computer and use it in GitHub Desktop.
Save potar/11320213 to your computer and use it in GitHub Desktop.
Modified 'radiolist'. Requires x-editable. Adds 'radiolist' option to x-editable where you can use radio inputs.
/**
* The idea was taken here https://github.com/vitalets/x-editable/issues/153
*
* We need to remove this code in the future because this functionality ('radiolist')
* will be in x-editable by default.
*
* You can use only json as a `source`:
* $('.editable-earn-method').editable({
* name: 'earn_method',
* source: {'1': 'Yes', '0': 'No'},
* });
*
*/
(function($) {
var Radiolist = function (options) {
this.init('radiolist', options, Radiolist.defaults);
};
$.fn.editableutils.inherit(Radiolist, $.fn.editabletypes.list);
$.extend(Radiolist.prototype, {
renderList: function() {
var $label;
this.$tpl.empty();
if(!$.isArray(this.sourceData)) {
return;
}
for(var i=0; i<this.sourceData.length; i++) {
var name = this.options.name || 'default_name';
$label = $('<label class="radio-inline">')
.append($('<input>', {
type: 'radio',
name: name,
value: this.sourceData[i].value
}));
$label.append($('<span>').text(this.sourceData[i].text));
// Add radio buttons to template
this.$tpl.append($label);
}
this.$input = this.$tpl.find('input[type="radio"]');
this.setClass();
},
value2str: function(value) {
return value;
},
//parse separated string
str2value: function(str) {
return str;
},
//set checked on a required radio button
value2input: function(value) {
this.$input.each(function(i, el) {
var val = $(el).val()
if (val == value) {
$(el).prop('checked', true);
}
});
},
input2value: function() {
return this.$input.filter(':checked').val();
},
//collect text of checked boxes
value2htmlFinal: function(value, element) {
var checked = $.fn.editableutils.itemsByValue(value, this.sourceData);
if(checked.length) {
var textual_value = this.sourceData.filter(
function(x) {
if (x.value == value) return x.text
}
)[0].text;
$(element).html($.fn.editableutils.escape(textual_value));
} else {
$(element).empty();
}
},
value2submit: function(value) {
return value;
},
activate: function() {
// Don't set focus because it looks useless for radio-buttons
}
});
Radiolist.defaults = $.extend({}, $.fn.editabletypes.list.defaults, {
/**
@property tpl
@default <div></div>
**/
tpl:'<label class="editable-radiolist input-sm"></label>',
/**
@property inputclass
@type string
@default null
**/
inputclass: '',
/**
Separator of values when reading from `data-value` attribute
@property separator
@type string
@default ','
**/
separator: ',',
name: 'defaultname'
});
$.fn.editabletypes.radiolist = Radiolist;
}(window.jQuery));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment