簡體   English   中英

從C#MVC控制器加載EXT JS樹面板

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

我很幸運使用C#MVC 2和3填充EXT JS組合框和列表框,但是現在我試圖解決加載EXT JS樹形面板的問題。 到目前為止,我無法從放置在控制器上的任何內容加載樹。

現在,我正在嘗試簡單地從控制器返回靜態數據,例如:

 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);
        }

...而且我顯然為此感到煩惱,但這是一個起點(從某種意義上說,什么不起作用通常會導致什么起作用)。 我現在意識到,我什至沒有一個從控制器加載樹的好起點。 我的暗示是,我需要從數據庫返回原始數據,並通過循環將所有數據格式化為“ Json”類型可以使用的格式。 我在網上找不到關於該主題的很多信息,因此任何人都能提供的指導將不勝感激。 如果有任何成功,我會在這里跟進。 謝謝!

這是一些我用來加載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);
    }
}

這是一些我用來構建JSON的C#。 Soryr向您扔廚房水槽-再次,希望其中一些可以幫助您:)

順便說一句-我使用了一些不同的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;
        }
    }

這是一種蠻力的方法(它還不完整;它需要更多的抽象,而且我不喜歡2條LINQ語句-它只需要1條),但是它確實使用有效的Json加載Tree Data。 請分享任何使用此方法可能彈出的危險信號,或者如果我要駛向邪惡的道路。

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;
    }

控制器僅包含以下內容:

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

            return Content(result);
        }

以下代碼在上面的代碼上做了一些改進,盡管仍然很粗糙,但是它完美地呈現了樹:

 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;
    }

我還發現了另外一些東西:如果任何節點具有相同的ID, 尤其是如果父級和子級具有匹配的ID,則EXT JS樹的行為可能會很奇怪。 當我第一次渲染此樹時,一旦打開第二個父級,第一個父級就不會打開。 原來,其父ID與其子ID之一匹配。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM