- 1、java三級級聯問題 主要幫我分析一下如何從資料庫中取值 和分析一下這個js代碼
- 2、java三級級聯問題
- 3、NetBeans IDE 6.8 JAVA Swing控制項三級聯動下拉列表怎麼寫?
- 4、三級級聯是什麼意思
- 5、三級聯動怎麼回顯 javaweb
在你選擇下拉框的時,先執行function ddl_Clear(ddl_name)這個方法,清空name=”big”所有項。
然後清空littl中所有項
之後獲取數據填充littl;
看了你的代碼,你的數據源,也就是下拉框的值應該不是這段代碼里拿的,但你JS肯定是取到了。第1個JS中根據bindData去構造出第1個下拉框。其中判斷了i是不是0,這裡我沒看懂,是不是你自己修改了代碼,因為if(i ==0)的else里兩段代碼一樣的。(我的理解是如果為0,插入1條空選項或『全部』選項)。循環結束後,第1個方法設置了第0位的值為默認的選項,即表示第1個下拉框默認選成第1項,再後來調用下面的JS去聯動構造第2個下拉框。
第2個下拉框首先接收參數,然後先清空所有的option,然後根據上面方法傳下來的參數ddl_data,去構造第2個下拉框,道理同1.
看了下keywords的意思,大概是你的數據源把所有數據都取得了,這裡根據keywords去篩選。。。這還不是傳統意義的聯動到後台取數據再重新構造。 而是籠統的取得所有數據,加上keywors去判斷要不要把對應的值塞進下拉框內
還有事這個地方報錯么?越看這段越像只是在初始化,難道初始化的時候就報錯,還不是下拉改變時候才報?
用else if不行啊,資料庫里如果「系部」有30幾個,難道你一個個寫過去嗎?可以用for循環,把系部加入第一個列表然後 對該列表添加監聽器 addActionListener 通過該列表.getSelectedItem得到選中的項 然後專業可以寫一個類。裡面有一個屬性是「系部」 當那些個專業的系部屬性為 第一個列表中的值的時候把那些專業的「專業名」屬性加入第二個列表中 依次類推我給你一個關於省份,城市,和營業網點的3級下拉列表的例子,你稍微修改就可以了
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel; @SuppressWarnings(“serial”)
public class Test extends JFrame{
private ListString provinces=new ArrayListString();
private ListCity citys=new ArrayListCity();
private ListBranch branchs=new ArrayListBranch();
private JComboBox provinceBox=new JComboBox();
private JComboBox cityBox=new JComboBox();
private JComboBox branchBox=new JComboBox();
public Test() {
init();
initData();
setBox();
addHandler();
}
public void init(){//初始化GUI界面
JPanel panel=new JPanel();
this.add(panel);
//panel.setLayout(new GridLayout(3,2));
panel.add(new JLabel(“省份:”));
panel.add(provinceBox);
panel.add(new JLabel(“城市:”));
panel.add(cityBox);
panel.add(new JLabel(“營業網點”));
panel.add(branchBox);
}
private void initData(){//初始化省份 城市和營業網點 這些數據實際上應該從資料庫里查詢
provinces.add(“浙江”);
provinces.add(“廣西”);
citys.add(new City(“浙江”,”寧波”));
citys.add(new City(“浙江”,”金華”));
citys.add(new City(“廣西”,”桂林”));
citys.add(new City(“廣西”,”南寧”));
branchs.add(new Branch(“浙江”,”寧波”, “寧波鎮海網點”));
branchs.add(new Branch(“浙江”,”寧波”, “寧波北侖網點”));
branchs.add(new Branch(“浙江”,”金華”, “金華網點1”));
branchs.add(new Branch(“浙江”,”金華”, “金華網點2”));
branchs.add(new Branch(“廣西”,”桂林”, “桂林網點1”));
branchs.add(new Branch(“廣西”,”桂林”, “桂林網點2”));
branchs.add(new Branch(“廣西”,”南寧”, “南寧網點1”));
branchs.add(new Branch(“廣西”,”南寧”, “南寧網點2”));
}
private void setBox(){//設置下拉列表裡的值
//設置先在各項列表裡加入的值
provinceBox.removeAllItems();
cityBox.removeAllItems();
branchBox.removeAllItems();
provinceBox.addItem(“所有”);
cityBox.addItem(“所有”);
branchBox.addItem(“所有”);
for (int i = 0; i provinces.size(); i++) {
provinceBox.addItem(provinces.get(i));
}
for (int i = 0; i citys.size(); i++) {
cityBox.addItem(citys.get(i).getCity());
}
for (int i = 0; i branchs.size(); i++) {
branchBox.addItem(branchs.get(i).getBranch());
}
}
private void addHandler(){//設置3個下拉列表的級聯關係
provinceBox.addActionListener(new ActionListener() {
//監聽province下拉列表
@Override
public void actionPerformed(ActionEvent e) {
cityBox.removeAllItems();
cityBox.addItem(“所有”);
branchBox.removeAllItems();
branchBox.addItem(“所有”);
String province=provinceBox.getSelectedItem().toString();//得到當前點中的值
//在city下拉列表中 遍歷citys,如果citys[i]的省份屬性和它相同則添加進去,其中要考慮是所有的特殊情況
if(province.equals(“所有”)){
for (int i = 0; i citys.size(); i++) {
cityBox.addItem(citys.get(i).getCity());
}
}else{
for (int i = 0; i citys.size(); i++) {
if(citys.get(i).getProvince().equals(province)){
cityBox.addItem(citys.get(i).getCity());
}
}
}
//在branch下拉列表中 遍歷branchs,如果branchs[i]的省份屬性和它相同則添加進去,其中要考慮是所有的特殊情況
if(province.equals(“所有”)){
for (int i = 0; i branchs.size(); i++) {
branchBox.addItem(branchs.get(i).getBranch());
}
}else{
for (int i = 0; i branchs.size(); i++) {
if(branchs.get(i).getProvince().equals(province)){
branchBox.addItem(branchs.get(i).getBranch());
}
}
}
}
});
//監聽cityBox下拉列表
cityBox.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
branchBox.removeAllItems();
branchBox.addItem(“所有”);
if(cityBox.getSelectedItem()==null){
return ;
}
String city=cityBox.getSelectedItem().toString();//得到當前點中的值
if(city.equals(“所有”)){//如果是所有,則看省份框中是什麼值
if(provinceBox.getSelectedItem().equals(“所有”)){
for (int i = 0; i branchs.size(); i++) {
branchBox.addItem(branchs.get(i).getBranch());
}
}else{
for (int i = 0; i branchs.size(); i++) {
if(branchs.get(i).getProvince().equals(provinceBox.getSelectedItem())){
branchBox.addItem(branchs.get(i).getBranch());
}
}
}
}else{
for (int i = 0; i branchs.size(); i++) {
if(city.equals(branchs.get(i).getCity())){
branchBox.addItem(branchs.get(i).getBranch());
}
}
}
}
});
}
private void showMe(){
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.pack();
}
public static void main(String[] args) {
new Test().showMe();
}
} //下面是要用的2個類
class City{//城市類
private String city;//城市名
private String province;//所屬省份
public City() {
// TODO Auto-generated constructor stub
}
public City(String province,String city) {
super();
this.province = province;
this.city = city;
} public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getProvince() {
return province;
}
public void setProvince(String province) {
this.province = province;
}
}
class Branch{//網點類
private String province;//所屬省份
private String city;//所屬城市
private String branch;//網點名 public Branch(String province, String city, String branch) {
super();
this.province = province;
this.city = city;
this.branch = branch;
}
public String getBranch() {
return branch;
}
public void setBranch(String branch) {
this.branch = branch;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getProvince() {
return province;
}
public void setProvince(String province) {
this.province = province;
}
}
三級級聯是一級下面掛了二級,二級下面又掛了三級,就是三級級聯.
以省,市,城聯動為例:
此小程序的功能主要是採用非同步請求方式從資料庫中調取省市區信息顯示到下拉列表:
代碼如下:
建立資料庫中的代碼和一些配置文件信息就省略了,主要有JavaScript中的代碼為:
$(document).ready(function(){
$.get(“getProvince.do”, function(result){
$(“#showp”).html(result);
});
})
var xmlhttp;
function mysend(str){
$(document).ready(function(){
$(“#show2”).html(“”);
})
var show = document.getElementByIdx_x_x_x_x_x_x_x_x_x(“show”);
show.innerHTML = “”;
var province = document.getElementByIdx_x_x_x_x_x_x_x_x_x(“province”).value;
if(province!=0){
if(window.XMLHttpRequest){
xmlhttp = new XMLHttpRequest();
}else{
xmlhttp = new ActiveXObject(“Microsoft.XMLHTTP”);
}
xmlhttp.onreadystatechange=function(){
if(xmlhttp.readyState==4 xmlhttp.status==200){
show.innerHTML = xmlhttp.responseText;
}
}
var ss = encodeURIComponent(str);
xmlhttp.open(“GET”,”getCity.do?provinceid=”+ss,true);
xmlhttp.send(null);
}
}
function myarea(str){
if(window.XMLHttpRequest){
xmlhttp = new XMLHttpRequest();
}else{
xmlhttp = new ActiveXObject(“Microsoft.XMLHTTP”);
}
xmlhttp.onreadystatechange=function(){
if(xmlhttp.readyState==4 xmlhttp.status==200){
var show2 = document.getElementByIdx_x_x_x_x_x_x_x_x_x(“show2”);
show2.innerHTML = xmlhttp.responseText;
}
}
var ss = encodeURIComponent(str);
xmlhttp.open(“GET”,”getArea.do?cityid=”+ss,true);
xmlhttp.send(null);
}
html頁面中的代碼為:
所在地
action中的代碼為:
package mobi.zhangsheng.jiejia.action;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.ServletActionContext;
import org.springframework.stereotype.Controller;
import mobi.zhangsheng.jiejia.domain.Areas;
import mobi.zhangsheng.jiejia.service.AgentsService;
import mobi.zhangsheng.jiejia.service.AreasService;
@Controller
public class ProvinceAction {
private int provinceid;
private int cityid;
@Resource
private AreasService as;
@Resource
private AgentsService ags;
public int getProvinceid() {
return provinceid;
}
public void setProvinceid(int provinceid) {
this.provinceid = provinceid;
}
public int getCityid() {
return cityid;
}
public void setCityid(int cityid) {
this.cityid = cityid;
}
public void getProvince(){
List provinceList = as.getAreasPrvinceList();
HttpServletResponse resp= ServletActionContext.getResponse();
HttpServletRequest request = ServletActionContext.getRequest();
//resp.setContentType(“xml”);
resp.setContentType(“text/html”);
resp.setCharacterEncoding(“utf-8”);
try {
PrintWriter out = resp.getWriter();
out.print(“”);
//out.print(“shanghai”);
} catch (IOException e) {
e.printStackTrace();
}
}
public void getCity(){
List cityList = as.getAreasCityList(provinceid);
HttpServletResponse resp= ServletActionContext.getResponse();
//resp.setContentType(“xml”);
resp.setContentType(“text/html”);
resp.setCharacterEncoding(“utf-8”);
try {
PrintWriter out = resp.getWriter();
out.print(“”);
//out.print(“shanghai”);
} catch (IOException e) {
e.printStackTrace();
}
}
public void getArea(){
List areaList = as.getAreasCityList(cityid);
if(areaList.size()==0){
}else{
HttpServletResponse resp= ServletActionContext.getResponse();
resp.setContentType(“text/html”);
resp.setCharacterEncoding(“utf-8”);
try {
PrintWriter out = resp.getWriter();
out.print(“”);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
原創文章,作者:QDO1T,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/126218.html