list input + checklist alpha
This commit is contained in:
150
src/inputs/checklist.js
Normal file
150
src/inputs/checklist.js
Normal file
@@ -0,0 +1,150 @@
|
||||
/**
|
||||
Checklist input. Internally value stored as array.
|
||||
|
||||
@class checklist
|
||||
@extends list
|
||||
@final
|
||||
@example
|
||||
<a href="#" id="status" data-type="checklist" data-pk="1" data-url="/post" data-original-title="Select options"></a>
|
||||
<script>
|
||||
$(function(){
|
||||
$('#options').editable({
|
||||
value: [2, 3],
|
||||
source: [
|
||||
{value: 1, text: 'Active'},
|
||||
{value: 2, text: 'Blocked'},
|
||||
{value: 3, text: 'Deleted'}
|
||||
]
|
||||
}
|
||||
});
|
||||
});
|
||||
</script>
|
||||
**/
|
||||
(function ($) {
|
||||
|
||||
var Checklist = function (options) {
|
||||
this.init('checklist', options, Checklist.defaults);
|
||||
};
|
||||
|
||||
$.fn.editableform.utils.inherit(Checklist, $.fn.editableform.types.list);
|
||||
|
||||
$.extend(Checklist.prototype, {
|
||||
renderList: function() {
|
||||
var $label, $div;
|
||||
if(!$.isArray(this.sourceData)) {
|
||||
return;
|
||||
}
|
||||
|
||||
for(var i=0; i<this.sourceData.length; i++) {
|
||||
$label = $('<label>').text(' '+this.sourceData[i].text)
|
||||
.prepend($('<input>', {
|
||||
type: 'checkbox',
|
||||
value: this.sourceData[i].value,
|
||||
name: this.options.name
|
||||
}));
|
||||
|
||||
$('<div>').append($label).appendTo(this.$input);
|
||||
}
|
||||
},
|
||||
|
||||
value2str: function(value) {
|
||||
return $.isArray(value) ? value.join($.trim(this.options.separator)) : '';
|
||||
},
|
||||
|
||||
//parse separated string
|
||||
str2value: function(str) {
|
||||
var reg, value = null;
|
||||
if(typeof str === 'string' && str.length) {
|
||||
reg = new RegExp('\s*'+$.trim(this.options.separator)+'\s*');
|
||||
value = str.split(reg);
|
||||
} else if($.isArray(str)) {
|
||||
value = str;
|
||||
}
|
||||
return value;
|
||||
},
|
||||
|
||||
//set checked on required checkboxes
|
||||
value2input: function(value) {
|
||||
var $checks = this.$input.find('input[type="checkbox"]');
|
||||
$checks.removeAttr('checked');
|
||||
if($.isArray(value) && value.length) {
|
||||
$checks.each(function(i, el) {
|
||||
if($.inArray($(el).val(), value) !== -1) {
|
||||
$(el).attr('checked', 'checked');
|
||||
}
|
||||
});
|
||||
}
|
||||
},
|
||||
|
||||
input2value: function() {
|
||||
var checked = [];
|
||||
this.$input.find('input:checked').each(function(i, el) {
|
||||
checked.push($(el).val());
|
||||
});
|
||||
return checked;
|
||||
},
|
||||
|
||||
//collect text of checked boxes
|
||||
value2htmlFinal: function(value, element) {
|
||||
var selected = [], html = '';
|
||||
if($.isArray(value) && value.length <= this.options.limit) {
|
||||
for(var i=0; i<value.length; i++){
|
||||
item = this.itemByVal(value[i]);
|
||||
if(item) {
|
||||
selected.push($('<div>').text(item.text).html());
|
||||
}
|
||||
}
|
||||
html = selected.join(this.options.viewseparator);
|
||||
} else {
|
||||
html = this.options.limitText.replace('{checked}', $.isArray(value) ? value.length : 0).replace('{count}', this.sourceData.length);
|
||||
}
|
||||
$(element).html(html);
|
||||
}
|
||||
});
|
||||
|
||||
Checklist.defaults = $.extend({}, $.fn.editableform.types.list.defaults, {
|
||||
/**
|
||||
@property tpl
|
||||
@default <div></div>
|
||||
**/
|
||||
tpl:'<div></div>',
|
||||
|
||||
/**
|
||||
Separator of values in string when sending to server
|
||||
|
||||
@property separator
|
||||
@type string
|
||||
@default ', '
|
||||
**/
|
||||
separator: ',',
|
||||
/**
|
||||
Separator of text when display as element content.
|
||||
|
||||
@property viewseparator
|
||||
@type string
|
||||
@default ', '
|
||||
**/
|
||||
viewseparator: '<br>',
|
||||
/**
|
||||
Maximum number of items shown as element content.
|
||||
If checked more items - <code>limitText</code> will be shown.
|
||||
|
||||
@property limit
|
||||
@type integer
|
||||
@default 4
|
||||
**/
|
||||
limit: 4,
|
||||
/**
|
||||
Text shown when count of checked items is greater than <code>limit</code> parameter.
|
||||
You can use <code>{checked}</code> and <code>count</code> placeholders.
|
||||
|
||||
@property limitText
|
||||
@type string
|
||||
@default 'Checked {checked} options of {count}'
|
||||
**/
|
||||
limitText: 'Checked {checked} options of {count}'
|
||||
});
|
||||
|
||||
$.fn.editableform.types.checklist = Checklist;
|
||||
|
||||
}(window.jQuery));
|
Reference in New Issue
Block a user