当前位置: 首页 > 工具软件 > dhtmlxTree > 使用案例 >

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

郎德馨
2023-12-01

先看代码:

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

 类似资料: