前段时间用coolite的一个动态动态结构的树形结构,可以根据传递过来的参数的个数来判断执行不同的方法
image
image

image

截个不多说了,因为没有提供数据库,有需要的大家就简单的参考下代码吧

传递参数的方法通过url传递:Tree.aspx?strings=1,15

1.传递一个值strings=1      //直接为选择的grid的ID号

2.传递两个值strings=1,15     //第一个为类型,第二个为ID

文件1:Tree.aspx,结构如下

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Tree.aspx.cs" Inherits="demoofzhai_Tree" %>

<%@ Register Assembly="Coolite.Ext.Web" Namespace="Coolite.Ext.Web" TagPrefix="ext" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>无标题页</title>

    <script type="text/javascript">
        function nodeLoad(node) {
            Coolite.AjaxMethods.NodeLoad(node.id, {
                success: function(result) {
                    var data = eval("(" + result + ")");
                    node.loadNodes(data);
                },

                failure: function(errorMsg) {
                    Ext.Msg.alert(‘Failure’, errorMsg);
                }
            });           
        }
        function jsBindStore(node) {
            if(node.id==’0′)
            {
            } else  if(node.id.substring(0,1)=="A")
            {
            Coolite.AjaxMethods.BindStore(node.id.substring(1));
            }
        }

    </script>

    <script type="text/javascript">
     FormatType=function (value){
        if(value ==’0′){
            return ‘整型’;
        }else if(value ==’1′){
            return ‘浮点型’;
        }else if(value ==’2′){
            return ‘字符型’;
        }else if(value ==’3′){
            return ‘字符串型’;
        }else if(value ==’4′){
            return ‘日期型’;
        }else if(value ==’6′){
            return ‘大数据’;
        }else if(value ==’7′){
            return ‘大文本’;
        }
     }
     IsPrimary=function (value){
        if(value ==’0′){
            return ‘否’;
        }else if(value==’1′){
            return ‘是’;
        }
     }
    </script>

</head>
<body>
    <form id="form1" runat="server">
    <ext:ScriptManager ID="ScriptManager1" runat="server" ScriptMode="Debug" />
    <ext:Store ID="Store1" runat="server">
        <Reader>
            <ext:JsonReader>
                <Fields>
                    <ext:RecordField Name="M_FDNO">
                    </ext:RecordField>
                    <ext:RecordField Name="M_ALIAS">
                    </ext:RecordField>
                    <ext:RecordField Name="M_TYPE">
                    </ext:RecordField>
                    <ext:RecordField Name="M_SIZE">
                    </ext:RecordField>
                    <ext:RecordField Name="M_FIELD">
                    </ext:RecordField>
                    <ext:RecordField Name="M_KEYER">
                    </ext:RecordField>
                </Fields>
            </ext:JsonReader>
        </Reader>
    </ext:Store>
    <ext:ViewPort ID="ViewPort1" runat="server">
        <Body>
            <ext:BorderLayout ID="BorderLayout1" runat="server">
                <West Split="true" CollapseMode="Mini">
                    <ext:TreePanel ID="TreePanel1" runat="server" Title="模型的表" Border="false" Width="200"
                        Height="0.8" Collapsible="true" ContainerScroll="true">
                        <Root>
                            <ext:AsyncTreeNode NodeID="0" Text="Root" Qtip="0" Expanded="true" />
                        </Root>
                        <Listeners>
                            <BeforeLoad Fn="nodeLoad" />
                            <Click Fn="jsBindStore" />
                        </Listeners>
                    </ext:TreePanel>
                </West>
                <Center>
                    <ext:GridPanel runat="server" ID="GridPanel1" Width="300" StoreID="Store1" Title="字段显示"
                        StripeRows="true" TrackMouseOver="true">
                        <View>
                            <ext:GridView ForceFit="true">
                            </ext:GridView>
                        </View>
                        <ColumnModel ID="ColumnModel1" runat="server">
                            <Columns>
                                <ext:RowNumbererColumn />
                                <ext:Column Header="编号" DataIndex="M_FDNO" Align="Center">
                                </ext:Column>
                                <ext:Column Header="名称" DataIndex="M_ALIAS" Align="Center">
                                </ext:Column>
                                <ext:Column Header="类型" DataIndex="M_TYPE" Align="Center">
                                    <Renderer Fn="FormatType" />
                                </ext:Column>
                                <ext:Column Header="大小" DataIndex="M_SIZE" Align="Center">
                                </ext:Column>
                                <ext:Column Header="存储位置" DataIndex="M_FIELD" Align="Center">
                                </ext:Column>
                                <ext:Column Header="是否主键" DataIndex="M_KEYER" Align="Center">
                                    <Renderer Fn="IsPrimary" />
                                </ext:Column>
                            </Columns>
                        </ColumnModel>
                        <SelectionModel>
                            <ext:RowSelectionModel ID="RowSelectionModel1" runat="server" SingleSelect="true">
                            </ext:RowSelectionModel>
                        </SelectionModel>
                    </ext:GridPanel>
                </Center>
                <South>
                    <ext:Panel ID="sqs" Title="调试面板" runat="server" Height="100">
                        <Body>
                            <ext:Button ID="Button1" runat="server" Text="输出表">
                                <Listeners>
                                    <Click Handler="
                if(TreePanel1.getSelectionModel().getSelectedNode()==null)
                {
                alert(‘您还没有选中表,请选中一行!’);
                }else
                {
                alert(TreePanel1.getSelectionModel().getSelectedNode().id.substring(1));
                }
                " />
                                </Listeners>
                            </ext:Button>
                            <ext:Button ID="Button2" runat="server" Text="输出字段">
                                <Listeners>
                                    <Click Handler="
                if(GridPanel1.getSelectionModel().getSelected()==null||GridPanel1.getSelectionModel().getSelected()==’undefined’)
                {
                alert(‘您还没有选中字段,请选中一行!’);
                }else               
                {               
                alert(GridPanel1.getSelectionModel().getSelected().data.M_FDNO);
                }
                " />
                                </Listeners>
                            </ext:Button>
                        </Body>
                    </ext:Panel>
                </South>
            </ext:BorderLayout>
        </Body>
    </ext:ViewPort>
    </form>
</body>
</html>

文件2:Tree.aspx.cs,结构如下

using System;
using System.Data;
using Coolite.Ext.Web;
using System.Collections.Generic;

public partial class demoofzhai_Tree : System.Web.UI.Page
{
    public static string sModelType = "";   //模型类型  简单为0 复杂为1
    public static string sModelID = "";     //模型ID
    public static string sTableID = "";    //表ID
    public static int flag=1;
    public static string flagstr = "";
    public static int iType = 1;      //标记当前传递的类型,1为传递的字符串是 两个值,模型类型+模型表ID
                                        //                    2 为传的单个值, 传递的是字段ID
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Ext.IsAjaxRequest)
        {

            if (Request.Params["strings"] != null)
            {
                string str = Request.Params["strings"];
                //如果传进来的是一个值的话

                if (str.IndexOf(‘,’) == -1)
                {

                    sTableID = str;
                    iType = 1; 

                }
                else
                {
                    if (str.Split(‘,’).Length == 2)
                    {
                        sModelType = str.Split(‘,’)[0];
                        sModelID = str.Split(‘,’)[1];
                        iType = 2;
                    }
                    else
                    {
                        Ext.Msg.Alert("系统提示", "当前传递的值出错!").Show();
                    }
                }
            }
            else
            {
                Ext.Msg.Alert("系统提示", "当前传递的值为空!").Show();
            }

        }
         TreePanel1.RootVisible = false;
        // TreePanel1.Width = System.Web.UI.WebControls.Unit.Percentage(50);
    }

    [AjaxMethod]
    public  string NodeLoad(string nodeID)
    {

        if (iType == 2)
        {
            if (sModelType == "0")//如果是简单模型的话 进行以下处理
            {
                Coolite.Ext.Web.TreeNodeCollection nodes = new Coolite.Ext.Web.TreeNodeCollection();
                if (!string.IsNullOrEmpty(nodeID))
                {
                    string sql = "SELECT mt_ident,mt_name,mt_main FROM oa2_modtree where m_ident=’" + sModelID + "’ and mt_main=’" + nodeID + "’  order by mt_ident";
                    DataTable dt = KqDbOperationLibry.DBOperateHelper.GetDBInstence("MISDataSouce").GetDataTableBySQL(sql);//所有的内容

                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        AsyncTreeNode asyncNode = new AsyncTreeNode();
                        asyncNode.Text = dt.Rows[i]["mt_name"].ToString();
                        asyncNode.NodeID = dt.Rows[i]["mt_ident"].ToString();
                        asyncNode.Qtip = asyncNode.NodeID;
                        asyncNode.Icon = Icon.Accept;
                        nodes.Add(asyncNode);

                    }
                }

                BindStore(nodeID);
                return nodes.ToJson();

            }
            else if (sModelType == "1")  //如果是复杂模型的话进行以下处理
            {

                //首先显示所有的简单模型
                Coolite.Ext.Web.TreeNodeCollection nodes = new Coolite.Ext.Web.TreeNodeCollection();
                if (!string.IsNullOrEmpty(nodeID))
                {

                    if (nodeID.Substring(0, 1) == "A")   // 如果当前节点是简单模型的树节点的话直接进入
                    {

                        //加载当前节点的简单模型

                        string sql = "SELECT mt_ident,mt_name,mt_main FROM oa2_modtree where  mt_main=’" + nodeID.Substring(1) + "’  order by mt_ident";
                        DataTable dt = KqDbOperationLibry.DBOperateHelper.GetDBInstence("MISDataSouce").GetDataTableBySQL(sql);//所有的内容

                        for (int i = 0; i < dt.Rows.Count; i++)
                        {
                            AsyncTreeNode asyncNode = new AsyncTreeNode();
                            asyncNode.Text = dt.Rows[i]["mt_name"].ToString();
                            asyncNode.NodeID = "A" + dt.Rows[i]["mt_ident"].ToString();
                            asyncNode.Qtip = asyncNode.NodeID;
                            asyncNode.Icon = Icon.Accept;
                            nodes.Add(asyncNode);
                        }

                        BindStore(nodeID.Substring(1));
                    }

                    else if (nodeID == "0")
                    {

                        //查询当前复杂模型所在的所有简单模型
                        string sql1 = " SELECT M_ident1,m_ident2 FROM OA2_MAPREL WHERE M_IDENT=’" + sModelID + "’ order by m_ident1,m_ident2";
                        //  string sql = "SELECT mt_ident,mt_name,mt_main FROM oa2_modtree where m_ident=’" + sModelID + "’ and mt_main=’" + nodeID + "’  order by mt_ident";
                        DataTable dt1 = KqDbOperationLibry.DBOperateHelper.GetDBInstence("MISDataSouce").GetDataTableBySQL(sql1);//所有的内容

                        //以上将获取当前复杂模型的包括的几个简单模型ID
                        int iSimpleModelCount = 0;
                        string simple = "";
                        for (int i1 = 0; i1 < dt1.Rows.Count; i1++) //循环取得简单模型的ID数组
                        {
                            iSimpleModelCount++;
                            simple = simple + dt1.Rows[i1]["m_ident1"].ToString() + "," + dt1.Rows[i1]["m_ident2"].ToString() + ",";
                        }
                        simple = simple.Substring(0, simple.Length – 1);
                        //再将simple 中重复的去掉
                        Array stringArray = simple.Split(‘,’);

                        List<string> listString = new List<string>();
                        foreach (string eachString in stringArray)
                        {
                            if (!listString.Contains(eachString))
                                listString.Add(eachString);
                        }

                        //当前的三个简单对象为Liststring  
                        //遍历出来

                        foreach (string lstring in listString)   //测试值 简单模型1,2,3
                        {
                            //Console.WriteLine(dinosaur);
                            AsyncTreeNode asyncNode = new AsyncTreeNode();
                            //  asyncNode.NodeID = "A" + lstring;
                            //得到简单模型的ID, 查询出当前简单模型树的ID值,Text值
                            string sql_simpletree = "select mt_ident,mt_main,mt_name from oa2_modtree where  mt_ident=’" + lstring + "’ and mt_main=’0’  order by mt_ident";
                            DataTable dt_simpletree = KqDbOperationLibry.DBOperateHelper.GetDBInstence("MISDataSouce").GetDataTableBySQL(sql_simpletree); //所有的内容
                            if (dt_simpletree.Rows.Count == 0)  //如果当前的简单模型被删除掉了的话
                            { }
                            else
                            {

                                asyncNode.NodeID = "A" + dt_simpletree.Rows[0]["mt_ident"].ToString();
                                asyncNode.Text = dt_simpletree.Rows[0]["mt_name"].ToString();
                                asyncNode.Qtip = asyncNode.NodeID;
                                asyncNode.Icon = Icon.Accept;
                                nodes.Add(asyncNode);
                            }
                        }

                    }
                }
                return nodes.ToJson();

            }
            else
            {

                // Ext.Msg.Alert("系统提示","值有错误请检查!").Show();
                //   return nodes.ToJson();

                return null;
            }
        }
        else 
        {
            //加载树节点

                //加载当前节点的简单模型
             Coolite.Ext.Web.TreeNodeCollection nodes = new Coolite.Ext.Web.TreeNodeCollection();
             string sql = "SELECT mt_ident,mt_name,mt_main FROM oa2_modtree where  mt_ident=’" + sTableID + "’  order by mt_ident";
                DataTable dt = KqDbOperationLibry.DBOperateHelper.GetDBInstence("MISDataSouce").GetDataTableBySQL(sql);//所有的内容

                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    AsyncTreeNode asyncNode = new AsyncTreeNode();
                    asyncNode.Text = dt.Rows[i]["mt_name"].ToString();
                    asyncNode.NodeID = "A" + dt.Rows[i]["mt_ident"].ToString();
                    asyncNode.Qtip = asyncNode.NodeID;
                    asyncNode.Icon = Icon.Add;
                    asyncNode.Leaf = true;
                    nodes.Add(asyncNode);
                }

                BindStore(nodeID.Substring(1));
                return nodes.ToJson();

        }

    }

    //绑定右边的字段
   [AjaxMethod]
    public void BindStore(string NodeID)  //根据左边的ID的值来选择右这边的值
    {
        try
        {
            //NodeID里记录有模型类型和模型ID 中间用 ‘|’ 符号隔开

            string sql5 = "select M_FDNO,M_ALIAS,M_TYPE,M_SIZE,M_FIELD,M_KEYER  from oa2_mapped where m_main=’" + NodeID + "’ ";
            DataTable dt5 = KqDbOperationLibry.DBOperateHelper.GetDBInstence("MISDataSouce").GetDataTableBySQL(sql5); //所有的内容
            Store1.DataSource = dt5;
            Store1.DataBind();

        }
        catch (Exception e1) { }
    }

}