简体   繁体   English

从C#MVC控制器加载EXT JS树面板

[英]EXT JS Tree panel loading from C# MVC controller

I've had good luck populating EXT JS combo boxes and list boxes using C# MVC 2 & 3, but now I'm trying to tackle loading an EXT JS tree panel. 我很幸运使用C#MVC 2和3填充EXT JS组合框和列表框,但是现在我试图解决加载EXT JS树形面板的问题。 So far I can't get the tree to load from anything I put on the controller. 到目前为止,我无法从放置在控制器上的任何内容加载树。

Right now I'm experimenting with simply returning static data from the controller, such as: 现在,我正在尝试简单地从控制器返回静态数据,例如:

 public  ActionResult TreeFill()
        {
            var stuff = "[{ text: Type1, id: 100, leaf: false, cls: 'folder', children: [{ text: 'Client 1', id: 1, leaf: true, cls: 'file' },{ text: 'Client 2', id: 2, leaf: true, cls: 'file' },{ text: 'Client 3', id: 3, leaf: true, cls: 'file' },{ text: 'Client 4', id: 4, leaf: true, cls: 'file' }]}";

            return Json(new {stuff},JsonRequestBehavior.AllowGet);
        }

...and I'm obviously having troubles with this, but it was a starting point (in the sense that what doesn't work often leads to what will work). ...而且我显然为此感到烦恼,但这是一个起点(从某种意义上说,什么不起作用通常会导致什么起作用)。 I'm now realizing that I don't even have a great starting point for tree loading from a controller. 我现在意识到,我什至没有一个从控制器加载树的好起点。 My inkling is that I'll need to return the raw data from the database and format it all via a loop into a format that the "Json" type can use. 我的暗示是,我需要从数据库返回原始数据,并通过循环将所有数据格式化为“ Json”类型可以使用的格式。 I'm not finding a lot online about this topic, so any guidance anyone can give will be much appreciated. 我在网上找不到关于该主题的很多信息,因此任何人都能提供的指导将不胜感激。 If I have any success, I'll follow up here. 如果有任何成功,我会在这里跟进。 Thank you! 谢谢!

Here's some JavaScript I used to load the Ext Tree - I know this isn't 100% what you're doing, but hopefully it helps: 这是一些我用来加载Ext Tree的JavaScript-我知道这不是100%的工作,但希望它能对您有所帮助:

tree.setRootNode(new Ext.tree.AsyncTreeNode({ id: 0, text: "Subjects", expandable: false, expanded: true }));
loadTree(function(d) {
    tree.setRootNode(new Ext.tree.AsyncTreeNode({ id: 0, text: "Subjects", children: d, expandable: false, expanded: true }));
});

function loadTree(callback) {
    runAjax("MyServiceBlahBlahBlah.asmx/Foo", "{ItemType:" + ItemType + "}",
        function(data) {
            cleanTree(data.d);
            callback(data.d);
        });
}

//hmmm - cleanTree isn't a good name for this function, not sure what I was thinking when I named it...
function cleanTree(data, expand) {
    for (var i = 0; i < data.length; i++) {
        data[i].checked = false;
        if (expand) data[i].expanded = true;

        cleanTree(data[i].children, expand);
    }
}

And here's some C# I use to build up the JSON. 这是一些我用来构建JSON的C#。 Soryr to throw the kitchen sink at you - again, hopefully some of this will help you out :) Soryr向您扔厨房水槽-再次,希望其中一些可以帮助您:)

BTW - I've used a few different web widget toolkits, and EXT is by far my favorite - so good choice! 顺便说一句-我使用了一些不同的Web窗口小部件工具包,而EXT是到目前为止我最喜欢的-很好的选择!

    public static object GetSubjectHierarchyJSON(Func<vwHierarchicalSubject, bool> InitialPredicate, HierarchicalSubjectDAO DAO, int ItemType) {
        List<LINQLayer.vwHierarchicalSubject> TempSource = DAO.GetAll(DAO.UserID, ItemType).
            OrderBy(V => V.ParentSubjectName).ThenBy(V => V.LevelNumber).ThenBy(V => V.SubjectName).ToList();

        var result = new List<object>();

        foreach (vwHierarchicalSubject V in TempSource.Where(InitialPredicate).OrderBy(v => v.SubjectName))
            result.Add(BuildJSON(V, TempSource));

        return result;
    }

    private static object BuildJSON(vwHierarchicalSubject V, List<vwHierarchicalSubject> TempSource) {
        IEnumerable<vwHierarchicalSubject> Children =
            TempSource.Where(x => x.ParentID.HasValue && x.ParentID.Value == V.id).OrderBy(v => v.SubjectName);

        int id = V.id;
        string subText = V.SubjectName.Replace("&", "&amp;");

        if (Children.Count() == 0)
            return new { id = V.id, text = subText, children = new List<object>(), expanded = true };
        else {
            var result = new { id = V.id, text = subText, children = new List<object>() };
            foreach (vwHierarchicalSubject vElement in Children)
                result.children.Add(BuildJSON(vElement, TempSource));
            return result;
        }
    }

This is a slightly brute force method (and it's not yet complete; it needs more abstractions, plus I don't like the 2 LINQ statements - it should only need 1), but it does load Tree Data with valid Json. 这是一种蛮力的方法(它还不完整;它需要更多的抽象,而且我不喜欢2条LINQ语句-它只需要1条),但是它确实使用有效的Json加载Tree Data。 Please share any red flags that might pop up with this method or if I'm heading down an icky path. 请分享任何使用此方法可能弹出的危险信号,或者如果我要驶向邪恶的道路。

public string TreeLoader()
    {
        var clis = TreeLoad();

        var parent = "[{ \"text\": \"Pharm\", \"id\": 100, \"leaf\": false, \"cls\": \"folder\", \"children\": [";

        for (int i = 1; i < clis.Count(); i++)
        {
            parent += "{\"text\": \"" + (from c in clis where c.id == i select c.text).SingleOrDefault() + "\"" +
                      ", \"id\":" + (from c in clis where c.id == i select c.id).SingleOrDefault() +
                      ", \"leaf\": true, \"cls\": \"file\"}";

            if (i < clis.Count() - 1)
            {
                parent += ", ";
            }
        }

        parent += "]}]";

        return parent;
    }

The controller only contains this: 控制器仅包含以下内容:

public ActionResult TreeSampleFill()
        {
            var result = repo.TreeLoader();

            return Content(result);
        }

The following code somewhat improves on the code above and, though still rough, it's rendering the tree perfectly: 以下代码在上面的代码上做了一些改进,尽管仍然很粗糙,但是它完美地呈现了树:

 public List<TreeNode> TreeTest()
    {
        var results1 = (from c in _Context.v_EntityTrees
                    where c.OU_TYPE_CD == "Type1"
                    orderby c.text
                    select c).ToList();

        var results2 = (from d in _Context.v_EntityTrees
                   where d.OU_TYPE_CD == "Type2"
                   orderby d.text
                   select d).ToList();

        TreeNode node = new TreeNode();
        List<TreeNode> nodelist = new List<TreeNode>();
        nodelist.Add(new TreeNode { text = "Parent1", cls = "folder", leaf = false, id = 1000, children = results1 });
        nodelist.Add(new TreeNode { text = "Parent2", cls = "folder", leaf = false, id = 2000, children = results2 });

        return nodelist;
    }

Something else I also discovered: an EXT JS tree may act strange if any of the nodes have identical ids, especially if a parent and a child have matching IDs. 我还发现了另外一些东西:如果任何节点具有相同的ID, 尤其是如果父级和子级具有匹配的ID,则EXT JS树的行为可能会很奇怪。 When I first rendered this tree the first parent would not open once the second parent was opened. 当我第一次渲染此树时,一旦打开第二个父级,第一个父级就不会打开。 Turns out its parent ID matched one of its child ids. 原来,其父ID与其子ID之一匹配。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM