0

UPDATE ON THIS: I can get to alert the checkboxes selected but I'm having a hard time to get working the binding of operations such as cut, paste, copy and rename

I have a div containing the menu items, in my case they are images for create, rename, cut, etc...rather than using the context-menu.

        <li id="Events" class="label">
                <a href='#Events'><span> Events </span></a>
        <div class="sub-menu"  style="height:auto; min-height:120px; background-color:#E5E5E5">
            <div class="menu" id="menu_a" style="position:relative; margin-left:56%">
            <img src="./js/jsTree/create.png" alt="" style="cursor:pointer" id="create" title="Create" >
            <img src="./js/jsTree/rename.png" alt="" style="cursor:pointer" id="rename" title="Rename" >
            <img src="./js/jsTree/remove.png" alt="" style="cursor:pointer"  id="remove"  title="Delete">
            <img src="./js/jsTree/cut.png" alt="" style="cursor:pointer" id="cut" title="Cut" >
            <img src="./js/jsTree/copy.png" alt="" style="cursor:pointer" id="copy" title="Copy">
            <img src="./js/jsTree/paste.png" alt=""  style="cursor:pointer" id="paste" title="Paste">
            </div>

                <div id="a" class="jstree_container"></div>
        </div>

        </li>

        <!-- JavaScript neccessary for the tree -->
        <script type="text/javascript" >
           jQuery.noConflict();

        jQuery(function ($) {

        $("#a").jstree({ 
                // List of active plugins
                "plugins" : [ 
                    "themes","json_data","ui","crrm","dnd","types","hotkeys","checkbox"
                ],

        /*"ui":{select_limit:-1},       */
        "checkbox": {
            override_ui:true,
                      real_checkboxes: true,
                      real_checkboxes_names: function (n) {
                         var nid = 1;
                         $(n).each(function (data) {
                            nid = $(this).attr("nodeid");
                         });
                         return (["check_" + nid, nid]);
                      },
                      two_state: true
                   },

              themes: {"theme": "classic"},
                // This example uses JSON as it is most common
                "json_data" : { 
                    // This tree is ajax enabled - as this is most common, and maybe a bit more complex
                    // All the options are almost the same as jQuery's AJAX (read the docs)
                    "ajax" : {
                        // the URL to fetch the data
                        "url" : "./js/jsTree/server.php?user_id=jeanpaul&cat=a",
                        // the `data` function is executed in the instance's scope
                        // the parameter is the node being loaded 
                        // (may be -1, 0, or undefined when loading the root nodes)
                        "data" : function (n) { 
                            // the result is fed to the AJAX request `data` option
                            return { 
                                "operation" : "get_children", 
                                "id" : n.attr ? n.attr("id").replace("node_","") : 1 
                            }; 
                        }
                    }
                },

            })


            .bind("create.jstree", function (e, data) {
                $.post(
                    "./js/jsTree/server.php?user_id=jeanpaul&cat=a", 
                    { 
                        "operation" : "create_node", 
                        "id" : data.rslt.parent.attr("id").replace("node_",""), 
                        "position" : data.rslt.position,
                        "label" : data.rslt.name,
                        "type" : data.rslt.obj.attr("rel")
                    }, 
                    function (r) {
                        if(r.status) {
                            $(data.rslt.obj).attr("id", "node_" + r.id);
                        }
                        else {
                            $.jstree.rollback(data.rlbk);
                        }
                    }
                );

            }
            )


            .bind("remove.jstree", function (e, data) {
                data.rslt.obj.each(function () {
                    $.ajax({
                        async : false,
                        type: 'POST',
                        url: "./js/jsTree/server.php?user_id=jeanpaul&cat=a",
                        data : { 
                            "operation" : "remove_node", 
                            "id" : this.id.replace("node_","")  
                        }, 
                        success : function (r) {
                            if(!r.status) {
                                data.inst.refresh();
                                $.jstree._reference($("#a")).refresh(-1);
                            }
                        }
                    });
                });
            })


            .bind("rename.jstree", function (e, data) {

                $.post(
                    "./js/jsTree/server.php?user_id=jeanpaul&cat=a", 
                    { 
                        "operation" : "rename_node", 
                        "id" : data.rslt.obj.attr("id").replace("node_",""),  /*data.rslt.obj.attr("id").replace("node_",""),*/
                        "label" : data.rslt.new_name
                    }, 
                    function (r) {
                        if(!r.status) {
                            $.jstree.rollback(data.rlbk);
                        }
                    }
                );

            })


            .bind("move_node.jstree", function (e, data) {
                data.rslt.o.each(function (i) {
                    $.ajax({
                        async : false,
                        type: 'POST',
                        url: "./js/jsTree/server.php?user_id=jeanpaul&cat=a",
                        data : { 
                            "operation" : "move_node", 
                            "id" : $(this).attr("id").replace("node_",""), 
                            "ref" : data.rslt.cr === -1 ? 1 : data.rslt.np.attr("id").replace("node_",""), 
                            "position" : data.rslt.cp + i,
                            "label" : data.rslt.name,
                            "copy" : data.rslt.cy ? 1 : 0
                        },
                        success : function (r) {
                            if(!r.status) {
                                $.jstree.rollback(data.rlbk);
                            }
                            else {
                                $(data.rslt.oc).attr("id", "node_" + r.id);
                                if(data.rslt.cy && $(data.rslt.oc).children("UL").length) {
                                    data.inst.refresh(data.inst._get_parent(data.rslt.oc));
                                }
                            }
                        }
                    });
                });
            });

        });
        </script>
        <script type="text/javascript" >
        // Code for the menu buttons of Events
           jQuery.noConflict();
            jQuery(function ($) { 

            $("#menu_a img").click(function () {

            //makes the images to behave as links, can't use a within the li because it thinks that is an accordion menu    



                switch(this.id) {



        case "create":
                    $.jstree._reference("#a").get_checked(-1, true).each(function(index,element){
                        $("#a").jstree("create",'#'+$(element).attr("id"),"last",null);     
                        });
        break;

        case "remove":
                    $.jstree._reference("#a").get_checked(-1, true).each(function(index,element){
                        $("#a").jstree("remove",'#'+$(element).attr("id"));     
                        });
        $.jstree._reference($("#a")).refresh(-1);               
        break;




        case "rename":

                    $.jstree._reference("#a").get_checked(-1, true).each(function(index,element){           


                    $("#a").jstree("rename", '#'+$(element).attr("id"), true  );


                        });

        break;



        case "cut":
                    $.jstree._reference("#a").get_checked(-1, true).each(function(index,element){

                            $("#a").jstree("cut", '#'+$(element).attr("id")); 
                        });
        break;



        case "copy":

                    $.jstree._reference("#a").get_checked(-1, true).each(function(index,element){

                            $("#a").jstree("copy", '#'+$(element).attr("id")); 
                        });
        break;



        case "paste":

                    $.jstree._reference("#a").get_checked(-1, true).each(function(index,element){

                            $("#a").jstree("paste", '#'+$(element).attr("id")); 
                        });
        break;



                    default:
        /*for tests only */
                    $.jstree._reference("#a").get_checked(-1, true).each(function(index,element){

                        //alert($(element).attr("id"));
                            $("#a").jstree(this.id, '#'+$(element).attr("id")); 
                        });

                        break;
                }
            });
        });

        </script>

What would I need to change in those functions for them to bind correctly to the checked elements to allow operations with the nodes?

Thank you in advance for any suggestions.

JP-

Jean G.T
  • 1
  • 10
  • 25

2 Answers2

2

Change

.bind("change_state create.jstree")

to just

.bind("create.jstree")
Hybride
  • 322
  • 4
  • 17
  • data.rslt.obj is undefined [Break On This Error] data.rslt.obj.each(function () { when tried to remove – Jean G.T Jun 04 '12 at 01:46
  • Hybride, your answer is partially correct... I mean it work only for removing many nodes with the checkbox plugin, but if I use rename with the content menu with a single selection, it does work, else if I select more than one I get only the last one for renaming.. however if I use the icon with the custom event I get: obj.children is not a function w1 = obj.children("ins").width(), on jquery.jstree.js (line 1267) .. I would like very much to accept your answer but don't want to confuse others though.. any ideas of how to bypass this problem? I want the context menu to work as the icons. – Jean G.T Jun 04 '12 at 15:06
  • Can you do me a favor and update your code with what you did since then? Just so when am reading, I don't try and fix something you already did. – Hybride Jun 04 '12 at 18:29
  • Hi Hybride, certainly! I actually just edited my post with the latest changes. Thank you in advance for any help that you can provide :) – Jean G.T Jun 04 '12 at 20:29
  • So the delete function IS working, but none of the others, correct? Why did you rename the functions to remove.jstree._reference(#f).get_selected() instead of just keeping them as bind("remove.jstree")? – Hybride Jun 04 '12 at 20:38
  • because I have more than one tree, the javascript is being echoed.. sorry I'm perhaps just doing it wrongly, but read on the documentation, maybe more correct would be to use get_checked? – Jean G.T Jun 07 '12 at 11:42
  • For multiple instances, have you tried using: var treeID = $(NODE).parents("div:first").attr("id"), // get the id of tree tree = jQuery.tree.reference($("#" + treeID)); // get the tree instance by id (Check out: [this link](http://code.google.com/p/jstree/issues/detail?id=436) ) – Hybride Jun 07 '12 at 15:41
0
<li id="Tasks" class="label">
    <a href='#Tasks'><span> Tasks </span></a>
    <div class="sub-menu" style="height:auto; min-height:120px; background-color:#E5E5E5">
        <div class="menu" id="menu_t" style="position:relative; margin-left:56%">
            <img src="./js/jsTree/create.png" alt="" id="create" title="Create">
            <img src="./js/jsTree/rename.png" alt="" id="rename" title="Rename">
            <img src="./js/jsTree/remove.png" alt="" id="remove" title="Delete">
            <img src="./js/jsTree/cut.png" alt="" id="cut" title="Cut">
            <img src="./js/jsTree/copy.png" alt="" id="copy" title="Copy">
            <img src="./js/jsTree/paste.png" alt="" id="paste" title="Paste">
        </div>
        <div id="t" class="jstree_container"></div>
    </div>
</li>

<!-- JavaScript neccessary for this tree : Tasks -->
<script type="text/javascript">
    jQuery.noConflict();

    jQuery(function ($) {

        $("#t").jstree({
            // List of active plugins used
            "plugins": ["themes", "json_data", "ui", "crrm", "dnd", "hotkeys", "checkbox", "contextmenu", "cookie"],

            "ui": {
                select_limit: -1,
                select_prev_on_delete: true,
            },
            "checkbox": {
                override_ui: true,
                real_checkboxes: true,
                real_checkboxes_names: function (n) {
                    var nid = 1;
                    $(n).each(function (data) {
                        nid = $(this).attr("nodeid");
                    });
                    return (["check_" + nid, nid]);
                },
                two_state: true
            },

            themes: {
                "theme": "classic"
            },
            // This example uses JSON as it is most common
            "json_data": {
                // This tree is ajax enabled - as this is most common, and maybe a bit more complex
                // All the options are almost the same as jQuery's AJAX (read the docs)
                "ajax": {
                    // the URL to fetch the data
                    "url": "./js/jsTree/server.php?user_id=jeanpaul&cat=t",
                    // the `data` function is executed in the instance's scope
                    // the parameter is the node being loaded 
                    // (may be -1, 0, or undefined when loading the root nodes)
                    "data": function (n) {
                        // the result is fed to the AJAX request `data` option
                        return {
                            "operation": "get_children",
                            "id": n.attr ? n.attr("id").replace("node_", "") : 1
                        };
                    }
                }
            }

        })


        .bind("create.jstree", function (e, data) {
            $.post(
                "./js/jsTree/server.php?user_id=jeanpaul&cat=t", {
                    "operation": "create_node",
                    "id": data.rslt.parent.attr("id").replace("node_", ""),
                    "position": data.rslt.position,
                    "label": data.rslt.name,
                    "type": data.rslt.obj.attr("rel")
                },
                function (r) {
                    if (r.status) {
                        $(data.rslt.obj).attr("id", "node_" + r.id);
                    } else {
                        $.jstree.rollback(data.rlbk);
                    }
                }
            );

        })


        .bind("remove.jstree", function (e, data) {
            data.rslt.obj.each(function () {
                $.ajax({
                    async: /*true*/ false,
                    type: 'POST',
                    url: "./js/jsTree/server.php?user_id=jeanpaul&cat=t",
                    data: {
                        "operation": "remove_node",
                        "id": this.id.replace("node_", "")
                    },
                    success: function (r) {
                        if (!r.status) {
                            data.inst.refresh();
                        }
                    }
                });
            });
        })


        .bind("rename.jstree", function (e, data) {

            data.rslt.obj.each(function () {
                $.ajax({
                    async: false,
                    type: 'POST',
                    url: "./js/jsTree/server.php?user_id=jeanpaul&cat=t",
                    data: {
                        "operation": "rename_node",
                        "id": this.id.replace("node_", ""),
                        "label": data.rslt.new_name
                    },
                    success: function (r) {
                        if (!r.status) {
                            data.inst.refresh();
                        }
                    }
                });
            });
        })


        .bind("move_node.jstree", function (e, data) {
            data.rslt.o.each(function (i) {
                $.ajax({
                    async: false,
                    type: 'POST',
                    url: "./js/jsTree/server.php?user_id=jeanpaul&cat=t",
                    data: {
                        "operation": "move_node",
                        "id": $(this).attr("id").replace("node_", ""),
                        "ref": data.rslt.cr === -1 ? 1 : data.rslt.np.attr("id").replace("node_", ""),
                        "position": data.rslt.cp + i,
                        "label": data.rslt.name,
                        "copy": data.rslt.cy ? 1 : 0
                    },
                    success: function (r) {
                        if (!r.status) {
                            $.jstree.rollback(data.rlbk);
                        } else {
                            $(data.rslt.oc).attr("id", "node_" + r.id);
                            if (data.rslt.cy && $(data.rslt.oc).children("UL").length) {
                                data.inst.refresh(data.inst._get_parent(data.rslt.oc));
                            }
                        }
                    }
                });
            });
        });

    });
</script>
<script type="text/javascript">
    // Code for the menu buttons of Tasks
    jQuery.noConflict();
    jQuery(function ($) {

        $("#menu_t img").css('cursor', 'pointer').click(function () {

            switch (this.id) {

            case "create":
                $.jstree._reference("#t").get_checked(-1, true).each(function (index, element) {
                    $("#t").jstree("create", '#' + $(element).attr("id"), "last", null, false);
                });
                break;

            case "remove":
                $.jstree._reference("#t").get_checked(-1, true).each(function (index, element) {
                    $("#t").jstree("remove", '#' + $(element).attr("id"));

                    // only refresh if we are taking the first node displayed... it's going to recreate it in the backend.
                    if ($(element).attr("id") == $("div.jstree > ul > li").first().attr("id")) {
                        $.jstree._reference($("#t")).refresh(-1);
                    }

                });
                break;

            case "rename":

                $.jstree._reference("#t").get_checked(-1, true).each(function (index, element) {
                    $("#t").jstree("rename", '#' + $(element).attr("id"), true);
                });

                break;

            case "cut":
                $.jstree._reference("#t").get_checked(-1, true).each(function (index, element) {

                    $("#t").jstree("cut", '#' + $(element).attr("id"));
                });
                break;

            case "copy":

                $.jstree._reference("#t").get_checked(-1, true).each(function (index, element) {

                    $("#t").jstree("copy", '#' + $(element).attr("id"));
                });
                break;

            case "paste":

                $.jstree._reference("#t").get_checked(-1, true).each(function (index, element) {

                    $("#t").jstree("paste", '#' + $(element).attr("id"));
                });
                break;

            }
        });
    });
</script>
Navnath Godse
  • 2,233
  • 2
  • 23
  • 32
Jean G.T
  • 1
  • 10
  • 25
  • obviously I am doing this for many trees, that's why i need the reference :) – Jean G.T Jun 07 '12 at 16:13
  • @Hybride: Updated the code above to reflect the latest changes – Jean G.T Jun 07 '12 at 16:19
  • In this case, you are trying to rename multiple nodes? (I apologize for asking so many questions, am trying to understand what you are trying to do, and I believe I confused myself between the two posts. :) – Hybride Jun 07 '12 at 17:49
  • well, I wish to do that if it's possible.. although I've explained it to the client and he said it's not so important :P – Jean G.T Jun 07 '12 at 21:37
  • Am not entirely sure that is possible without creating a PHP (or other language) form to process multiple renames at once (such as an update script) Is there any chance you can show us what the server.php form has? Am thinking the jsTree/jQuery is fine, but the actual form is not receiving the variables. – Hybride Jun 08 '12 at 11:11
  • In move_node and rename, try changing "label" to "title"; and also, "switch(this.id)" - are you actually passing in any variables for the id? – Hybride Jun 08 '12 at 15:24
  • this.id is the value of the id of the image that is triggering the event.. and label is just a convention because on our dB we have label and full label which is an encrypted value of label. This is probably not a requirement and is not implemented just yet. What I was thinking of is more of an Ajax jQuery function before that event that will stop it from happening just like an alert or a prompt would do with regular javascript. In the real life, is trying to rename the node but jumping til the last element.. and then stops to wait for the user to input the name to change it. Then you can see – Jean G.T Jun 08 '12 at 20:39
  • that it actually tries to query one last time the server and passing the values. Server.php only communicates with the db classes that come with the package. Probably some of the values passed that it will need later is cat = d for discussions, a for asks, etc.. it should then return only the matches with that db value and as well user id which I am passing, and it's in the output code as jeanpaul... that is my username and it should be simply $_SESSION[loginid] which is the value for the user logged in. So basically we are using one single table to host all the values for that data for many – Jean G.T Jun 08 '12 at 20:41
  • users and then it will return it over.. user_id =jeanpaul & cat=t and then on the server side will include the function to encode label to a full_label variable and insert it into the DB.. This is why it appears on the code now, but actually it will be a DB value just as 'label', 'parent_id', 'id', etc.. some of those values will autoincrement so the plugin will basically interact to pass those values to the DB for insertion, manipulation and retrieval from it. :) – Jean G.T Jun 08 '12 at 20:44
  • about the prompt or alert I think that there is a jquery based function that allows to do something before an AJAX function is completed.. so probably there is the key to make it stop and then allow the user input for the rename... (maybe your idea of a form it's not so bad idea) and then take from the queue the next rename. Maybe something like Facebox as I'm using it already for some forms with that project using the AJAX proprietary function of it to prompt for entering data as well in other sections.. For example: 'Create a new Contact'.. the trees are for labels, therefore the var name. – Jean G.T Jun 08 '12 at 20:47
  • 1
    Any chance you can stick this into jsFiddle so I can see a working copy of this? I apologize that I haven't been much help/going back and forth like this. Also, I would recommend a form for renaming multiple rows. You can pass in all the selected IDs, do a select query to grab the data from the db, and just update all at once. – Hybride Jun 09 '12 at 15:49
  • Hi Hybride, by the way in a meeting today apparently things have changed a bit of direction. I'm not sure how jsfiddle will help as this is using a DB. Long story short, apparently the end client wants it to be that only delete would require many checkboxes and the rest pretty much single operations :) Anyhow, I did some updates to this but since it's pretty much you and me only looking at this sort of issue, I would be honored to get in touch via chat or something just to exchange ideas.. don't know how to do that through here though.. anyways, thanks for everything.. hope to see u soon here. – Jean G.T Jun 13 '12 at 01:08
  • Feel free, contact is in the profile. Try this: [delete all nodes](http://stackoverflow.com/questions/6297852/delete-all-nodes-in-jstree) or this, [directly from the author](http://osdir.com/ml/jstree/2010-10/msg00483.html). – Hybride Jun 13 '12 at 14:04
  • Thanks for this, actually the issue is more with retrieving the correct data from the db now.. the delete works just fine :) – Jean G.T Jun 13 '12 at 18:49