Java框架SSH結(jié)合Easyui控件實(shí)現(xiàn)省市縣三級(jí)聯(lián)動(dòng)示例解析
Easyui調(diào)用數(shù)據(jù)庫(kù)實(shí)現(xiàn)省市縣區(qū)三級(jí)聯(lián)動(dòng)的效果如果下



1、首先要設(shè)計(jì)數(shù)據(jù)庫(kù),如圖所示。一個(gè)有4個(gè)字段code,note,pycode。code:行政區(qū)劃代碼,note:中文注釋,pycode:拼音縮寫(xiě)。 其中code是由6個(gè)字段組成。如果是省級(jí)最后4位是0000,如果是地級(jí)市最后2位是00,其他是縣區(qū)。
我已經(jīng)把相關(guān)數(shù)據(jù)庫(kù)代碼上傳到我的csdn資源中,需要的同學(xué)自行下載。

2、我用的是java、SSH框架結(jié)合Easyui控件
3、html代碼如下
<tr>
<td class="left">省:</td>
<td><input name="contact.province" id="province" style="width: 174px;" ></td>
<td class="left">市:</td>
<td><input name="contact.city" id="city" style="width: 174px;"></td>
<td class="left">縣區(qū):</td>
<td><input name="contact.county" id="county" style="width: 174px;" ></td>
</tr>
4、對(duì)應(yīng)的JS代碼如下
$(function(){
// 下拉框選擇控件,下拉框的內(nèi)容是動(dòng)態(tài)查詢數(shù)據(jù)庫(kù)信息
$('#province').combobox({
url:'apply/provinceCombobox_combobox.action',
editable:false, //不可編輯狀態(tài)
cache: false,
// panelHeight: 'auto',//自動(dòng)高度適合
valueField:'code',
textField:'note',
onHidePanel: function(){
$("#city").combobox("setValue",'');
$("#county").combobox("setValue",'');
$("#cregicounty").val('');
var province = $('#province').combobox('getValue');
if(province!=''){
$.ajax({
type: "POST",
url: "apply/cityCombobox_combobox.action?province="+province,
cache: false,
dataType : "json",
success: function(data){
$("#city").combobox("loadData",data);
}
});
}
}
});
$('#city').combobox({
editable:false, //不可編輯狀態(tài)
cache: false,
//panelHeight: 'auto',//自動(dòng)高度適合
valueField:'code',
textField:'note',
onHidePanel: function(){
$("#cregicounty").val('');
$("#county").combobox("setValue",'');
var city = $('#city').combobox('getValue');
if(city!=''){
$.ajax({
type: "POST",
url: "apply/countyCombobox_combobox.action?city="+city,
cache: false,
dataType : "json",
success: function(data){
$("#county").combobox("loadData",data);
}
});
}
}
});
$('#county').combobox({
editable:false, //不可編輯狀態(tài)
cache: false,
// panelHeight: 'auto',//自動(dòng)高度適合
valueField:'code',
textField:'note',
onHidePanel: function(){
var str=$('#county').combobox('getText');
$("#cregicounty").val(str);
}
});
$('#country').combobox({//國(guó)家代碼初始化
valueField:'english',
textField:'note',
url:'json/country.json',
cache: false,
//panelHeight: 'auto',//自動(dòng)高度適合
onChange: function(newValue,oldValue){
countrySearch(newValue);
countrys(newValue);
}
});
});
5、Java的Action代碼
//查詢?nèi)珖?guó)行政區(qū)代碼省
public String provinceCombobox() throws Exception{
List list=comboboxService.findProvince();
this.jsonUtil(list);
return null;
}
//查詢?nèi)珖?guó)行政區(qū)代碼市
public String cityCombobox() throws Exception{
List list=comboboxService.findCity(province);
this.jsonUtil(list);
return null;
}
//查詢?nèi)珖?guó)行政區(qū)代碼縣區(qū)
public String countyCombobox() throws Exception{
List list=comboboxService.findCounty(city);
this.jsonUtil(list);
return null;
}
//調(diào)用json工具方法,傳入?yún)?shù)alist
public void jsonUtil(Object accountlist) throws Exception{
HttpServletResponse response = ServletActionContext.getResponse();
log.info("JSON格式:" + accountlist.toString());
String returnJson = JsonConvert.returnJson(accountlist);
response.setCharacterEncoding("utf-8");
response.getWriter().println(returnJson);
}
6、工具JSON代碼
import java.io.StringWriter;
import org.codehaus.jackson.map.ObjectMapper;
public class JsonConvert {
static String jsonStr;
public static String returnJson(Object object) throws Exception{
ObjectMapper objectMapper = new ObjectMapper();
StringWriter stringWriter = new StringWriter();
objectMapper.writeValue(stringWriter, object);
jsonStr = stringWriter.toString();
return jsonStr;
}
}
7、對(duì)應(yīng)接口代碼
//查詢省 public List findProvince() throws Exception; //查詢市 public List findCity(String code) throws Exception; //查詢縣區(qū) public List findCounty(String code) throws Exception;
8、對(duì)應(yīng)接口實(shí)現(xiàn)類(lèi)代碼
//下拉框--查詢省
public List findProvince() {
log.info("===下拉框--查詢省");
Criteria criteria=this.sessionFactory.getCurrentSession().createCriteria(CareacodeTblQg.class);
criteria.add(Restrictions.like("code", "%0000"));
criteria.addOrder(Order.asc("code"));
return criteria.list();
}
//下拉框--查詢市
public List findCity(String code2) {
log.info("===下拉框--查詢市");
String id=code2.substring(0,2);
Criteria criteria=this.sessionFactory.getCurrentSession().createCriteria(CareacodeTblQg.class);
criteria.add(Restrictions.like("code", id+"%00"));
criteria.add(Restrictions.ne("code",code2 ));
criteria.addOrder(Order.asc("code"));
return criteria.list();
}
//下拉框--查詢縣區(qū)
public List findCounty(String code3) {
log.info("===下拉框--查詢縣區(qū)");
String id=code3.substring(0,4);
Criteria criteria=this.sessionFactory.getCurrentSession().createCriteria(CareacodeTblQg.class);
criteria.add(Restrictions.like("code", id+"%"));
criteria.add(Restrictions.not(Restrictions.like("code", "%01")));
criteria.add(Restrictions.ne("code",code3 ));
criteria.addOrder(Order.asc("code"));
return criteria.list();
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
jQuery實(shí)現(xiàn)別踩白塊兒網(wǎng)頁(yè)版小游戲
本文主要介紹了jQuery實(shí)現(xiàn)別踩白塊兒網(wǎng)頁(yè)版小游戲的思路分析與代碼。具有一定的參考價(jià)值,下面跟著小編一起來(lái)看下吧2017-01-01
JQuery Highcharts 動(dòng)態(tài)生成圖表的方法
動(dòng)態(tài)圖表生成方法有很多,在接下來(lái)的文章中將為大家介紹下使用JQuery Highcharts是如何實(shí)現(xiàn)的2013-11-11
jquery中EasyUI實(shí)現(xiàn)異步樹(shù)
前面我們分享了使用jquery中EasyUI實(shí)現(xiàn)同步樹(shù)的代碼,本文我們就來(lái)看下使用EasyUI實(shí)現(xiàn)異步樹(shù)的方法和示例,希望小伙伴們能夠喜歡。2015-03-03
jQuery EasyUI tree 使用拖拽時(shí)遇到的錯(cuò)誤小結(jié)
在我使用tree拖拽時(shí)總是失敗,控制臺(tái)輸出了很多錯(cuò)誤。經(jīng)過(guò)問(wèn)題跟蹤分析最終找到的錯(cuò)誤原因,下面小編給大家分享下,感興趣的朋友參考下2016-10-10
jQuery簡(jiǎn)單倒計(jì)時(shí)效果完整示例
這篇文章主要介紹了jQuery簡(jiǎn)單倒計(jì)時(shí)效果,結(jié)合完整實(shí)例形式分析了jQuery針對(duì)日期與時(shí)間的計(jì)算與頁(yè)面元素動(dòng)態(tài)操作相關(guān)技巧,需要的朋友可以參考下2016-09-09

