user2379092
user2379092

Reputation: 31

JsTree contextmenu error

A javascript error indicating that this.rename(obj) is not defined when selecting to rename a node.

JavaScript runtime error: Object doesn't support property or method 'rename'

$(document).ready(function () {
        $('#marketing-category-tree').jstree({
            themes: {
                theme: "default",
                dots: true,
                icons: true
            },
            contextmenu: {
                items: {
                    "rename" : {
                        "label": "Rename",
                        "action": function (obj) { this.rename(obj); }
                    }
                }
            },
            plugins: ["themes", "html_data", "ui", "crrm", "contextmenu"]
        })
        .bind("rename.jstree", function (e, data) {
            debugger;
            alert("RENAMING!!!");
        });
});

I have also tried the following code and am able to select and do a rename but cannot capture the change event.

$('#marketing-category-tree').jstree({
    themes: {
        theme: "default",
        dots: true,
        icons: true
    },
    plugins: ["themes", "html_data", "ui", "crrm", "contextmenu"]
})
.bind("rename.jstree", function (e, data) {
    alert("RENAMING!!!");
});

Upvotes: 3

Views: 1352

Answers (3)

sep7696
sep7696

Reputation: 575

you should get node of the tree with var tree = $("#marketing-category-tree").jstree(true);then operate on nodes.

u can use this example goodluck :)

Upvotes: 0

Nikita
Nikita

Reputation: 4686

Your first code example ain't gonna work because

"action": function (obj) { this.rename(obj); }

in this case "this" is a point to Window object the next things is that documentation http://www.jstree.com/api/ doesn't have mentions of method rename and only rename_node

Here is the working example (right click at any node and then click on rename)

http://jsfiddle.net/w9snc6z1/4/

Pay attention that rename_node also not working but according to documentation

set_text: set the text value of a node. Used internally, please use rename_node(obj, val).

it's not recommended to use set_text instead of rename_node.

Upvotes: 0

oerl
oerl

Reputation: 1224

I think the method you are looking for is edit. But first you have to get the node of the tree. Try to use this code below:

...
"contextmenu" : {
  "items" : renameItem : { // The "rename" menu item
              label : "Rename",
              action : function (obj) {
              n = $('#marketing-category-tree').jstree(true).get_node(obj.reference); //get node
              $('#marketing-category-tree').jstree(true).edit(n); //puts the node into edit mode
              }
            }
  }
...

HTH

Upvotes: 4

Related Questions