菜单 转成 树结构 示例1(权限设置1)

简单整理了一下以前项目里写过的一个权限设置部分中的菜单的处理过程,思路比较通用,但代码不太通用,对不起大家了,以后时间充裕再整理!

(程序里得到系统所有对象的树已经分离成独立的pbl了:http://download.csdn.net/source/1551384

第一步,将菜单保存到数据库中

//函数名wf_insert

//功能:将传入的menu参数转成形成树结构的数据结构存入数据库

//参数列表 value long superoid

// value menu l_menu

// value integer level

//第一次循环将所有菜单读入数据库

long i,ll_count
long ll_oid
string ls_oname,ls_otext

INSERT INTO SOBJECTS (OID,ONAME,OTEXT,OTYPE,OLEVEL,OSORT,superOID,bz)
VALUES (0, 'menu_main','主菜单', 1, 1, 1, null,null);

ll_count=upperbound(l_menu.item)

for i=1 to ll_count
yield()
ll_oid=superoid*100+i
ls_oname=l_menu.item[i].classname()
ls_otext=l_menu.item[i].text
wf_set_stabar(ls_otext)
INSERT INTO SOBJECTS (OID,ONAME,OTEXT,OTYPE,OLEVEL,OSORT,superOID,bz)
VALUES (:ll_oid, :ls_oname,:ls_otext,1,:level,:i,:superoid,null);

if upperbound(l_menu.item[i].item)>0 then
wf_insert(ll_oid,l_menu.item[i],level+1)
else
wf_insert_win(ll_oid,l_menu.item[i],level+1)
end if
next
return 1

第二步,树的初始化

//------------------------------------------------------------------------------------------
//20070115 xuysh加载数根节点
//
//------------------------------------------------------------------------------------------
str_objects lstr_new//节点data变量
TreeViewItemltvi_Item//节点

This.DeleteItem(0)

lstr_new.oid=0//写死的,根据数据库里维护的id
lstr_new.olevel=1//写死的,根为第一级
lstr_new.osort=1//写死的,根据排序不同赋值
lstr_new.otype=1//写死的,根据类型不同赋值,1=menu 2=window 3=button 4=...还没用

ltvi_Item.Label = "主菜单"
ltvi_Item.Data = lstr_new
ltvi_Item.PictureIndex = 1
ltvi_Item.SelectedPictureIndex = ltvi_Item.PictureIndex

ltvi_Item.Children = True
il_root = This.InsertItemLast (0, ltvi_Item)

this.ExpandItem ( il_root )

第三步 在树的itempopulate事件上写上代码

//------------------------------------------------------------------------------------------
//20070115 xuysh展开树上的节点
//
//------------------------------------------------------------------------------------------

Integeri,li_rowcount
str_objects lstr_new,lstr_this,lstr_parent

//Stringls_thisdata,ls_pdata,ls_cdata
TreeViewItemltvi_child, ltvi_Parent,ltvi_this
long ll_phandle,ll_chandle,ll_Level

//取得当前节点item
This.GetItem(handle, ltvi_this)
lstr_this = ltvi_this.Data

//取得父节点ite
ll_phandle=this.FindItem ( ParentTreeItem!, handle)
if ll_phandle<>-1 then
This.GetItem(ll_phandle, ltvi_Parent)
lstr_parent = ltvi_Parent.Data
end if

//ll_Level = ltvi_this.level
//ids_objects.setfilter( "")
//ids_objects.filter()

//根据当前节点item里存储的id,过滤出他的叶节点item,并加载展开
ids_objects.setfilter( "superOID="+string(lstr_this.oid))
ids_objects.filter()
li_rowcount=ids_objects.rowcount()

for i = 1 to li_rowcount
lstr_new.oid=ids_objects.object.oid[i]
lstr_new.superoid=ids_objects.object.superoid[i]
lstr_new.olevel=ids_objects.object.olevel[i]
lstr_new.oname=ids_objects.object.oname[i]
lstr_new.osort=ids_objects.object.osort[i]
lstr_new.otext=ids_objects.object.otext[i]
lstr_new.otype=ids_objects.object.otype[i]

ltvi_child.Label = ids_objects.object.otext[i]
ltvi_child.Data = lstr_new
ltvi_child.PictureIndex = lstr_new.otype
ltvi_child.SelectedPictureIndex = ltvi_child.PictureIndex

ltvi_child.children=true
This.InsertItemLast(handle, ltvi_child)
next
ids_objects.setfilter( "")
ids_objects.filter()

附1

global type str_objects from structure
longoid
longsuperoid
longolevel
longosort
stringotext
stringoname
longotype
end type

附2

窗口左边的树是根据下面的数据窗口创建,数据窗口的数据由指定的menu生成的

窗口右边是提供维护用的,数据窗口中的数据生成后可以参照,右侧的树进行更改等

菜单 转成 树结构 示例1(权限设置1)

左边的树与窗口可以实现一对一焦点自动对应,如选中树上的节点即可定位datawindow的对应行,反之亦然

右侧树选中相应节点可以在下面窗口中显示出相应代码

菜单 转成 树结构 示例1(权限设置1)

各个控件单独滚动截图效果:

菜单 转成 树结构 示例1(权限设置1)

菜单 转成 树结构 示例1(权限设置1)

菜单 转成 树结构 示例1(权限设置1)

菜单 转成 树结构 示例1(权限设置1)

个别的太长了没全部传上来