Dorado7自定义下拉框(三)

本篇介绍省市县级联下拉框,最终效果如图:


1.添加如下控件


这里的DataType选择base里面的Bean,我这里做成一个公共的插件,用到了GroupStart和GroupEnd(在这里我就不详细介绍了)。

2.dataSet的实现方法:

/**
	 * 查询省市县
	 * 
	 * @param page
	 * @param params
	 * @throws SQLException
	 */
	@DataProvider
	public void getProvCityDist(Page<CityBean> page, Map<String, Object> params) throws SQLException {
		if (params == null) {
			params = Collections.emptyMap();
		}
		List<CityBean> entities = new ArrayList<CityBean>();
		
		String type = (String) params.get("type");
		String parentCode = (String) params.get("parentCode");
		String filterValue = (String) params.get("filterValue");
		
		Map<Integer, String> ps = new HashMap<Integer, String>();
		String sql = "SELECT S.CODE,S.NAME from SYS_CITY S WHERE ISNULL(S.IS_VALID,1) = 1 AND 1=1";
		if (StringUtils.isNotEmpty(type)) {
			sql += " AND S.TYPE = ? AND S.PARENT_CODE = ? ";
			ps.put(1, type);
			ps.put(2, parentCode == null ? "" : parentCode);
		}
		if (StringUtils.isNotEmpty(filterValue)) { // 注意:若前台未正确处理 type 和 parentCode,则在全体数据中过滤
			sql += " AND (S.CODE like ? or S.NAME like ?)";
			ps.put(3, "%" + filterValue + "%");
			ps.put(4, "%" + filterValue + "%");
		}
		Connection conn = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {
			conn = QxxtUtil.getConnection();
			stmt = conn.prepareStatement(sql);
			if (!ps.isEmpty()) {
				for (Integer index : ps.keySet()) {
					stmt.setString(index, ps.get(index));
				}
			}
			rs = stmt.executeQuery();
			while (rs.next()) {
				CityBean cb = new CityBean();
				cb.setCode(rs.getString(1));
				cb.setName(rs.getString(2));
				entities.add(cb);
			}
		} finally {
			if (rs != null) {
				rs.close();
			}
			if (stmt != null) {
				stmt.close();
			}
			if (conn != null) {
				conn.close();
			}
		}
		paging(page, entities);
	}
	
	private <T> void paging(Page<T> page, List<T> entities) {
		int start = (page.getPageNo() - 1) * page.getPageSize();
		int end = start + page.getPageSize();
		int size = entities.size();
		if (end > size) {
			end = size;
		}
		page.setEntities(entities.subList(start, end));
		page.setEntityCount(size);
	}

3.自定义下拉框的onOpen事件:

view.__initEditor(view.get("#$dsSsx"), self.get("editor"), null, null, "code", "name", function(userData, text) {
	return {
		type : userData == null ? "" : userData.type,
		parentCode : userData == null ? "" : userData.parentCode,
		filterValue : text
	};
});
onValueSelect事件:

arg.processDefault = view.__selectedValue(arg.editor,arg.selectedValue,"setSsx");

Control控件的onCreate事件:

//自定义下拉框的onOpen事件中调用该方法。
//ds:下拉框数据集DataSet;editor:关联下拉框的编辑框;defaultCode和defaultName:正在编辑的Entity中默认的关联字段;
//code和name:下拉框数据来源字段;parameterCallback:设置下拉框查询的参数的回调函数,接收两个参数,分别是绑定下拉框的控件的userData和编辑框中的文本
view.__initEditor = function(ds, editor, defaultCode, defaultName, code, name, parameterCallback) {
	if (editor.__$editorBind == 1) {
		ds.set("parameter", parameterCallback(editor.__ud, editor.get("text"))).flushAsync();
	} else {
		var parent = editor.get("parent");
		if (dorado.Object.isInstanceOf(parent, dorado.widget.FormElement)) {
			editor.__ud = parent.get("userData");
			editor.__controlId = parent.get("id");
		} else if (dorado.Object.isInstanceOf(parent, dorado.widget.DataGrid)) {
			editor.__ud = parent.get("currentColumn.editor.userData");
			editor.__controlId = parent.get("currentColumn.id");
		}
		ds.set("parameter", parameterCallback(editor.__ud, editor.get("text"))).flushAsync();
		editor.__sourceCode = code;
		editor.__sourceName = name;
		editor.__defaultCode = editor.__ud == null ? defaultCode : editor.__ud.code;
		editor.__defaultName = editor.__ud == null ? defaultName : editor.__ud.name;
		editor.__getEntity = function(callback) {
			if (dorado.Object.isInstanceOf(parent, dorado.widget.autoform.AutoFormElement)) {
				// entity = parent.get("parent").get("entity");
				parent.get("dataSet").getDataAsync(parent.get("dataPath"), function(entity) {
					callback(entity);
				});
			} else if (dorado.Object.isInstanceOf(parent, dorado.widget.FormElement)) {
				parent.get("dataSet").getDataAsync(parent.get("dataPath"), function(entity) {
					callback(entity);
				});
			} else if (dorado.Object.isInstanceOf(parent, dorado.widget.DataGrid)) {
				callback(parent.get("currentEntity"));
			}
		}

		editor.addListener("onPost", function(self, arg) {
			editor.__getEntity(function(entity) {
				ds.getDataAsync("[@.get('" + code + "')=='" + entity.get(editor.__defaultCode) + "']", function(data) {
					var found = false;
					var text = editor.get("text");
					if (data) {
						if (text != null && text != "") {
							if (data.get(name) == text)
								found = true;
						}
					}
					if (!found) {
						entity.set(editor.__defaultCode, null);
						entity.set(editor.__defaultName, null);
					}
				});
			});
		});

		editor.addListener("onTextEdit", function(self, arg) {
			ds.set("parameter", parameterCallback(editor.__ud, self.get("text"))).flushAsync();
		});
		// 上下左右方向键按键事件
		var udlrKeyDown = function(arg, data) {
			if (!arg.shiftKey && !arg.ctrlKey && !arg.altKey) {
				switch (arg.keyCode) {
				case 13:
					// 回车键
					editor.set("text", data == null ? "" : data.get(editor.__sourceName));
					editor.get("trigger").close(data);
					// arg.returnValue = false;
					break;
				case 37:
					// left
					ds.getDataAsync("*", function(list) {
						list.previousPage();
					});
					arg.returnValue = false;
					break;
				case 39:
					// right
					ds.getDataAsync("*", function(list) {
						list.nextPage();
					});
					arg.returnValue = false;
					break;
				case 38:
					// up
					ds.getDataAsync("*", function(list) {
						list.previous();
					});
					arg.returnValue = false;
					break;
				case 40:
					// down
					ds.getDataAsync("*", function(list) {
						list.next();
					});
					arg.returnValue = false;
					break;
				}

			}
		};
		var container = editor.get("trigger").get("control");
		container.get("children").each(function(control) {
			if (dorado.Object.isInstanceOf(control, dorado.widget.DataGrid)) {
				control.addListener("onKeyDown", function(self, arg) {
					udlrKeyDown(arg, control.get("currentEntity"));
				});
				control.addListener("onDataRowClick", function(self, arg) {
					editor.get("trigger").close(arg.data);
				});
				container.__grid = control;
				return false;
			}
		});
		editor.addListener("onKeyDown", function(self, arg) {
			udlrKeyDown(arg, container.__grid.get("currentEntity"));
		});
		container.addListener("onKeyDown", function(self, arg) {
			if (!arg.shiftKey && !arg.ctrlKey && !arg.altKey) {
				udlrKeyDown(arg, self.__grid.get("currentEntity"));
			}
		});
		editor.__$editorBind = 1;
	}
}
view.__selectedValue = function(editor, selectedValue, setFunctionName) {
	if (selectedValue) {
		editor.__getEntity(function(entity) {
			if (entity) {
				var setFun = null;
				if (editor.__controlId) {
					var str = editor.__controlId;
					var s = str.substring(0, 1).toUpperCase();
					if (str.length > 1) {
						s += str.substring(1);
					}
					setFun = eval("view.set" + s);
				}
				if (setFun) {
					setFun(entity, selectedValue);
				} else {
					setFun = eval("view." + setFunctionName);
					if (setFun) {
						setFun(selectedValue, editor.__controlId, entity);
					} else {
						var dt = entity.dataType;
						var pd = dt.getPropertyDef(editor.__defaultCode);
						if (pd) {
							entity.set(editor.__defaultCode, selectedValue.get(editor.__sourceCode));
						}
						pd = dt.getPropertyDef(editor.__defaultName);
						if (pd) {
							entity.set(editor.__defaultName, selectedValue.get(editor.__sourceName));
						}
					}
				}
			}
		});
	}
	return false;
}

此外,设置该控件hideMode属性为display和visible属性为false。

4.在你当前的页面添加Import控件,在src属性写上你刚才做的那个页面的位置,下面是我的位置:com.jursoft.qxxt.view.ProvDropDown#provDropDownStart

5.在相应的字段下面添加你的下拉框,由于是做的公共的,不在本页面,因此,你在本页面是查找不到该下拉框,你只需要直接将该下拉框的id复制即可。


设置code和name为你的相应实体字段,type的值:省字段为1,市字段为2,县字段为3,这里type的值是根据你数据库里面的值进行设置的,parentCode的值为空

6.市字段的onFocus事件:

self._userData.parentCode = self._dataSet.getData("#").get("item1");
该事件是获取省的code值并赋值给市字段的parentCode的值,由于本人做的是根据省加载相应的市,考虑到没有必要将市全部加载出来。同理,县字段的onFocus事件获取市的code值。

到此,省市县级联下拉框就可以实现了。如有建议的,欢迎提出评论!

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Dorado7是一款功能强大的下载和安装管理软件。以下是关于如何下载和安装Dorado7的详细步骤: 1. 打开浏览器并访问Dorado7的官方网站。 2. 在官方网站的主页上,您会找到一个下载按钮,点击它。 3. 在弹出的下载窗口中,选择适用于您操作系统的版本(Windows、Mac或Linux),然后点击“下载”按钮。 4. 下载完成后,找到您下载的安装包文件,它通常会保存在您的浏览器默认下载文件夹中。 5. 双击安装包文件,以启动安装程序。 6. 在安装向导中,您将被要求接受许可协议。请阅读并仔细阅读协议内容,然后点击“同意”或“接受”按钮,以继续安装过程。 7. 您可以选择要安装的Dorado7的目标文件夹,或者使用默认的安装路径。点击“下一步”继续。 8. 在下一个安装向导步骤中,您可以选择是否要创建一个桌面快捷方式以便更方便地访问Dorado7。选择您偏好的选项,然后点击“下一步”。 9. 然后,您将看到一个总结页面,显示安装设置的概述。点击“安装”按钮以开始安装过程。 10. 安装过程可能需要一些时间,取决于您的计算机性能和网络连接速度。请耐心等待,直到安装完成。 11. 安装完成后,您可以选择启动Dorado7或退出安装向导。如果您想立即使用该软件,请选择启动Dorado7并跟随其提示进行操作。 现在,您已经成功地下载和安装了Dorado7。您可以使用它来下载和管理各种文件,享受其功能强大的下载和安装管理能力。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值