استفاده از افزونهی jsTree در ASP.NET MVC
نویسنده: وحید نصیری
تاریخ: ۱۳۹۳/۰۳/۱۹ ۷:۵
آدرس: www.dntips.ir
| مطالب | ۳۶۹۴ |
| نویسندگان | ۲۷۶ |
| گروههای مطالب | ۱۰۲۴ |
| نقشههای راه | ۱۱۹ |
| دورهها | ۱۴ |
| اشتراکها | ۱۷۹۱۴ |
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width" />
<title>@ViewBag.Title</title>
<link href="~/Content/Site.css" rel="stylesheet" />
<link href="~/Content/themes/default/style.min.css" rel="stylesheet" />
<script src="~/Scripts/jquery.min.js"></script>
<script src="~/Scripts/jstree.min.js"></script>
</head>
<body dir="rtl">
@RenderBody()
@RenderSection("scripts", required: false)
</body>
</html> right_to_left = $("body").css("direction") === "rtl"; using System.Collections.Generic;
namespace MvcJSTree.Models
{
public class JsTreeNode
{
public string id { set; get; } // نام این خواص باید با مستندات هماهنگ باشد
public string text { set; get; }
public string icon { set; get; }
public JsTreeNodeState state { set; get; }
public List<JsTreeNode> children { set; get; }
public JsTreeNodeLiAttributes li_attr { set; get; }
public JsTreeNodeAAttributes a_attr { set; get; }
public JsTreeNode()
{
state = new JsTreeNodeState();
children = new List<JsTreeNode>();
li_attr = new JsTreeNodeLiAttributes();
a_attr = new JsTreeNodeAAttributes();
}
}
public class JsTreeNodeAAttributes
{
// به هر تعداد و نام اختیاری میتوان خاصیت تعریف کرد
public string href { set; get; }
}
public class JsTreeNodeLiAttributes
{
// به هر تعداد و نام اختیاری میتوان خاصیت تعریف کرد
public string data { set; get; }
}
public class JsTreeNodeState
{
public bool opened { set; get; }
public bool disabled { set; get; }
public bool selected { set; get; }
public JsTreeNodeState()
{
opened = true;
}
}
} [HttpPost]
public ActionResult GetTreeJson()
{
var nodesList = new List<JsTreeNode>();
var rootNode = new JsTreeNode
{
id = "dir",
text = "Root 1",
icon = Url.Content("~/Content/images/tree_icon.png"),
a_attr = { href = "http://www.bing.com" }
};
nodesList.Add(rootNode);
nodesList.Add(new JsTreeNode
{
id = "test1",
text = "Root 2",
icon = Url.Content("~/Content/images/tree_icon.png"),
a_attr = { href = "http://www.bing.com" }
});
return Json(nodesList, JsonRequestBehavior.AllowGet);
} <div id="jstree"> </div>
$('#jstree').jstree({
"core": {
"multiple": false,
"check_callback": true,
'data': {
'url': '@getTreeJsonUrl',
"type": "POST",
"dataType": "json",
"contentType": "application/json; charset=utf8",
'data': function (node) {
return { 'id': node.id };
}
},
'themes': {
'variant': 'small',
'stripes': true
}
},
"types": {
"default": {
"icon": '@Url.Content("~/Content/images/bookmark_book_open.png")'
},
},
"plugins": ["contextmenu", "dnd", "state", "types", "wholerow", "sort", "unique"],
"contextmenu": {
"items": function (o, cb) {
var items = $.jstree.defaults.contextmenu.items();
items["create"].label = "ایجاد زیر شاخه";
items["rename"].label = "تغییر نام";
items["remove"].label = "حذف";
var cpp = items["ccp"];
cpp.label = "ویرایش";
var subMenu = cpp["submenu"];
subMenu["copy"].label = "کپی";
subMenu["paste"].label = "پیست";
subMenu["cut"].label = "برش";
return items;
}
}
}); @{
ViewBag.Title = "Demo";
var getTreeJsonUrl = Url.Action(actionName: "GetTreeJson", controllerName: "Home");
} $('#jstree').jstree({
// تمام تنظیمات مانند قبل
}).on('delete_node.jstree', function (e, data) {
})
.on('create_node.jstree', function (e, data) {
})
.on('rename_node.jstree', function (e, data) {
})
.on('move_node.jstree', function (e, data) {
})
.on('copy_node.jstree', function (e, data) {
})
.on('changed.jstree', function (e, data) {
})
.on('dblclick.jstree', function (e) {
})
.on('select_node.jstree', function (e, data) {
}); function postJsTreeOperation(operation, data, onDone, onFail) {
$.post('@doJsTreeOperationUrl',
{
'operation': operation,
'id': data.node.id,
'parentId': data.node.parent,
'position': data.position,
'text': data.node.text,
'originalId': data.original ? data.original.id : data.node.original.id,
'href': data.node.a_attr.href
})
.done(function (result) {
onDone(result);
})
.fail(function (result) {
alert('failed.....');
onFail(result);
});
} .on('create_node.jstree', function (e, data) {
postJsTreeOperation('CreateNode', data,
function (result) {
data.instance.set_id(data.node, result.id);
},
function (result) {
data.instance.refresh();
});
}) [HttpPost]
public ActionResult DoJsTreeOperation(JsTreeOperationData data)
{
switch (data.Operation)
{
case JsTreeOperation.CopyNode:
case JsTreeOperation.CreateNode:
//todo: save data
var rnd = new Random(); // آی دی رکورد پس از ثبت در بانک اطلاعاتی دریافت و بازگشت داده شود
return Json(new { id = rnd.Next() }, JsonRequestBehavior.AllowGet);
case JsTreeOperation.DeleteNode:
//todo: save data
return Json(new { result = "ok" }, JsonRequestBehavior.AllowGet);
case JsTreeOperation.MoveNode:
//todo: save data
return Json(new { result = "ok" }, JsonRequestBehavior.AllowGet);
case JsTreeOperation.RenameNode:
//todo: save data
return Json(new { result = "ok" }, JsonRequestBehavior.AllowGet);
default:
throw new InvalidOperationException(string.Format("{0} is not supported.", data.Operation));
}
} namespace MvcJSTree.Models
{
public enum JsTreeOperation
{
DeleteNode,
CreateNode,
RenameNode,
MoveNode,
CopyNode
}
public class JsTreeOperationData
{
public JsTreeOperation Operation { set; get; }
public string Id { set; get; }
public string ParentId { set; get; }
public string OriginalId { set; get; }
public string Text { set; get; }
public string Position { set; get; }
public string Href { set; get; }
}
} var selectedData;
// ...
.on('dblclick.jstree', function (e) {
var href = selectedData.node.a_attr.href;
alert('selected node: ' + selectedData.node.text + ', href:' + href);
// auto redirect
if (href) {
window.location = href;
}
// activate edit mode
//var inst = $.jstree.reference(selectedData.node);
//inst.edit(selectedData.node);
})
.on('select_node.jstree', function (e, data) {
//alert('selected node: ' + data.node.text);
selectedData = data;
}); [{"id":"OrganizationTree","text":"ساختار سازمانی","icon":"/Content/images/tree_icon.png","state":{"opened":true,"disabled":false,"selected":false},"children":[{"id":"2","text":"آنات","icon":"/Content/images/nuclear.png","state":{"opened":true,"disabled":false,"selected":false},"children":[{"id":"4","text":"آموزش","icon":"/Content/images/nuclear.png","state":{"opened":true,"disabled":false,"selected":false},"children":[],"li_attr":{"data":null},"a_attr":{"href":null}},{"id":"5","text":"هیات مدیره","icon":"/Content/images/nuclear.png","state":{"opened":true,"disabled":false,"selected":false},"children":[],"li_attr":{"data":null},"a_attr":{"href":null}}],"li_attr":{"data":null},"a_attr":{"href":null}},{"id":"1","text":"پرنیان","icon":"/Content/images/nuclear.png","state":{"opened":true,"disabled":false,"selected":false},"children":[{"id":"1","text":"BPM","icon":"/Content/images/nuclear.png","state":{"opened":true,"disabled":false,"selected":false},"children":[],"li_attr":{"data":null},"a_attr":{"href":null}},{"id":"3","text":"پشتیبانی","icon":"/Content/images/nuclear.png","state":{"opened":true,"disabled":false,"selected":false},"children":[],"li_attr":{"data":null},"a_attr":{"href":null}},{"id":"2","text":"فروش","icon":"/Content/images/nuclear.png","state":{"opened":true,"disabled":false,"selected":false},"children":[],"li_attr":{"data":null},"a_attr":{"href":null}}],"li_attr":{"data":null},"a_attr":{"href":null}}],"li_attr":{"data":null},"a_attr":{"href":null}}] اینهم تنظیمات فراخوانی
<script>
$(function () {
$('#jstree').jstree({
"core": {
"multiple": true,
"check_callback": true,
'data': {
'url': '@getTreeJsonUrl',
"type": "POST",
"dataType": "json",
"contentType": "application/json; charset=utf8",
'data': function (node) {
return { 'id': node.id };
}
},
'themes': {
'variant': 'large',
'stripes': false
}
},
"types": {
"default": {
"icon": '@Url.Content("~/Content/images/bookmark_book_open.png")'
},
},
"plugins": ["contextmenu", "dnd", "state", "types", "checkbox", "wholerow", "sort", "unique", "real_checkboxes"],
"contextmenu": {
"items": function (o, cb) {
var items = $.jstree.defaults.contextmenu.items();
items["create"].label = "ایجاد زیر شاخه";
items["rename"].label = "تغییر نام";
items["remove"].label = "حذف";
var cpp = items["ccp"];
cpp.label = "ویرایش";
var subMenu = cpp["submenu"];
subMenu["copy"].label = "کپی";
subMenu["paste"].label = "پیست";
subMenu["cut"].label = "برش";
return items;
}
}
});
});
</script> فکر میکنین مشکل از کجا باشه؟
جالب اینجاست که اگه فقط یه سطح پایین برم مشکلی نیست!
<script src="jquery.js"></script> <script src="jquery-migrate-1.2.1.js"></script>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
namespace MvcJSTree.Models.Entities
{
public class Category
{
public Category()
{
}
public Category(string id,string parentid,string orginialid,string text,string position,string href)
{
this.Id = id;
this.ParentId = parentid;
this.OriginalId = orginialid;
this.Text = text;
this.Position = position;
this.Href = href;
}
public string Id { set; get; }
public string ParentId { set; get; }
public string OriginalId { set; get; }
public string Text { set; get; }
public string Position { set; get; }
public string Href { set; get; }
public override string ToString()
{
return string.Format("{0},{1},{2},{3},{4},{5}",this.Id,this.ParentId,this.OriginalId,this.Text,this.Position,this.Href);
}
}
} using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
using System.Data.Entity;
namespace MvcJSTree.Models
{
public class DataBaseContext:System.Data.Entity.DbContext
{
public DataBaseContext()
{
}
static DataBaseContext ()
{
System.Data.Entity.Database.SetInitializer(
new System.Data.Entity.DropCreateDatabaseIfModelChanges<DataBaseContext>());
}
public System.Data.Entity.DbSet<DataBaseContext> Category { get; set; }
}
}
پارامتر رو با ویژگی [FromBody] یا بدون آن تست کردم ولی هر بار مقدار پارامتر 0 است. حتی بدون استفاده از ویو مدل هم تست کردم (مثلا int id) ولی باز هم 0 دریافت کردم. ممنون میشم راهنمایی کنید.
"dataType": "json",
"contentType": "application/x-www-form-urlencoded",
'data': function (node) {
return { 'id': node.id, recordId: 7885, name: "Test" };
}