效果图:
前台部份:
<ext:Store ID="Store1" runat="server" AutoLoad="false" >
<Reader>
<ext:JsonReader ReaderID="id">
<Fields>
<ext:RecordField Name="id" Type="String" Mapping="id" />
<ext:RecordField Name="name" Type="String" Mapping="name" />
</Fields>
</ext:JsonReader>
</Reader>
</ext:Store>
<ext:ComboBox ID="cboSortCoding" runat="server" Enabled="true" FieldLabel="知识类别" EmptyText="请选择知识类别"
AllowBlank="false" BlankText="请选择知识类别!" Width="260" DisplayField="name" ValueField="id"
StoreID="Store1" Editable="false" />
后台部分:
System.Data.DataView dv;
List<object> data = new List<object>();
int NodeLevelNum = 0;
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
clsDA_kno_KnowledgeSort myKnoSort = new clsDA_kno_KnowledgeSort();//读数据库
dv =myKnoSort.m_Select().DefaultView;//得到DataTable
m_InitKnoSort(0);
this.Store1.DataSource = data;
this.Store1.DataBind();
}
}
//初始化下拉菜单
private void m_InitKnoSort(int intParentNote)
{
dv.RowFilter = String.Format("ParentNode={0}", intParentNote);
foreach (DataRowView drv in dv)
{
if (intParentNote != 0)//第一个节点不加
{
string strNodeName = drv["NodeName"].ToString();
strNodeName = m_NodeLevel(intParentNote) + strNodeName;
data.Add(new { id = drv["Node"].ToString(), name = strNodeName });
}
m_InitKnoSort((int)drv["Node"]);
}
}
//通过节点级数合成为 2级则为"--"
private string m_NodeLevel(int intParentNote)
{
string str = "";
NodeLevelNum = 0;
m_NodeLevelNum(intParentNote);
for (int i =2; i < NodeLevelNum; i++)
{
str = str + "—";
}
return str;
}
//获取节点级数
private void m_NodeLevelNum(int intParentNote)
{
NodeLevelNum = NodeLevelNum + 1;
System.Data.DataView dv1 = new System.Data.DataView();
dv1=dv;
dv1.RowFilter = String.Format("Node={0}", intParentNote);
foreach (DataRowView drv in dv)
{
m_NodeLevelNum((int)drv["ParentNode"]);
}
}
数据库部分:
select * from kno_KnowledgeSort ORDER BY NodeOrder asc