在 jstree 中创建删除和重命名不起作用

Create Delete and Rename in jstree not working

我第一次使用 jstree time.I 我正在尝试使用 jstree 进行创建、重命名和删除操作。我正在通过从 codeigniter 中的数据库中获取数据来创建具有 json 数据的树。这里我在控制器中给出了一个函数。创建重命名和删除按钮在视图页面中。功能在控制器中。此代码动态形成三棵树。但这些按钮仅适用于最后一棵树。对于这些操作,其他两棵树的 ID 为空。我无法知道错误在哪里。

谁能帮我解决这个问题。如果我的问题不清楚,请问我。

这是代码

public function accounts()
{
    $this->load->view('header');
    $accountids = $this->Accounts_model->getaccounts();
    $i=1;
    $function='';
    foreach($accountids as $accountid){
        $htmll='';
        $html='';
        $json='[';
        $jstree='';
        $jstree.='$("#'.preg_replace('/\s+/', '', $accountid['accname']).'")';
        $function.='function demo_create() {
                    var ref = $("#'.preg_replace('/\s+/', '', $accountid['accname']).'").jstree(true),
                        sel = ref.get_selected();
                        console.log(sel);
                        alert(sel);
                    if(!sel.length) { return false; }
                    sel = sel[0];
                    sel = ref.create_node(sel, {"type":"file"});
                    if(sel) {
                        ref.edit(sel);
                    }
                };
                function demo_rename() {
                    var ref = $("#'.preg_replace('/\s+/', '', $accountid['accname']).'").jstree(true),
                        sel = ref.get_selected();
                        console.log(sel);
                        alert(sel);
                    if(!sel.length) { return false; }
                    sel = sel[0];
                    ref.edit(sel);
                };
                function demo_delete() {
                    var ref = $("#'.preg_replace('/\s+/', '', $accountid['accname']).'").jstree(true),
                        sel = ref.get_selected();
                        console.log(sel);
                        alert(sel);
                    if(!sel.length) { return false; }
                    ref.delete_node(sel);
                };';
                $html.='<tr><td>'.$i.'</td><td><div id="'.preg_replace('/\s+/', '', $accountid['accname']).'">"'.$accountid['accname'].'"';
                $json.='{"id":"'.$accountid['accname'].'","parent":"#","text":"'.$accountid['accname'].'","type":"account"},';

                $sub_accountids= $this->Accounts_model->get_subaccounts($accountid['id']);

                foreach($sub_accountids as $sub_accid){
                    $json.='{"id":"'.$sub_accid['subAccname'].'","parent":"'.$accountid['accname'].'","text":"'.$sub_accid['subAccname'].'","type":"tranche"},';
                }       
                $htmll.='</div></td><td><div class="account_ox" id="'.$accountid['id'].'"><a href="#"><img src="'.JS_FOLDER_PATH.'/image/add.png" data-toggle="modal" width="27px" height="27px" class="cat" data-target="#myModal2" onclick="testleena('.$regionid['id'].');"/></a><input type="hidden" id="hiding" value="'.$accountid['id'].'"/></div></td></tr>';
                $i++;
                $json=rtrim($json,",");
                $json.=']';     

                echo $html.'<script>'.$function.'$(document).ready(function() {'.$jstree.'.jstree({  
                    "core" : {
                    "data" : 
                        '.$json.',
                        "check_callback":true
                    }
                });
                $(".account_ox").click(function (){
                        $("#opex_hiddenid").val(this.id);
                });
                });</script>'.$htmll;   
        }
    }

使用方法 get_node 根据您在 sel 变量中的新节点 ID 获取新节点的名称。 像这样:$("#"+treeId).jstree().get_node(sel).text

查看演示 - select 要添加子节点的节点,然后单击按钮创建节点:JS Fiddle