KendoUI renaming Tree Node and Right Clicking on tree nodes

Currently, KendoUI does not support renaming tree nodes, and since I coded up a solution with jeditable, I may as well share it. - Miguel Castillo

by manchagnu

HTML

<script src="http://cdn.kendostatic.com/2011.3.1129/js/kendo.all.min.js"></script>
<link rel="stylesheet" href="http://cdn.kendostatic.com/2011.3.1129/styles/kendo.common.min.css">
<link rel="stylesheet" href="http://cdn.kendostatic.com/2011.3.1129/styles/kendo.default.min.css">
<script src="http://www.appelsiini.net/download/jquery.jeditable.js"></script>
<ul id='tree'>
    <li>Item1
        <ul>
            <li>Item1.1</li>
            <li>Item1.2</li>
            <li>Item1.3</li>
        </ul>
    </li>
    <li>Item2</li>
    <li>Item3</li>
</ul>

CSS

.treeInlineEdit > input
{
    font-size: 1.5em;
    min-width: 10em;
    min-height: 2em;
    border-radius: 5px 5px 5px 5px;
    -moz-border-radius: 5px 5px 5px 5px;
    border: 0px solid #ffffff;
}

JavaScript

$(document).ready(function()
{
    // If you want to disable showing the context menu when right clicking
    // on the document, the code below would do the trick.
    $(document).bind("contextmenu",function(e)
    {
        return false;
    }); 
    
    $("#tree").kendoTreeView(
    {
        select: function (event)
        {
            var $item = $(event.node);
            console.log( $item );
        }
    })
    .on('mousedown', '.k-in', function(event)
    {
        // Handle right click events...
        if (event.which === 3)
        {
            var $item = $(event.target);
            console.log( $item );
        }
    })
    .on('dblclick', '.k-in', function(event)
    {
        //
        // NOTE: The editable control will take over the dblclick event
        //   attached to this tree control and it will prevent bubbling of the
        //   doubleclick events...  This means that we don't need to worry about
        //   this item getting the editable plugin invoked on it twice.
        //   It also means that if we need to handle a double click, at
        //   this point we will need to add the logic in the function call
        //   passed into the editable plugin.
        //
        //   Also, replace .k-in with whatever selector you need to filter
        //   on.  For example, all my tree nodes in production applications
        //   are anchor, so my filter selector is 'a' rather than '.k-in'.
        //
        $target = $(event.target);

        $target.editable(function (value, settings)
        {
            // if you were using something like knockout for your databinding,
            // this is how you could update your model :)
            //var model = ko.dataFor(this);
            //model.name(value);

            return value;
        },
        {
            event: 'dblclick',
            cssclass: 'treeInlineEdit'
        });

        $target.trigger('dblclick', [event]);

    });
});