extjs4 java_extjs4 Accordion布局
項(xiàng)目結(jié)構(gòu):
實(shí)現(xiàn)效果:
index.jsp代碼:
Accordion布局菜單href="/Accordion/ext4/resources/css/ext-all.css" />
.icon-accordion{
background-image: url(/Accordion/images/accordion.gif) !important;
}
.icon-panel{
background-image: url(/Accordion/images/panel.png) !important;
}
var ajax = function(config) { //封裝、簡化AJAX
Ext.Ajax.request({
url : config.url, //請(qǐng)求地址
params : config.params, //請(qǐng)求參數(shù)
method : 'post', //方法
callback : function(options, success, response) {
config.callback(Ext.JSON.decode(response.responseText));
//調(diào)用回調(diào)函數(shù)
}
});
return false;
};
Ext.onReady(function() {
var win = Ext.create("Ext.window.Window", {
title : "Accordion布局動(dòng)態(tài)菜單",
width : 300,
height : 500,
iconCls : "icon-accordion",
autoScroll : false,
layout : 'accordion',
layoutConfig : {
animate : true
}
});
win.show();
ajax({
url : "/Accordion/accordion",//獲取面板的地址
params : {
action : "list"
},
callback : addTree
});
function addTree(data){
for ( var i = 0; i < data.length; i++) {
win.add(Ext.create("Ext.tree.Panel", {
title : data[i].title,
iconCls : data[i].iconCls,
autoScroll : true,
rootVisible : false,
viewConfig : {
loadingText : "正在加載..."
},
store : createStore(data[i].id),
listeners : {
afterlayout : function(){
if(this.getView().el){
var el = this.getView().el;
var table = el.down("table.x-grid-table");
if(table){
table.setWidth(el.getWidth());
}
}
}
}
}));
win.doLayout();
}
}
var model = Ext.define("TreeModel", { //定義樹節(jié)點(diǎn)數(shù)據(jù)模型
extend : "Ext.data.Model",
fields : [ {name : "id",type : "string"},
{name : "text",type : "string"},
{name : "iconCls",type : "string"},
{name : "leaf",type : "boolean"}
]
});
var createStore = function(id){ //創(chuàng)建樹面板數(shù)據(jù)源
var me = this;
return Ext.create("Ext.data.TreeStore",{
defaultRootId : id, //默認(rèn)的根節(jié)點(diǎn)id
model : model,
proxy : {
type : "ajax", //獲取方式
url : "/Accordion/accordion?action=node" //獲取樹節(jié)點(diǎn)的地址
},
clearOnLoad : true,
nodeParam : "id"//設(shè)置傳遞給后臺(tái)的參數(shù)名,值是樹節(jié)點(diǎn)的id屬性
});
};
});
AccordionServlet代碼如下:
package servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
@SuppressWarnings("serial")
public class AccordionServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String action = request.getParameter("action");
initHeader(response);
if(action.equals("list")){//獲取屬面板列表
renderText(this.getTreePanelList(), response);
}else if(action.equals("node")){
renderText(this.getTreeNodeList(request.getParameter("id")), response);
}
}
public String getTreeNodeList(String id){
JSONArray array = new JSONArray();
for (int j = 0; j < 5; j++) {
JSONObject json = new JSONObject();
json.element("id", id + "-" +(j+1));
json.element("text", "樹節(jié)點(diǎn)-"+ id + "-" +(j+1));
if((j+1) % 2 == 0 && id.length() <= 3){
json.element("leaf", false);
}else{
json.element("leaf", true);
}
array.add(json);
}
return array.toString();
}
public String getTreePanelList(){
JSONArray array = new JSONArray();
for (int i = 0; i < 5; i++) { //生成5個(gè)屬面板
JSONObject json = new JSONObject();
json.element("id", i+1);
json.element("iconCls", "icon-panel");
json.element("title", "Accordion菜單"+(i+1));
array.add(json);
}
return array.toString();
}
public static void renderText(final String content,HttpServletResponse response){
try{
response = initHeader(response);
response.getWriter().write(content);
response.getWriter().close();
}catch(Exception e){
e.printStackTrace();
}
}
private static HttpServletResponse initHeader(HttpServletResponse response){
response.setHeader("Pragma", "No-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
response.setCharacterEncoding("UTF-8");
return response;
}
}
web.xml代碼如下:
This is the description of my J2EE component
This is the display name of my J2EE component
AccordionServlet
servlet.AccordionServlet
AccordionServlet
/accordion
index.jsp
總結(jié)
以上是生活随笔為你收集整理的extjs4 java_extjs4 Accordion布局的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java正则表达式空行_正则表达式删除空
- 下一篇: python写界面进度条程序_Pytho