此許可權系統的涉及到的資料表結構如下:
很簡單,有圖在此一目瞭然。一個角色對應多個功能,而每個功能又包含很多子功能,所以在設計的時候考慮要用到這5張表。在進行開發之前需要安裝和配置TreeView控制項,如果不知道怎麼用可以參看這裡:http://jigee.cnblogs.com/archive/2006/04/14/375623.html;應為實現這樣的系統主要問題是解決TreeView的問題,開發的時候考慮到代碼的簡潔和以後便於維護,所以放在了UserControl中,這個對你可能沒什麼關係,主要是因為我們的系統還有其他模組要整合在一起,通過動態載入使用者控制項的形式來完成頁面的切換工作!PS:為了不涉及公司代碼的外泄,我只將寫出一些與公司資訊無關但又是實現此功能的核心代碼,也就是說唯寫與TreeView有關的部分代碼,OK!不說了開始我們的codeing ! TeeView的HTML如下:
<div id="dvRolesFunction" style="BORDER-RIGHT: #99ccff 2px solid; BORDER-TOP: #99ccff 2px solid; OVERFLOW: auto; BORDER-LEFT: #99ccff 2px solid; WIDTH: 400px; BORDER-BOTTOM: #99ccff 2px solid; HEIGHT: 485px">
<iewc:treeview id="tvRoleFunctions" runat="server"SystemImagesPath="../webctrl_client/1_0/treeimages/" showlines="true" shoplus="true">
</iewc:treeview>
</div>
填充TeeView控制項的主要代碼如下:
private void FillFuntionAndPointInfo()
{
string name, value;
TreeNode parentTreeNode = null;
TreeNode childTreeNode;
DataSet ds = MappingUser.GetFunctionAndFunctionPoint();
if (ds != null && ds.Tables[0] != null)
{
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
value = Common.GetFieldValue(ds.Tables[0].Rows[i]["functionid"], ""); //為了獲得有效傳回值
name = Common.GetFieldValue(ds.Tables[0].Rows[i]["functionname"], "");// .DLL不會公開
if (parentTreeNode == null || parentTreeNode.Text != name)
{
parentTreeNode = new TreeNode();
parentTreeNode.Text = name;
parentTreeNode.CheckBox=true;
parentTreeNode.ID =value;
parentTreeNode.Expandable=ExpandableValue.Auto;
tvRoleFunctions.Nodes.Add(parentTreeNode);
}
value = Common.GetFieldValue(ds.Tables[0].Rows[i]["functionpointid"], "").Trim();
name = Common.GetFieldValue(ds.Tables[0].Rows[i]["functionpointname"], "");
if (value.Length > 0)
{
childTreeNode = new TreeNode();
childTreeNode.Text = name;
childTreeNode.CheckBox=true;
childTreeNode.ID = value;
childTreeNode.Expandable=ExpandableValue.Auto;
parentTreeNode.Nodes.Add(childTreeNode);
}
}
}
}
GetFunctionAndFunctionPoint()方法用到的Sql語句如下:
SELECT functionid,
b.name AS functionname,
functionpointid,
c.name AS functionpointname
FROM Function b (NOLOCK),
FunctionRelationPoint a (NOLOCK)
LEFT JOIN FunctionPoint c (NOLOCK) ON a.FunctionPointID = c.id
WHERE a.FunctionID = b.id
ORDER BY functionid,functionpointid
下次寫作預告:
怎麼用指令碼實現TreeView的父子節點聯動和全選的功能,之前我在網上和CSDN找過一個實現父子節點聯動效果的方法,可惜是在TreeView.htc檔案中實現,由於考慮到項目部署時可能帶來問題,乾脆自己用javaScript實現。