踩 jstree的简单应用 如果有开发中用的朋友可以交流一下~~
参见 http://www.jstree.com/
标签: jstree 代码片段(1)
[代码] [JavaScript]代码
view sourceprint?
001 jstree主要是看后台如何组织数据
002
003 java类:
004
005 public class TreeAction extends BaseAction implements IAuthIdGetter, IOperationLog {
006
007 /**
008 * 序列号
009 */
010 private static final long serialVersionUID = 1L;
011
012 /**
013 * 数据list
014 */
015 private List<TreeDomain> treeList = new ArrayList<TreeDomain>();
016
017 /**
018 * 日志相关
019 */
020 private static LogService logger = LogService.getLogger(OneCMDBTreeAction.class);
021
022 /**
023 * 获取展示树
024 * @return String
025 */
026 public String showTree() {
027 logger.info(getText("function.title") + getText("log.showTree.begin"));
028 this.parseXml();
029 logger.info(getText("function.title") + getText("log.showTree.end"));
030 return SUCCESS;
031 }
032
033 /**
034 * @return
035 */
036 public void parseXml() {
037 try {
038 CMDBUtil cmdb = new CMDBUtil();
039 // 创建DOM工厂
040 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
041 DocumentBuilder db = dbf.newDocumentBuilder();
042 String treeXml = cmdb.getXML();// getXML();
043 logger.info(treeXml);
044 byte source[] = treeXml.getBytes("UTF-8");
045 InputStream is = new ByteArrayInputStream(source);
046 Document document = db.parse(is);
047 // 显示的节点id
048 String alias = "";
049 // 树节点要展示的名称
050 String displayName = "";
051 // 获取所有名称为template的节点
052 NodeList nodeIdList = document.getElementsByTagName("template");
053 for (int i = 0; i < nodeIdList.getLength(); i++) {
054 Node node = nodeIdList.item(i);
055 // 获取节点要显示的id
056 alias = node.getAttributes().getNamedItem("alias").getNodeValue();
057 displayName = node.getAttributes().getNamedItem("displayName").getNodeValue();
058 if ("Ci".equals(alias)) {
059 TreeDomain domain = new TreeDomain();
060 domain.setDisplayName(displayName);
061 domain.setShowId(alias);
062 domain.setParentId("0");
063 treeList.add(domain);
064 //调用递归算法
065 findList(alias, nodeIdList);
066 }
067 }
068 } catch (Exception e) {
069 // TODO Auto-generated catch block
070 logger.info("parseXml error :" + e);
071 }
072 }
073
074 /**
075 * 递归获取Ci节点下的所有子节点
076 * @param id "Ci"
077 * @param nodeIdList
078 */
079 public void findList(String id, NodeList nodeIdList) {
080 for (int i = 0; i < nodeIdList.getLength(); i++) {
081 Node node = nodeIdList.item(i);
082 Node derivedNode = node.getFirstChild().getNextSibling();
083 if ("derivedFrom".equals(derivedNode.getNodeName())) {
084 // 当前node的id
085 String alias = node.getAttributes().getNamedItem("alias").getNodeValue();
086 // 当前node的显示名称
087 String displayName = node.getAttributes().getNamedItem("displayName")
088 .getNodeValue();
089 Node refNode = derivedNode.getFirstChild().getNextSibling();
090 if (refNode.getAttributes().getNamedItem("alias") != null) {
091 String pid = refNode.getAttributes().getNamedItem("alias").getNodeValue();
092 if (id.equals(pid)) {
093 TreeDomain domain = new TreeDomain();
094 domain.setDisplayName(displayName);
095 domain.setShowId(alias);
096 domain.setParentId(pid);
097 treeList.add(domain);
098 findList(alias, nodeIdList);
099 }// end if id
100 }// end if refNode
101 }// end if
102 }// end for i
103 }
104
105 /**
106 * 读取树的内容
107 * @return
108 */
109 public String getXML() {
110 String xml = "";
111 ByteArrayOutputStream out = new ByteArrayOutputStream();
112 InputStream is = null;
113 try {
114 is = new FileInputStream("C:\\cloud\\template.xml");
115 byte[] b = new byte[1024];
116 int n;
117 while ((n = is.read(b)) != -1) {
118 out.write(b, 0, n);
119 }// end while
120 byte treebyte[] = out.toByteArray();
121 String str = new String(treebyte, "UTF-8");
122 xml = str;
123 // logger.info(xml);
124 } catch (Exception e) {
125 // TODO Auto-generated catch block
126 e.printStackTrace();
127 } finally {
128 if (is != null) {
129 try {
130 is.close();
131 } catch (Exception e) {
132 // log.error(e);// TODO
133 }// end try
134 }// end if
135 if (out != null) {
136 try {
137 out.close();
138 } catch (Exception e) {
139 // log.error(e);// TODO
140 }// end try
141 }// end if
142 }
143 return xml;
144 }
145
146 @Override
147 public String getOpType() {
148 // TODO Auto-generated method stub
149 return null;
150 }
151
152 @Override
153 public String getOperationFunction() {
154 // TODO Auto-generated method stub
155 return null;
156 }
157
158 @Override
159 public String getOperationInfo() {
160 // TODO Auto-generated method stub
161 return null;
162 }
163
164 public List<TreeDomain> getTreeList() {
165 return treeList;
166 }
167
168 public void setTreeList(List<TreeDomain> treeList) {
169 this.treeList = treeList;
170 }
171 }
172
173 <script type="text/JavaScript">
174 <!--
175 var iconCss = "background: url(././././themes/default/images/ico_file.png;) no-repeat scroll center center transparent;";
176 var iconUrl = "././././themes/default/images/ico_file.png ";
177 $(function(){
178 $("#resourceTree").jstree({
179 "xml_data" : {
180 "ajax" : {
181 "url" : "showTree.action"
182 },
183 "xsl" : "flat"
184 },
185 "ui" : {
186 "initially_select" : [ "NJCMP_Pm_info_tab" ]
187 },
188 "plugins" : [ "themes", "xml_data","ui"]
189 })
190 .bind("open_node.jstree",function (e,data){
191 var data = $(data.rslt.obj);
192 var $parentUL = $("ul",data);
193 var $content = $("ul",$parentUL);
194 if($content.html() == null ){
195 $("a",$parentUL).attr("icon",iconUrl);
196 $("ins:odd",$parentUL).attr("style",iconCss);
197 }
198 })
199 .bind("select_node.jstree", function (event, data){
200 var id = data.rslt.obj.attr("id");
201 var data = $(data.rslt.obj);
202 var $parentUL = $("ul",data);
203 var $content = $("ul",$parentUL);
204 if($content.html() == null ){
205 url = "<%=request.getContextPath()%>/getbase.action?CIName=" + id;
206 $("#framezc").attr("src",url);
207 }
208 })
209 .bind("loaded.jstree", function (e, data){
210 $("#resourceTree").css("background-color","#f4f6f8");
211 $("#resourceTree").css("overflow","auto");
212 $("#resourceTree").css("height","700px");
213 $("#resourceTree").jstree("toggle_node","#NJCMP_PM");
214 url = "<%=request.getContextPath()%>/getbase.action?CIName='NJCMP_Pm_info_tab'";
215 $("#framezc").attr("src",url);
216 });
217 });
218 //-->
219 </script>
220
221 <div id="resourceTree"></div>
de]
jstree后台和前台
最新推荐文章于 2022-03-02 02:46:03 发布