asp.Net
发布时间:2022-04-07 发布网站:大佬教程 code.js-code.com
大佬教程收集整理的这篇文章主要介绍了无限分级和tree结构数据增删改【提供Demo下载】,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
无限分级
很多时候我们不确定等级关系的层级,这个时候就需要用到无限分级了。
说到无限分级,又要扯到递归调用了。(),在此我们需要先设计好表机构,用来存储无限分级的数据。
说来其实也简单,就是一个ID和父ID的关系。
title="无限分级和tree结构数据增删改【提供Demo下载】" alt="无限分级和tree结构数据增删改【提供Demo下载】" src="https://cn.js-code.com/res/2019/02-07/23/a3e2fe7060014f7c1bb431135e150db4.png" >
以此类推,Id需要是唯一的,ParenId需要是Id列里面存在即可。这样我们就实现无限分级了,如果再加一列Sort排序就更完美了。
jstree插件
官方地址:
为什么要用这个插件?因为有方便的api给我们做数据绑定,且支持节点拖动来实现增删改,个人觉得这个功能挺强大的。
Demo
下面我们来基于jstree插件来实现无限分级数据操作。以区域数据操作为例,用Code First的方式来编写demo代码。
创建Region实体
为了配合插件自动生成的节点id,我们这里使用的Node和ParentNode来存储上下级关系()。
Id { ;
Name { ;
Node { ;
ParentNode { ; }
满足jstree插件的数据对象Dto
为了适应jstree插件的数据要求,我们需要把上面的数据转换成树状的数据对象。
RegionsId { ;
text { ;
id { ;
List children { ;
数据转换
GetRegionTree 初始化数据获取 的辅助方法
RegionsTreeOutput LoadRegions( id,List List</span><Region> regions = inRegions.Where(t => t.ParentNode ==<span style="color: #000000;"> id).ToList();
</span><span style="color: #0000ff;">if</span> (outRegions == <span style="color: #0000ff;">null</span>)<span style="color: #008000;">//</span><span style="color: #008000;">加载父节点</span>
<span style="color: #000000;"> {
outRegions
= ToTreeData(regions[<span style="color: #800080;">0<span style="color: #000000;">]);
LoadRegions(outRegions.id,inRegions,outRegions);
}
<span style="color: #0000ff;">else<span style="color: #008000;">//<span style="color: #008000;">加载子节点
<span style="color: #000000;"> {
outRegions.children =<span style="color: #000000;"> ToTreesData(regions);
<span style="color: #0000ff;">if (regions.Count > <span style="color: #800080;">0<span style="color: #000000;">)
{
<span style="color: #0000ff;">for (<span style="color: #0000ff;">int i = <span style="color: #800080;">0; i < regions.Count; i++<span style="color: #000000;">)
{
LoadRegions(regions[i].Node,outRegions.children[i]);<span style="color: #008000;">//<span style="color: #008000;">递归调用
<span style="color: #000000;"> }
}
}
<span style="color: #0000ff;">return<span style="color: #000000;"> outRegions;
}
</span><span style="color: #0000ff;">public</span><span style="color: #000000;"> RegionsTreeOutput ToTreeData(Region region)
{
</span><span style="color: #0000ff;">var</span> treeData = <span style="color: #0000ff;">new</span><span style="color: #000000;"> RegionsTreeOutput();
treeData.id </span>=<span style="color: #000000;"> region.Node;
treeData.text </span>=<span style="color: #000000;"> region.Name;
treeData.RegionsId </span>=<span style="color: #000000;"> region.Id;
</span><span style="color: #0000ff;">return</span><span style="color: #000000;"> treeData;
}
</span><span style="color: #0000ff;">public</span> List<RegionsTreeOutput> ToTreesData(List<Region><span style="color: #000000;"> listRegion)
{
</span><span style="color: #0000ff;">var</span> regions = <span style="color: #0000ff;">new</span> List<RegionsTreeOutput><span style="color: #000000;">();
</span><span style="color: #0000ff;">for</span> (<span style="color: #0000ff;">int</span> i = <span style="color: #800080;">0</span>; i < listRegion.Count; i++<span style="color: #000000;">)
{
regions.Add(ToTreeData(listRegion[i]));
}
</span><span style="color: #0000ff;">return</span><span style="color: #000000;"> regions;
}
</span><span style="color: #0000ff;">#endregion</span></pre>
初始化获取转换后的数据
= regions = db.Regions.Where(t => regionObj = LoadRegions(,regions,
以上后台的数据差不多就完成了。
前台数据加载
$("/Home/GetResultData",,= $('#jstree_demo'
'plugins': ["contextmenu","dnd","search","state","types","wholerow"'core'"animation": 0"check_callback": 'force_text': "themes": { "stripes": 'data'"types""default""icon": "fa fa-folder icon-state-warning icon-lg""file""icon": "fa fa-file icon-state-warning icon-lg""contextmenu"
actions =
actions.create = {
"separator_before": ,
"separator_after": ,
"_disabled": ,
"label": "新增",
"action": (data) {
inst == inst.get_node(data.reference);
inst.create_node(obj,{},"last", () { inst.edit(new_node); },0);
(o.id != "0001")
actions.rename ="separator_before": "separator_after": "_disabled": ,
"label": "重命名""action": inst ==
actions. ="separator_before": "icon":
"label": "删除"= actions;
$('#jstree_demo').on('delete_node.jstree', id ="get""/Home/DeleteRegion?id=" + }
});
});
<span style="color: #008000;">//<span style="color: #008000;">移动节点
$('#jstree_demo').on('move_node.jstree',data) {
saveRegions(data);
});
<span style="color: #008000;">//<span style="color: #008000;">修改名
$('#jstree_demo').on('rename_node.jstree',data) {
saveRegions(data);
});
<span style="color: #008000;">//<span style="color: #008000;">保存
<span style="color: #0000ff;">function<span style="color: #000000;"> saveRegions(data) {
<span style="color: #0000ff;">var id =<span style="color: #000000;"> data.node.original.RegionsId;
<span style="color: #0000ff;">var name = data.node.text;<span style="color: #008000;">//<span style="color: #008000;">修改后的name
<span style="color: #008000;">//<span style="color: #008000;">var oldName = data.old;//原name
<span style="color: #008000;">//<span style="color: #008000;">var pNode = $('#jstree_demo').jstree().get_node(data.node.parent).original.RegionsId;
<span style="color: #0000ff;">var josnData = { "Id": id,"Node": data.node.id,"ParentNode": data.node.parent,"Name"<span style="color: #000000;">: name };
$.ajax({
url: "/Home/SaveRegions"<span style="color: #000000;">,data: josnData,success: <span style="color: #0000ff;">function<span style="color: #000000;"> (sData) {
data.node.original.RegionsId =<span style="color: #000000;"> sData;
data.node.state.opened = <span style="color: #0000ff;">false;<span style="color: #008000;">//<span style="color: #008000;">是否展开
<span style="color: #000000;"> }
});
}
ref = $('#jstree_demo').jstree(= (!sel.length) { = sel[0= ref.create_node(sel,{ "type": "file"<span style="color: #0000ff;">function<span style="color: #000000;"> renameTree() {
<span style="color: #0000ff;">var ref = $('#jstree_demo').jstree(<span style="color: #0000ff;">true<span style="color: #000000;">),sel =<span style="color: #000000;"> ref.get_selected();
<span style="color: #0000ff;">if (!sel.length) { <span style="color: #0000ff;">return <span style="color: #0000ff;">false<span style="color: #000000;">; }
sel = sel[0<span style="color: #000000;">];
ref.edit(sel,<span style="color: #0000ff;">function<span style="color: #000000;"> () {
});
};
<span style="color: #0000ff;">function<span style="color: #000000;"> deleteTree() {
<span style="color: #0000ff;">var ref = $('#jstree_demo').jstree(<span style="color: #0000ff;">true<span style="color: #000000;">),sel =<span style="color: #000000;"> ref.get_selected();
<span style="color: #0000ff;">if (!sel.length) { <span style="color: #0000ff;">return <span style="color: #0000ff;">false<span style="color: #000000;">; }
ref.delete_node(sel);
};
input.Node = Guid.NewGuid().To
String(
);