欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Dorado7自定义下拉框(三)

程序员文章站 2022-05-31 16:48:21
...

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

Dorado7自定义下拉框(三)

1.添加如下控件

Dorado7自定义下拉框(三)

这里的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复制即可。

Dorado7自定义下拉框(三)

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

6.市字段的onFocus事件:

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

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