Extjs實作類別似樹下拉框Ext.form.TriggerField

來源:互聯網
上載者:User

網上找了些資料,寫了個方便以後進行尋找

構建一棵樹:

var tl = new Ext.tree.TreeLoader({});
    var tp = new Ext.tree.TreePanel({
        animate: true,
        frame: true,
        autoScroll: true,
        height: 270,
        width: 200,
        border: false,
        useArrows: false,
        trackMouseOver: false,
        lines: false,
        listeners: {
            'click': function (node, e) {
                field.setValue(node.text);//點擊把值填充到下拉框中
            }
        },
        loader: tl
    });

    var root = new Ext.tree.AsyncTreeNode({
        id: 'root',
        text: '所有省份',
        expanded: true
    });
    tp.setRootNode(root);

 

//後台載入資料

tp.on("beforeload", function (node) {
        tl.dataUrl = '/Index/mainIndex';
});

 

//建立一個菜單用來儲存 樹

 var selectMenu = new Ext.menu.Menu({
        items: [tp]
    });

//建立一個下拉框,因為ComboBox是繼承於Ext.form.TriggerField,重寫他的onTriggerClick()函數實現快顯視窗
    var field = new Ext.form.TriggerField({
        fieldLabel: '選擇',
        name: 'name',
        onTriggerClick: function () {
            if (this.menu == undefined) {
                this.menu = selectMenu;
            }
            this.menu.show(this.el, 'tl-bl?');//'tl-bl?'表示相對於下拉框顯示的位置
        }
    })

 

//取樹的後台代碼

public JsonResult mainIndex()
        {
            List<Node> nodes = new List<Node>();
            Node nodesOne = null;
            Node nodesTwo = null;
            var province = proObject.GetList("ProvinceInfo");
            foreach (ProvinceInfo pro in province)
            {
                nodesOne = new Node();
                nodesOne.id = "pro_" + pro.ProvinceID;
                nodesOne.text = pro.ProvinceName;
                nodesOne.leaf = false;
                IList<CriteriaProjection> fields = new List<CriteriaProjection>()
                 {
                new CriteriaProjection(){ fieldName = "ProvinceID", fieldValue = pro.ProvinceID, expression= CExpression.Eq}
                 };
                var city = cityObject.GetList(fields, null, 0, 0);
                foreach (CityInfo ci in city)
                {
                    nodesTwo = new Node();
                    nodesTwo.id = "city_" + ci.CityID;
                    nodesTwo.text = ci.CityName;
                    nodesTwo.leaf = true;
                    nodesOne.children.Add(nodesTwo);
                }
                nodes.Add(nodesOne);
            }
            return Json(nodes, JsonRequestBehavior.AllowGet);
        }
    }

 

//Node 類

public class Node
    {
        public string id;
        public string text;
        public bool leaf;
        public List<Node> children = new List<Node>();
    }

 

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.