如何在扩展剑道树视图上加载子节点

时间:2014-11-11 09:26:15

标签: c# jquery asp.net-mvc kendo-treeview

我想从我的电脑上的文件夹和文件中创建一个treeview nodes。用户可以dragdrop nodes

我的设计视图(cshtml):

@(
    Html.Kendo().TreeView()
        .Name("treeview")
        .Events(ev=>ev.Expand("onExpand"))
        .BindTo((IEnumerable<TreeViewItemModel>)ViewBag.ParentNode)
)

我的控制器:

public class HomeController : Controller
{
    public ActionResult TreeView()
    {
        ViewBag.ParentNode = GetDefaultNode();
        return View();
    }
    private IEnumerable<TreeViewItemModel> GetDefaultNode()
    {
        List<TreeViewItemModel> defaultNode = new List<TreeViewItemModel>();
        string[] drivers = Directory.GetLogicalDrives();
        for (int i = 0; i < drivers.Length; i++)
        {
                TreeViewItemModel node = new TreeViewItemModel();
                node.Text = drivers[i];
                node.Url = drivers[i];
                defaultNode.Add(node);
                GetChildNode(node);
        }

        return defaultNode;
    }
    private void GetChildNode(TreeViewItemModel item)
    {
        if (Directory.Exists(item.Text))
        {
            try
            {
                string[] dirNodes = Directory.GetDirectories(item.Text);
                if (dirNodes.Length > 0)
                {
                    for (int i = 0; i < dirNodes.Length; i++)
                    {
                        TreeViewItemModel child = new TreeViewItemModel();
                        DirectoryInfo dirInfo = new DirectoryInfo(dirNodes[i]);
                        child.Text = dirInfo.Name;
                        child.Url = dirInfo.FullName;
                        child.HasChildren = true;
                        item.Items.Add(child);
                        child.Items.Add(new TreeViewItemModel());
                    }
                }
                string[] fileNodes = Directory.GetFiles(item.Text);
                if (fileNodes.Length > 0)
                {
                    for (int f = 0; f < fileNodes.Length; f++)
                    {
                        TreeViewItemModel child = new TreeViewItemModel();
                        FileInfo fileInfo = new FileInfo(fileNodes[f]);
                        child.Text = fileInfo.Name;
                        child.Url = fileInfo.FullName;
                        child.HasChildren = true;
                        item.Items.Add(child);
                    }
                }

            }
            catch { }

        }
    }
}

显示根node和root node的子项。

我希望用户扩展根node加载子节点。所以我在onExpand中声明了事件treeview,但我不知道如何将当前的扩展节点传递给控制器​​,并在插入后获取nodes的视图列表它当前扩展节点。 我在这个页面和谷歌搜索了很多,但无法找到解决问题的方法。

ps:抱歉,因为我的英语不好,希望你能理解我的意思。

更新

我改为使用绑定到远程日期,但我仍然没有解决我的问题。

我的cshtml:

    @(Html.Kendo().TreeView()
.Name("treeview")
.DataTextField("Text")
.DataUrlField("Url")
.DataSource(dataSource => dataSource
    .Read(read => read
        .Action("GetRoot", "Home")
    )
)
)

我的控制器:

        public JsonResult GetRoot()
    {
        return Json(GetDefaultNode(), JsonRequestBehavior.AllowGet);
    }
    private IEnumerable<TreeViewItemModel> GetDefaultNode()
    {
        List<TreeViewItemModel> defaultNode = new List<TreeViewItemModel>();
        string[] drivers = Directory.GetLogicalDrives();
        for (int i = 0; i < drivers.Length; i++)
        {
                TreeViewItemModel node = new TreeViewItemModel();
                node.Text = drivers[i];
                node.Url = drivers[i];
                defaultNode.Add(node);
                GetChildNode(node);
        }

        return defaultNode;
    }
    private void GetChildNode(TreeViewItemModel item)
    {
        if (Directory.Exists(item.Text))
        {
            try
            {
                string[] dirNodes = Directory.GetDirectories(item.Text);
                if (dirNodes.Length > 0)
                {
                    for (int i = 0; i < dirNodes.Length; i++)
                    {
                        TreeViewItemModel child = new TreeViewItemModel();
                        DirectoryInfo dirInfo = new DirectoryInfo(dirNodes[i]);
                        child.Text = dirInfo.Name;
                        child.Url = dirInfo.FullName;
                        item.Items.Add(child);
                    }
                }
                string[] fileNodes = Directory.GetFiles(item.Text);
                if (fileNodes.Length > 0)
                {
                    for (int f = 0; f < fileNodes.Length; f++)
                    {
                        TreeViewItemModel child = new TreeViewItemModel();
                        FileInfo fileInfo = new FileInfo(fileNodes[f]);
                        child.Text = fileInfo.Name;
                        child.Url = fileInfo.FullName;
                        item.Items.Add(child);
                    }
                }

            }
            catch { }

        }
    }

这就是结果:

enter image description here

如您所见,它只显示root nodes而不显示child nodes。当我调试时,根节点包含子项

enter image description here

1 个答案:

答案 0 :(得分:0)

问题解决了。使用远程数据绑定时,节点只加载在同一级别上。当onExpand fire时,它将再次调用方法GetNodes。

 public JsonResult GetNodes(string id)
    {
        List<Node> node = new List<Node>();
        List<string> drivers=new List<string>();
        if (string.IsNullOrEmpty(id))
        {
            drivers.AddRange(Directory.GetLogicalDrives());
        }
        else
        {
            if(Directory.Exists(id))
                drivers.AddRange(Directory.GetDirectories(id));
        }

            try
            {
                for (int i = 0; i < drivers.Count; i++)
                {
                    Node item = new Node();
                    DirectoryInfo dirInfo = new DirectoryInfo(drivers[i]);
                    item.id = dirInfo.FullName;
                    item.Name = dirInfo.Name;
                    item.hasChildren = HasNodes(drivers[i]);
                    node.Add(item);
                }
            }
            catch { }

        return Json(node, JsonRequestBehavior.AllowGet);
    }