Kendo UI Checkbox
A custom Kendo UI checkbox widget
by Josh Eastburn
HTML
<link rel="stylesheet" href="http://cdn.kendostatic.com/2014.1.318/styles/kendo.common.min.css">
<link rel="stylesheet" href="http://cdn.kendostatic.com/2014.1.318/styles/kendo.default.min.css">
<script src="http://cdn.kendostatic.com/2014.1.318/js/kendo.all.min.js"></script>
<link rel="stylesheet" href="http://cdn.kendostatic.com/2014.1.318/styles/kendo.rtl.min.css">
<link rel="stylesheet" href="http://cdn.kendostatic.com/2014.1.318/styles/kendo.dataviz.min.css">
<link rel="stylesheet" href="http://cdn.kendostatic.com/2014.1.318/styles/kendo.dataviz.default.min.css">
<div>The source SVG images:
<br/>
<svg xmlns="http://www.w3.org/2000/svg" xmlns:svg="http://www.w3.org/2000/svg" width="24" height="48">
<rect fill="#ffffff" x="0" y="0" width="24" height="48" id="svg_1" stroke="#f4f4f4" stroke-width="0" />
<rect id="svg_4" height="23" width="23" y="0.5" x="0.5" fill-opacity="0" stroke="#cdcdcd" fill="#000000" />
<line id="svg_3" y2="5.05" x2="19" y1="19.05" x1="5" fill-opacity="0" stroke-linecap="null" stroke-linejoin="null" stroke-dasharray="null" stroke-width="4" fill="none" stroke="#00007f" />
<rect height="23" width="23" y="24.5" x="0.5" fill-opacity="0" stroke="#cdcdcd" fill="#000000" id="svg_5" />
<line y2="5.05" x2="19" y1="19.05" x1="5" fill-opacity="0" stroke-linecap="null" stroke-linejoin="null" stroke-dasharray="null" stroke-width="4" fill="none" stroke="#00007f" id="svg_6" transform="rotate(90 12 12.05)" />
</svg>
<svg width="24" height="48" xmlns="http://www.w3.org/2000/svg" xmlns:svg="http://www.w3.org/2000/svg">
<rect fill="#e5e5e5" x="0" y="0" width="24" height="48" id="svg_1" stroke="#d8d8d8" stroke-width="0" />
<rect id="svg_4" height="23" width="23" y="0.5" x="0.5" fill-opacity="0" stroke="#cdcdcd" fill="#000000" />
<line id="svg_3" y2="5.05" x2="19" y1="19.05" x1="5" fill-opacity="0" stroke-linecap="null" stroke-linejoin="null" stroke-dasharray="null"...
CSS
/* Custom Checkbox and Radio */
.custom-k-checkbox > input {
position: absolute;
overflow: hidden;
clip: rect(0 0 0 0);
height: 1px;
width: 1px;
margin: -1px;
padding: 0;
border: 0;
}
.custom-k-checkbox > label {
padding-left: 24px;
height: 24px;
display: inline-block;
background-repeat: no-repeat;
background-position: 0 -24px;
vertical-align: middle;
cursor: pointer;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
margin: 4px 5px 4px 4px;
font-family: Arial;
font-weight: bold;
line-height: 2em;
color: #999;
font-size: 10pt;
text-transform: uppercase;
text-indent: 6px;
}
.custom-k-checkbox > input[type=checkbox]:checked + label {
background-position: 0 0;
color: #666;
}
/* Using Base64 Encoding for SVG images for JSFiddle only.
Save the svg content as an .svg file */
.custom-k-checkbox > label {
background-image:...
JavaScript
(function () {
var CHANGE = 'change';
kendo.ui.plugin(kendo.ui.Widget.extend({
init: function (element, options) {
kendo.ui.Widget.fn.init.call(this, element, options);
this._create();
},
options: {
name: 'CustomCheckbox'
},
events: [
CHANGE],
enable: function (val) {
if (val === false) {
$(this.element).attr('disabled', 'disabled');
$(this.wrapper).addClass('k-state-disabled');
} else {
$(this.element).removeAttr('disabled');
$(this.wrapper).removeClass('k-state-disabled');
}
},
value: function (checked) {
var $element = $(this.element),
currentValue = $element.prop('checked');
if (typeof checked === 'boolean') {
if (checked !== currentValue) {
$element.prop('checked', checked);
this.trigger(CHANGE, {
field: 'value'
});
}
} else {
return $element.prop('checked');
}
},
_create: function () {
var me = this,
$element = $(me.element),
text = $element.data('label') || me.options.label || '';
me.element.prop('type','checkbox');
me.wrapper = $element.wrap('<span class="custom-k-checkbox"></span>')
.parent();
$element.after('<label>' + text + '</label>');
$element.siblings('label').click(function (e) {
$element.click();
});
$element.bind(CHANGE, function (e) {
//PROPAGATE EVENT TO WIDGET
me.trigger(CHANGE, {
field: 'value'
});
});
if (me.options.enabled !== undefined) {
...