Grid with color picker

by OnaBai

HTML

<link rel="stylesheet" href="http://cdn.kendostatic.com/2013.2.716/styles/kendo.common.min.css">
<link rel="stylesheet" href="http://cdn.kendostatic.com/2013.2.716/styles/kendo.default.min.css">
<script src="http://cdn.kendostatic.com/2013.2.716/js/jquery.min.js"></script>
<script src="http://cdn.kendostatic.com/2013.2.716/js/kendo.all.min.js"></script>
<div id="grid"></div>

CSS

#grid {
    width : 450px;
}

JavaScript

function createRandomData(n) {
    var data = [];
    for (var i = 0; i < n; i++) {
        var r = Math.floor(Math.random() * 255);
        var g = Math.floor(Math.random() * 255);
        var b = Math.floor(Math.random() * 255);
        var hex = (r * 65536 + g * 256 + b);
        hex = "#" + ("000000" + hex.toString(16)).substr(-6);
        var elem = {Id: i + 1, Color : hex};
        data.push (elem)
    }
    return data;
}

var ds = new kendo.data.DataSource({
    data    : createRandomData(20),
    pageSize: 10,
    schema  : {
        model: {
            id : "Id",
            fields: {
                Id       : { type: 'number' },
                Color    : { type: 'string' }
            }
        }
    }
});

var grid = $("#grid").kendoGrid({
    dataSource: ds,
    editable  : "popup",
    pageable  : true,
    columns   : [
        { command : [ "edit" ] },
        { field: "Id", width: 100, title: "Id" },
        {
            field: "Color", 
            width: 150,
            title: "Color", 
            template:  function(dataItem) {
                return "<div style='background-color: " + dataItem.Color + ";'>&nbsp;</div>";
            },
            editor : function (container, options) {
                // create an input element
                var input = $("<input/>");
                // set its name to the field to which the column is bound ('name' in this case)
                input.attr("name", options.field);
                // append it to the container
                input.appendTo(container);
                // initialize a Kendo UI AutoComplete
                input.kendoColorPicker({
                    value: options.model.Color,
                    buttons: false
                });
            }
        }
    ]
}).data("kendoGrid");