我想将我在jsTree上所做的更改保存回数据库。我尝试了不同的方法,但无法开始工作。我在codeigniter工作。如果可以,请你帮助我。谢谢。 这是我的代码, view.php:
<button id="target" type="submit">Click here</button>
<script type="text/javascript">
$(document).ready(function () {
$('#tree-container').on('changed.jstree', function (e, data) {
var i, j, r = [];
// var state = false;
for (i = 0, j = data.selected.length; i < j; i++) {
r.push(data.instance.get_node(data.selected[i]).id);
}
$('#txttuser').val(r.join(','));
}).jstree({
'plugins': ["checkbox", "dnd", "search", "contextmenu"],
'core': {
"check_callback" : true,
"multiple": true,
'data': {
"url": "getChildren",
"dataType": "json"
}
},
'checkbox': {
three_state: false,
cascade: 'up'
},
'plugins': ["dnd","contextmenu","search","wholerow"],
}
);
});
$( "#target" ).click(function() {
var v =$('#tree-container').jstree(true).get_json();
var mytext = JSON.stringify(v);
alert(mytext); });
</script>
<div class="row">
<div class="container">
<input type="hidden" name="node" id="node" value="" />
<div class="form-group"> <div id="tree-container"></div>
</div>
</div>
</div>
Controller.php这样:
function getChildren() {
$this->load->model('Category_model');
$result = $this->Category_model->tree_all();
$itemsByReference = array();
// Build array of item references:
foreach($result as $key => &$item) {
$itemsByReference[$item['category_id']] = &$item;
// Children array:
$itemsByReference[$item['category_id']]['children'] = array();
// Empty data class (so that json_encode adds "data: {}" )
$itemsByReference[$item['category_id']]['data'] = new StdClass();
}
// Set items as children of the relevant parent item.
foreach($result as $key => &$item)
if($item['parent_id'] && isset($itemsByReference[$item['parent_id']]))
$itemsByReference [$item['parent_id']]['children'][] = &$item;
// Remove items that were added to parents elsewhere:
foreach($result as $key => &$item) {
if($item['parent_id'] && isset($itemsByReference[$item['parent_id']]))
unset($result[$key]);
}
foreach ($result as $row) {
$data[] = $row;
}
// Encode:
echo json_encode($data);
}
model.php:
function tree_all() {
$result = $this->db->query("SELECT category_id, category,category as text, parent_id FROM category ")->result_array();
foreach ($result as $row) {
$data[] = $row;
}
return $data;
}
Pleeaaase heeelp meee !!!我试图解决这个问题好几天。
答案 0 :(得分:1)
要使用JSTree管理添加,编辑和更新,首先要下载jstree.js
并检查以下行:
"create" : {
这是在您单击create时执行的代码块,将您的创建代码逻辑放在此处,并使用ajax调用add / update
数据库。
以同样的方式管理edit
,delete
。
注意: edit
,delete
就在create
之后
Ajax语法:
$.ajax({
url : '<?php echo base_url().'/controllerfunction' ?>',
method : 'post',
data: {
key : value
},
success: function(response) // response from controller function
{
// your code here
}
});