執行個體:Easyui的combobox實現動態資料級聯

來源:互聯網
上載者:User

package crj.portal.web.management.action;import java.io.IOException;import java.util.List;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import net.sf.json.JSONArray;import net.sf.json.JSONObject;import net.sf.json.JsonConfig;import org.apache.log4j.Logger;import org.apache.struts2.ServletActionContext;import org.hibernate.Criteria;import org.hibernate.criterion.Order;import com.sun.tools.javac.util.Log;import crj.portal.web.management.model.Cpersontypetbl;import crj.portal.web.management.service.CategoryService;import crj.portal.web.management.service.ItemService;import crj.portal.web.management.service.UserService;public class ItemManageAction  {Logger log=Logger.getLogger(this.getClass());private String page;private String rows;private String lanmuid;private ItemService itemService;// 依賴注入//下拉框--查詢欄目public String categorytbl() throws Exception{List list=itemService.queryLanMu();this.toJsonArray(list);return null;}//根據欄目的ID 查詢擁有的類別public String leibie() throws Exception{List list=itemService.queryLeiBie(lanmuid);this.toJsonArray(list);return null;}public String toJsonArray(List list) throws IOException{HttpServletResponse response = ServletActionContext.getResponse();HttpServletRequest request = ServletActionContext.getRequest();JSONArray json = JSONArray.fromObject(list);log.info("JSON格式:" +json.toString());         response.setCharacterEncoding("utf-8");// 指定為utf-8    response.getWriter().write(json.toString());// 轉化為JSOn格式return null;}public String save() throws Exception {return this.alllist();}public ItemService getItemService() {return itemService;}public void setItemService(ItemService itemService) {this.itemService = itemService;}public String getPage() {return page;}public void setPage(String page) {this.page = page;}public String getRows() {return rows;}public void setRows(String rows) {this.rows = rows;}public UserService getUserService() {return userService;}public void setUserService(UserService userService) {this.userService = userService;}public CategoryService getCategoryService() {return categoryService;}public void setCategoryService(CategoryService categoryService) {this.categoryService = categoryService;}public String getLanmuid() {return lanmuid;}public void setLanmuid(String lanmuid) {this.lanmuid = lanmuid;}}

5、對應的介面代碼

public interface ItemService {//下拉框--查詢欄目public List queryLanMu() throws Exception;//下拉框--查詢類別public List queryLeiBie(String ids) throws Exception;}

6、對應的介面實作類別代碼

public class ItemServiceImpl implements ItemService {Logger log = Logger.getLogger(this.getClass());private SessionFactory sessionFactory;//下拉框--查詢欄目public List queryLanMu() throws Exception {Criteria criteria=this.sessionFactory.getCurrentSession().createCriteria(Categorytbl.class);criteria.add(Restrictions.eq("parentid", 0));criteria.addOrder(Order.asc("categoryid"));return criteria.list();}//下拉框--查詢類別public List queryLeiBie(String ids) throws Exception {int i=Integer.parseInt(ids);Criteria criteria=this.sessionFactory.getCurrentSession().createCriteria(Categorytbl.class);criteria.add(Restrictions.eq("parentid", i));criteria.addOrder(Order.asc("categoryid"));return criteria.list();}public SessionFactory getSessionFactory() {return sessionFactory;}public void setSessionFactory(SessionFactory sessionFactory) {this.sessionFactory = sessionFactory;}}

7、對應的JSP代碼

<%@ page language="java" errorPage="/error.jsp" pageEncoding="utf-8"%><%@ taglib prefix="s" uri="/struts-tags"%><%String path = request.getContextPath();%><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><title>資訊管理</title><!-- 引入Jquery --><script type="text/javascript" src="<%=path%>/easyui_1.3.2/jquery-1.8.0.min.js" charset="utf-8"></script><!-- 引入Jquery_easyui --><script type="text/javascript"src="<%=path%>/easyui_1.3.2/jquery.easyui.min.js" charset="utf-8"></script><!-- 引入easyUi國際化--中文 --><script type="text/javascript"src="<%=path%>/easyui_1.3.2/locale/easyui-lang-zh_CN.js"charset="utf-8"></script><!-- 引入easyUi預設的CSS格式--藍色 --><link rel="stylesheet" type="text/css"href="<%=path%>/easyui_1.3.2/themes/default/easyui.css" /><!-- 引入easyUi小表徵圖 --><link rel="stylesheet" type="text/css"href="<%=path%>/easyui_1.3.2/themes/icon.css" /><script type="text/javascript">    /* 初始化下載表格資訊 */$(function() {  // 下拉框選擇控制項,下拉框的內容是動態查詢資料庫資訊$('#lanmu').combobox({     url:'itemManage!categorytbl',     editable:false, //不可編輯狀態    cache: false,    panelHeight: 'auto',//自動高度適合    valueField:'categoryid',       textField:'categoryName',    onHidePanel: function(){    $("#leibie").combobox("setValue",'');var lanmuid = $('#lanmu').combobox('getValue');$.ajax({type: "POST",url: "itemManage!leibie?lanmuid="+lanmuid,cache: false,dataType : "json",success: function(data){$("#leibie").combobox("loadData",data);   }}); }           }); $('#leibie').combobox({     //url:'itemManage!categorytbl',     editable:false, //不可編輯狀態    cache: false,    panelHeight: 'auto',//自動高度適合    valueField:'categoryid',       textField:'categoryName'   });});</script></head><body><!-- 工具攔 --><div id="tb" style="padding: 3px">  <form ><span>所屬欄目:</span> <select  id="lanmu" class="lanmu" style="width:160px; border: 1px solid #ccc"> </select><span>類別:</span> <select  id="leibie" class="leibie" style="width:160px; border: 1px solid #ccc"> </select>  </form></div></body></html>


相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.