TA的每日心情 | 衰 2021-2-2 11:21 |
---|
签到天数: 36 天 [LV.5]常住居民I
|
js方法:- //点击Ztree 实践 发送请求 2 x$ _7 U6 a. L- b# \
- function querZtree(){
9 ?# O* k2 i' k1 `7 ^ - $.ajax({
4 t' @6 R, v5 K- ^% q; O - url:'baseInfoAction_queryZtree.action',, E7 ~9 _, Z& m. {) y+ ^) X! @
- async:false,; X6 q3 V2 h0 r5 m! @6 H& \9 ?
- data:{'time':(new Date()).toString()},
1 I! o. b# L! l8 v& Y3 m" \ - success:function(result){; Q. e' o% h. D' F6 I
- var array = eval(result);
0 o! g, W. W4 \# \; I, y( G - var setting = { 3 i1 b% n8 D' Z& R
- view: { # P I$ A5 A+ w
- showIcon: false,
. m0 Z# J+ i6 g* n* `' y3 `- K3 ? - showLine: false ' n3 y z0 v9 p4 p9 L
- },
7 G* h( F, k, g( v i. y& q3 g - callback : {2 e1 i2 I: L& I) H, D
- onClick: zTreeOnClick//回调方法 用户点击 传参数9 p. l2 c9 E% _" X
- }
7 B% C8 G, U+ x/ r0 a - };* W/ G6 V6 U. u) D' Q
- $.fn.zTree.init($("#treeBox"), setting, array);//把数据放到 treeBox内
3 P4 h7 P$ q4 A7 G - }
/ B' H: H9 d" U, @6 y. O4 F4 M - });: k6 C6 {3 W% O$ b8 v
- }
复制代码 Ztree 回调方法:- //点击知识点 获取对象 ; `8 X1 a) y% O2 M; m
- function zTreeOnClick(event, treeId, treeNode){
7 `# n( d, w/ t - alert(treeNode.name)
复制代码
9 {3 a- H3 [# u/ Q' G3 ]. [& hstruts.xml 配置文件:- u- I- |9 `/ w( D# s
- <action name="baseInfoAction_*" method="{1}" class="com.53itstyle.web.baseInfo.action.BaseInfoAction">$ s* q8 ~9 F7 w8 w$ }: y
- <result name="knowledge"></result># }1 b0 ~# j* M) l, W/ X c
- </action>
复制代码 Action 后台方法:
v$ T: i' y" S3 _6 c- public void queryZtree(){
# P- V5 x( Q* K/ X2 Z - try {
2 J Y1 B& c$ o5 y- J - JSONArray resultArray = new JSONArray();& E2 ?; q1 o) H5 l. f) G8 u
- for(int i=0;i<4;i++){0 Z$ U% ]% R8 ?4 P) t2 A
- JSONObject jsonObject = new JSONObject();
% D* o: P4 g5 s2 H+ t - jsonObject.put("name", "一级"+i);
- M; q2 B( [ d, o# ?% S1 h - jsonObject.put("open", true);# F5 i4 y: Y3 u/ {/ {- G1 t
- JSONArray typeArr = new JSONArray();
' L( ]. z! E8 d2 F7 O- M - for(int j=0;j<4;j++){
$ K4 V+ ^) ~; n8 i8 I - JSONObject knObj = new JSONObject(); r6 h9 C! X3 Z; b
- knObj.put("name", "二级"+j);
: s$ R( G: g5 }9 o6 ~( w/ C3 Q: x - knObj.put("open", true);
5 \3 o7 R) C6 c0 I5 A* d8 g( O - typeArr.put(knObj);
+ C% Z* k d D2 s* ^, p: V - jsonObject.put("children", typeArr);
1 _$ l9 E6 P% @/ q% z; ?; `. Q1 ` - }! u7 E0 h2 s6 M+ H. E
- resultArray.put(jsonObject);
' n/ \4 p: z. _5 u) Y8 d - }. B8 n2 ^) b8 \ F- v* L
- printMsgToClient(resultArray.toString());
6 g7 P& M; D* ]% c# s$ e - } catch (Exception e) {, w3 k4 g+ G; w- n& f' Z6 l
- e.printStackTrace();& v: p- B9 R- S
- }7 V! c, S$ i! s1 w7 e! m
- }
复制代码 将结果返回给xmlRequest:$ R* J" m L! m p" x y
- /**
* x2 p' w. r2 @# l - * 将结果返回给xmlRequest# [! x, z1 ` S+ k0 v
- * 4 D8 b' M/ i8 T/ J3 h$ E! ?
- * @param s
9 p5 J8 O! a! L4 e, m. ` - * @param dictList
* e5 ^" ~) y, ]6 ?0 ~ - * @return
j. L3 e6 w, K' y - * @throws Exception
% ?$ I) S* d8 S9 ?# n - */
. D6 n& K; M5 D6 W; O6 ?: m% Y- D - public void printMsgToClient(String result) throws Exception {
& \9 X* U& j s" B! Z - getResponse().setCharacterEncoding("UTF-8");+ E" t% p" P" R& R
- PrintWriter out = getResponse().getWriter();* [7 e, A. I( a n
- try {8 k8 M% ~+ f) b g: `
- out.print(result);
8 W1 D. ~8 j% a: h/ z6 p0 M - }catch (Exception e) {. l2 K, d. m8 X
- e.printStackTrace();
% V9 @; g, A& o5 F' J - } finally {
( ]- P/ G+ F m) S7 O6 T U - out.close();
- p# l9 s/ X9 \" F - }, F( k3 U( B/ M* M9 f, q
- }
复制代码 7 x' h/ g! F4 c& S
2 _' N! u# A# U4 U# Q4 t f; L/ Y% L/ Z$ J$ r, ?
' L5 v9 _( {6 J( T* _; S
! x1 q: d) `# i8 i' M9 {4 S. D
|
|