【发布时间】:2023-03-27 22:28:01
【问题描述】:
我是第一次使用 jstree。我正在尝试使用 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;
}
}
【问题讨论】:
-
请用 jstree 和数据显示一个完全渲染的页面(可能使用 jsfiddle.net ),否则几乎无法想象您的视图有什么问题。
-
嗨瓦维洛夫。感谢你的回复。我成功地做到了。现在创建、重命名和删除功能正常工作。但是它创建的节点具有 jx_y 格式的 id。而且我无法抓住这个名字。有什么方法可以捕获新创建节点的正确 id 和名称。或重命名节点。
-
抱歉,在最新评论中没有理解您的问题。正如我所说,请展示使用 PHP 生成的 HTML 页面以及 JSON 数据示例,我或许可以帮助您。
-
[{"id":"Pay","parent":"#","text":"Pay","type":"account"},{"id":"1 ","parent":"Pay","text":"Salaries","type":"subaccount"},{"id":"2","parent":"Pay","text":"奖金","type":"subaccount"},{"id":"3","parent":"Pay","text":"Commissions","type":"subaccount"}]
-
这是一棵树的数据。以 json 格式。我正在从数据库中获取它。
标签: php jquery codeigniter jstree