dhtmlxtree 如何得到xml,json等文件中的自定义的属性值

先看代码:

var TreeForJSON = new dhtmlXTreeObject('TreeForJSON', '100%', '100%', 0); 
TreeForJSON.setImagePath("dhtmlx/codebase/imgs/csh_vista/");
TreeForJSON.enableDragAndDrop(true);
TreeForJSON.enableKeyboardNavigation(true)
TreeForJSON.loadJSONObject({id:0, 
        item:[
            {id:1,text:"first",userdata:[{name:'url',content:'http://g.cn#1'}]},
            {id:2, text:"middle",userdata:[{name:'url',content:'http://g.cn#2'}], 
                item:[
                    {id:"21", text:"child",userdata:[{name:'url',content:'http://g.cn#3'}]}
                ]},
            {id:3,text:"last",userdata:[{name:'url',content:'空'}]}
        ]
    }
);
TreeForJSON.setOnClickHandler(TestOnClick);
function TestOnClick(id){
    var url = (TreeForJSON.getUserData(id, "url"));
    alert(url);
}

dhtmlxTree中的loadJSONObject方法也是支持userdata属性的,只是写法特殊一点
取值时是通过当前ID去找userdatea下的name,如代码中的url,然后获取其content内容,即http://g.cn...
同理,使用这个方法可以自定义更多的属性

userdata:[{name:'type',content:'1'},{name:'remark',content:'空'}]

 

 

dhtmlXTree API

 

Methods

 

assignKeysconfigures the keys used for keyboard navigation
attachEventadds any user-defined handler to available events
changeItemIdchanges the item's id
clearCutcancels marking an item as cut
clearSelectionunselects the item in the tree
closeAllItemscollapses target node and all sub nodes
closeItemcollapses a node
deleteChildItemsdeletes all children of the node
deleteItemdeletes a node
destructordeletes tree and clears memory
detachEventdetaches a handler from an event
disableCheckboxdisables a checkbox
doCutmarks the selected item as cut
doPasteinserts the previously cut branch
editItemopens editor for the specified item
enableActiveImagesenables active images (clickable and dragable). By default only text part of the node is active
enableAutoSavingSelectedenables/disables autosaving selected node in cookies
enableAutoTooltipsenables auto tooltips (node text as tooltip)
enableCheckBoxesshows/hides checkboxes (all checkboxes in the tree)
enableContextMenuenables context menu
enableDistributedParsingenables distributed parsing of a big tree (items loaded portion by portion with some timeouts)
enableDragAndDropenables/disables drag-and-drop
enableDragAndDropScrollingenables/disables auto scrolling during the drag-and-drop operation
enableHighlightingenables item highlighting (item's text is highlighted on mouseover)
enableIEImageFixreplaces IMG tag with background images - solves the problem with IE image caching, doesn't work for IE6 SP1
enableImageDragenables dragging an item by its image (by default an item is dragged by text)
enableItemEditorenables editing of the item's text
enableKeySearchenables searching of items by pressing keys
enableKeyboardNavigationenables keyboard navigation in the tree
enableLoadingItemenables/disables "Loading..." item
enableMercyDragenables dragging without removing (copies instead of moving)
enableMultiLineItemsenables multiline items
enableMultiselectionenables multiselection
enableRTLenables Right-to-Left mode in the tree
enableRadioButtonsreplaces checkboxes with radio buttons
enableSingleRadioModereplaces checkboxes with radio buttons
enableSmartCheckboxesenables smart checkboxes
enableSmartRenderingenables smart rendering mode
enableSmartXMLParsingenables/disables smart XML parsing mode
enableTextSignsreplaces images with text signs
enableThreeStateCheckboxesenables three-state checkboxes
enableTreeImagesenables tree images
enableTreeLinesenables/disables tree lines (parent-child threads)
findItemfinds a tree item by text, selects it and focuses on it
findItemIdByLabelfinds a tree item by text
focusItemscrolls Tree to the indicated item to make it visible and sets focus on it
getAllCheckedreturns the list of ids of the nodes with checked checkboxes, separated by default delimiter
getAllCheckedBranchesreturns the list of ids of the nodes with checked and three-state checkboxes, separated by default delimiter
getAllChildlessreturns the list of all items which don't have child nodes
getAllItemsWithKidsreturns the list of all items which have child nodes, separated by default delimiter
getAllPartiallyCheckedreturns the list of ids of the nodes with three-state checkboxes, separated by default delimiter
getAllSubItemsreturns the list of all children nested on all sublevels of the tree (in relation to the specified item)
getAllUncheckedreturns the list of identificators of the nodes with unchecked checkboxes, separated by default delimiter
getAttributereturns the value of the specified item attribute
getChildItemIdByIndexreturns child node's id by index
getDistributedParsingStategets the current state of distributed parsing
getIndexByIdreturns the node's index in the children collection by id
getItemColorgets the color of the target item
getItemIdByIndexreturns the node's id by index
getItemImagegets URL of the item's image
getItemParsingStategets current parsing state of an item
getItemTextreturns the node's text
getItemTooltipgets item's tooltip
getLevelgets the node's level (position in the hierarchy)
getOpenStatereturns open/close state
getParentIdreturns the parent item's id
getSelectedItemIdreturns the selected item's id
getSelectedItemTextreturns the selected node's text
getSubItemsreturns a comma-delimited list of ids of all children nested on the sublevel in relation to the specified item
getUserDatagets user data from the target node
getXMLStatereturns the current state of XML loading
hasChildrenreturns the number of children
insertNewChildcreates a new node as a child to the specified one with parentId
insertNewItemcreates a new node as a child to the specified one with parentId
insertNewNextcreates a new node next to the specified one
isItemCheckedgets the state of the node's checkbox
isLockedreturns "true" if the item is locked
loadloads data to the component via XML or JSON, usually data-values pairs
loadCSVloads tree from csv file/stream
loadCSVStringloads tree from csv string
loadJSArrayloads tree from js array object
loadJSArrayFileloads tree from js array file/stream
loadJSONloads tree from json file
loadJSONObjectloads dhtmlxTree from a JSON object
loadOpenStatesrestores open nodes from cookie
loadStateloads tree from cookie
loadXMLloads tree from xml file
loadXMLStringloads tree from xml string
lockItemlocks/unlocks an item
lockTreelocks tree
makeAllDraggableadds drag-n-drop capabilities to all HTML items with dragInDhtmlXTree attribute
makeDraggableadds drag-n-drop capabilities (with the possibility to drop into dhtmlxTree) to HTML object
moveItemmoves an item (inside of tree)
openAllItemsexpands target node and all sub-nodes
openAllItemsDynamicexpands target node and all child nodes (the same as openAllItems, but works in dynamic trees)
openItemexpands a node
openItemsDynamicexpands the list of nodes in dynamic tree (wait till the end of a node's loading before expanding the next one)
openOnItemAddedconfigures if parent node will be expanded immediately after a child item has been added
parseloads data from a local datasource (XML string, CSV string, XML island, XML object, JSON object, javascript array)
preventIECachingprevents caching in IE by adding a random value to URL string
refreshItemrefreshes a tree branch from XML
refreshItemsrefreshes the specified tree nodes (gets XML from server and updates only the nodes included into itemIdList)
registerXMLEntityregisters XML entity for replacement during the initialization (the default entities are: ampersand, less-than and greater-than symbols)
restoreSelectedItemrestores the selected item from cookie
saveOpenStatessaves open nodes to cookie
saveSelectedItemsaves the selected item to cookie
saveStatesaves tree to cookie
selectItemselects the specified node (and optionally fires the onClick event)
serializeTreegets xml representation (as string) of tree
serializeTreeToJSONreturns tree as json string
setAttributesets an attribute for an item
setChecksets the state of a node's checkbox
setChildCalcHTMLsets children calculation prefix and postfix
setChildCalcModesets children calculation mode
setCustomSortFunctionsets custom sort function which has two parameters - id_of_item1,id_of_item2
setDataModesets a default data transfer mode
setDragBehaviorsets Drag-And-Drop behavior (child - drop as child, sibling - drop as sibling, complex - complex drop behaviour)
setEditStartActiondefines which events must start the process of editing
setEscapingModesets escaping mode (used for escaping ID in requests)
setIconSizesets the size of icons
setIconsPathdefines path to the icons folder
setImageArrayssets the images used for parent-child threads drawing (lines, plus, minus)
setImagesPathdefines path to the imgs folder
setItemCloseableprevents a node from closing
setItemColorsets the color of the node's text
setItemContextMenusets context menu for individual nodes
setItemImagesets item's icons (mostly useful for childless nodes)
setItemStylesets an individual item's style
setItemTextsets a new node's text (HTML is allowed)
setItemTopOffsetsets the top offset for an item
setListDelimetersets list separator ("," by default)
setLockedIconssets an icon for locked items
setSerializationLevelconfigures XML serialization
setSkinset skin for the component
setStdImagessets default images for nodes (must be called before XML loading)
setSubCheckedchanges the state of the node's checkbox and all children checkboxes
setUserDatasets user data for the target node
setXMLAutoLoadingenables dynamic loading from XML
setXMLAutoLoadingBehaviourdefines the mode of passing to the server the id of an item for which the data should be loaded during dynamic loading
showItemCheckboxshows/hides a checkbox for a tree item
showItemSignshows/hides (+/-) an icon
smartRefreshBranchrefreshes the specified tree branch (gets XML from server, adds new nodes, removes unused nodes)
smartRefreshItemrefreshes the specified tree item (gets XML from server, adds new nodes, removes unused nodes)
sortTreereorders items in the tree according to their text
stopEditswitches the item which is currently being edited back to normal view
updateItemupdates item's properties

 

Events

 

onAllOpenDynamicfires when all sub levels is loaded and opened
onBeforeCheckfires before an item is checked/unchecked
onBeforeContextMenufires before the moment when a context menu appears on the right mouse click
onBeforeDragfires when the item's dragging starts (the item is selected and the mouse is moving)
onCheckfires immediately after an item in the tree has been checked/unchecked
onClickfires when the text part of a tree item is clicked, but after the default onClick functionality has been processed
onDblClickfires right after a tree item has been double clicked, but before the default onDblClick functionality has been processed
onDragfires when the item was dragged and dropped on some other item, but before item's moving has been processed
onDragInfires when the item is dragged over some target the item can be dropped to
onDropfires when drag-and-drop has already been processed; besides, fires when the nodes are moved programmatically
onEditfires on 4 different stages of the editing process (see the details)
onEditCancelevent occurs after cancelling edit by Escape key or other means
onKeyPressfires after each key press when tree is in focus
onMouseInfires when the mouse pointer is hovered over an item
onMouseOutfires when the mouse pointer is moved out of item's area
onOpenDynamicEndfires after the item defined in the openItemsDynamic() method opens
onOpenEndfires right after an item in the tree has become open
onOpenStartfires right when an item in the tree starts to open, but before this item has opened
onRightClickfires when the user clicks the right mouse button
onSelectfires when selection in the tree was changed
onXLEfires when the data loading is finished and a component or data is rendered
onXLSfires when XML loading started

 

转载于:https://www.cnblogs.com/interdrp/p/8987492.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
dhtmlXTree进行一个小的扩展 需求1: 动态生成树形菜单,每个节点都有各自的URL地址,单击不同的节点框架页的右侧跳转到该节点所对应的URL。(框架页说明:左边是树形菜单;右边是显示页面相应信息的页面) 分析: dhtmlXTree提供了很好的添加,删除节点的方法,故选择dhtmlXTree。 但是dhtmlXTree不能满足"每个节点都有各自的URL地址,单击不同的节点框架页的右侧跳转到该节点所对应的URL"这点需求,因次想到了对dhtmlXTree进行一个小的扩展,即在其节点对象原有属性的基础上,再添加两个扩展属性。具体操作如下: 1、找到定义节点对象的那个函数(或方法) function dhtmlXTreeItemObject(itemId,itemText,parentObject,treeObject,actionHandler,mode) 修改为 function dhtmlXTreeItemObject(itemId,itemText,parentObject,treeObject,actionHandler,mode,url,target) 并在方法体添加赋语句:this.itemURL=url;this.itemTarget=target; 2、然后修改所有与dhtmlXTreeItemObject有关(直接或者间接相关)的方法: _attachChildNode,insertNewItem,insertNewChild,insertNewNext,_recreateBranch,_parseXMLTree 注:_parseXMLTree方法是与loadXML,loadXMLString相关的。 在这些方法生成节点的语句添加相应的参数和语句,以支持新添加的属性itemURL,itemTarget。 需求2: 为dhtmlXTree树上的每一个节点添加右键菜单。附:在树上的节点上点右键时才会生成菜单,空白区域单击时不会生成菜单。 分析: 1、用 dhtmlXTree + dhtmlxmenu 实现。 2、 用dhtmlxmenu生成菜单的部分代码: var menu = new dhtmlXMenuObject(); menu.setImagePath("imgs/"); menu.setIconsPath("images/"); menu.renderAsContextMenu(); menu.loadXML("dhtmlxmenu.xml?e="+new Date().getTime()); menu.addContextZone("treeboxbox_tree"); menu.addContextZone方法是为了把菜单添加到指定区域。 3、dhtmlXTreeObject.prototype._createItem方法是构造树形菜单上元素的具体实现方法。看这个方法的具体操作,可以发现它为每一个节点构建了一个table,节点的内容(即名字)放置在一个span。 4、考虑到dhtmlxmenu实在指定的区域构建菜单,所以可以为dhtmlXTree树上的每一个节点添加右键事件,在这个右键事件里获得该节点对象所对应的区域,然后在这个区域内构建Menu菜单。 难点和解决方案: 1、怎样获得dhtmlXTree树上的每一个节点对象所对应的区域?(dhtmlXTreeObject.prototype._createItem方法没有为这个节点的span设置id) 解决方法: 在dhtmlXTreeObject.prototype._createItem方法添加一个为span设置id的语句: 即: 在itemObject.span=document.createElement('span'); itemObject.span.className="standartTreeRow"; 后,新添加一句 itemObject.span.id="treeNode_"+itemObject.id;//为这个span新增一个Id属性 2、为dhtmlXTree树上的每一个节点添加右键事件,在这个右键事件里获得该节点对象所对应的区域,然后在这个区域内构建Menu菜单。 解决方法: a、为dhtmlXTree树上的每一个节点添加右键事件: tree.setOnRightClickHandler(treeOnRegihtClick);//右键事件 b、构建Menu菜单: var menu = new dhtmlXMenuObject(); function treeOnRegihtClick(id){ alert("右键 "+" span.id:"+tree.getItem(id).span.id); menu.setImagePath("imgs/"); menu.setIconsPath("images/"); menu.renderAsContextMenu(); menu.loadXML("dhtmlxmenu.xml?e="+new Date().getTime()); menu.addContextZone(tree.getItem(id).span.id);alert("width:"+tree.getItem(id).span.clientWidth); //var X=tree.getItem(id).span.getBoundingClientRect().left; //var Y=tree.getItem(id).span.getBoundingClientRect().top; var X=document.getElementById('mouseXPosition').value;//获得鼠标的横坐标位置 var Y=document.getElementById('mouseYPosition').value;//获得鼠标的纵坐标位置 menu.showContextMenu(X,Y);//调用showContextMenu方法显示菜单 说明:如果这儿不加上这条语句的话,第一次点击右键时只能生成菜单,但是显示不出菜单,下次点击右键Menu菜单才能弹出。 //menu._showContextMenu(X,Y,tree.getItem(id).span.id); } c、用 javascript 获取当页面上鼠标(光标)位置 <script type="text/javascript"> // 说明:获取鼠标位置 function mousePosition(ev){ if(ev.pageX || ev.pageY){ return {x:ev.pageX, y:ev.pageY}; } return { x:ev.clientX + document.body.scrollLeft - document.body.clientLeft, y:ev.clientY + document.body.scrollTop - document.body.clientTop }; } document.onmousemove = mouseMove; function mouseMove(ev){ ev = ev || window.event; var mousePos = mousePosition(ev); document.getElementById('mouseXPosition').value = mousePos.x; document.getElementById('mouseYPosition').value = mousePos.y; } </script> 页面上放置两个隐藏域存放鼠标的位置:<input type="hidden" id=mouseXPosition><input type="hidden" id=mouseYPosition>

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值