`

Ext4 treePanel checkBox选择样例

阅读更多
/**
 * 功能主程序类
 */
Ext.define('com.RolePanel', {
	extend : 'Ext.panel.Panel',
	xtype : 'com-role-grid',
	initComponent : function() {
		var self = this;
		var store = new Ext.data.Store({
			autoLoad : true,
			proxy : {
				type : 'ajax',
				url : 'roleAction!list.action',
				reader : {
					type : 'json',
					root : 'rows'
				}
			},
			model : 'com.RolePanel.Mode',
			writer : new Ext.data.JsonWriter(),
			listeners : {
				'load' : function(store, records) {
					if (records) {
						if (roleGridPanel.getSelectionModel().getSelection().length <= 0) {
							roleGridPanel.getSelectionModel()
									.select(records[0]);
							self.authFormPanel.loadRecord(records[0]);
						}
					}
				}
			}
		});

		var roleGridPanel = new Ext.grid.Panel({
					region : 'center',
					columnLines : true,
					store : store,
					loadMask : true,
					columns : [{
								text : '编号',
								sortable : true,
								dataIndex : 'cid',
								locked : true,
								hidden : true
							}, {
								text : '角色名称',
								dataIndex : 'cname',
								flex : 5
							}, {
								text : '备注',
								dataIndex : 'cdesc',
								flex : 5
							}, {
								text : 'ctype',
								dataIndex : 'ctype',
								hidden : true
							}, {
								text : '拥有权限',
								dataIndex : 'authIds',
								hidden : true
								// renderer : function(value, metaData, record)
							// {
							// if (record.raw.authNames) {
							// return Ext.String.format(
							// '<span title={0}>{1}</span>',
							// record.raw.authNames,
							// record.raw.authNames);
							// }
							// }
						}],
					bbar : Ext.create('Ext.PagingToolbar', {
								store : store,
								displayInfo : true,
								beforePageText : '第',
								afterPageText : '页,共{0}页',
								displayMsg : '共{2}个',
								emptyMsg : "没有记录"
							}),
					tbar : [{
								text : '增加',
								scope : this,
								iconCls : 'Add',
								handler : function() {
									roleGridPanel.getSelectionModel()
											.deselectAll();
									self.authFormPanel.form.reset();
								}
							}, {
								text : '删除',
								scope : this,
								iconCls : 'Delete',
								handler : function() {
									this.openDelete(self);
								}
							}],
					forceFit : true
				});
		this.roleGridPanel = roleGridPanel;
		roleGridPanel.getSelectionModel().on('selectionchange',
				function(t, sMode) {
					if (sMode[0]) {
						self.authFormPanel.loadRecord(sMode[0]);
					}
				}, this);
		var storeRoleAuth = Ext.create('Ext.data.TreeStore', {
					// autoLoad : true,
					proxy : {
						type : 'ajax',
						url : "authAction!authTreeRecursive.action"
					},
					fields : ['id', 'text', 'leaf', 'checked'],
					root : {
						id : '0',
						text : 'root',
						lazyLoad : true
					}
				});

		var authTree = new Ext.tree.Panel({
					width : 400,
					height : 450,
					autoScroll : true,
					animate : true,
					containerScroll : true,
					rootVisible : false,
					store : storeRoleAuth,
					listeners : {
						'expand' : function(p) {
							var node = p.getSelectedNode();
							if (!node)
								self.refresh(p.getRootNode());
							else {
								self.refresh(node);
							}
						}
					}
				});
		authTree.on('checkchange', function(node, checked) {
					node.checked = checked;
					if (node.parentNode.id.length != 1) {
						self.setParentNode(node.parentNode, checked, self);
						node.expand();
					}
					if (node.hasChildNodes())
						self.setChildNode(node, checked, self);
				}, authTree);
		self.authTree = authTree;
		var authFormPanel = new Ext.form.Panel({
			title : '角色编辑',
			region : 'east',
			split : true,
			collapsible : true,
			border : false,
			items : [{
						fieldLabel : '编号',
						name : 'id',
						hidden : true
					}, {
						xtype : 'textfield',
						allowBlank : false,
						fieldLabel : '角色名称',
						name : 'name',
						emptyText : '角色名称'
					}, {
						xtype : 'textfield',
						fieldLabel : '备注',
						name : 'desc'
					}, {
						xtype : 'textfield',
						name : 'authIds',
						fieldLabel : '权限Id',
						hidden : true,
						listeners : {
							'change' : function(authId) {
								var root = authTree.getRootNode();
								root.eachChild(function(child) {
											child.checked = false;
											child.set('checked', false);
											authTree.fireEvent('checkchange',
													child, false);
										});
								var authIds = authId.getValue();
								if (authIds) {
									Ext.each(authIds.split(","), function(
											authId) {
										self.setNodeChecked(authId, root, self);
									});
								}
							}
						}
					}, {
						xtype : 'fieldset',
						autoHeight : true,
						title : '权限',
						width : '100%',
						items : authTree
					}],
			tbar : [{
						text : '保存',
						iconCls : 'icon-save',
						scope : this,
						handler : function() {
							self.saveRoleAuth(self);
						}
					}]
		});
		self.authFormPanel = authFormPanel;
		Ext.apply(this, {
					layout : 'border',
					border : 0,
					items : [roleGridPanel, authFormPanel]
				});
		this.callParent();
	},
	setNodeChecked : function(nodeId, node, self) {
		if (nodeId == node.data.id) {
			node.checked = true;
			node.set('checked', true);
			self.authTree.fireEvent('checkchange', node, true);
			return false;
		} else if (node.hasChildNodes()) {
			node.eachChild(function(child) {
						var result = self.setNodeChecked(nodeId, child, self);
						if (!result)
							return false;
					});
		}
		return true;
	},
	setChildNode : function(node, checked, self) {
		node.expand();
		node.eachChild(function(child) {
					child.checked = checked;
					child.set('checked', checked);
					if (child.hasChildNodes())
						self.setChildNode(child, checked, self);
				});
	},
	setParentNode : function(node, checked, self) {
		var childNoChecked = false;
		node.checked = checked;
		node.eachChild(function(child) {
					if (!child.checked) {
						childNoChecked = true;
						return false;
					}
				});
		node.set('checked', childNoChecked
						? (checked ? false : checked)
						: checked);
		if (node.parentNode != null)
			self.setParentNode(node.parentNode, checked, self);
	},
	openDelete : function(self) {
		var selection = self.roleGridPanel.getSelectionModel().getSelection()[0];
		if (selection) {
			Ext.MessageBox.confirm('提示', '你确定要删除"' + selection.raw.cname
							+ '"吗?', function(resu) {
						if (resu == 'yes') {
							Ext.Ajax.request({
										url : 'userAction!delete.action',
										params : {
											ids : selection.raw.cid
										},
										success : function(response, options) {
											var jsonResult = Ext.JSON
													.decode(response.responseText);
											if (jsonResult.success)
												self.roleGridPanel.getStore()
														.reload();
											Ext.Msg.alert('提示', jsonResult.msg);
										},
										failure : function(response, options) {
											var jsonResult = Ext.JSON
													.decode(response.responseText);
											Ext.Msg.alert('失败', jsonResult.msg);
										}
									});
						}
					});
		} else {
			Ext.MessageBox.alert('提示', '请选择你要删除的数据');
		}
	},
	saveRoleAuth : function(self) {
		var result = self.authFormPanel.form.getFieldValues();
		var root = self.authTree.getRootNode();
		var authIds = [];
		root.eachChild(function(child) {
					self.getChildNodeAuth(child, authIds, self);
				});
		self.authFormPanel.form.findField('authIds')
				.setValue(authIds.join(','));
		self.authFormPanel.form.submit({
					url : "roleAction!save.action",
					method : 'POST',
					success : function(f, action) {
						var result = action.result;
						if (result.success) {
							self.roleGridPanel.getStore().load();
						} else {
							Ext.Msg.alert('错误', result.message);
						}
					},
					failure : function(f, action) {
						Ext.Msg.alert('错误', resp.responseText);
					}
				});
	},
	getChildNodeAuth : function(node, authIds, self) {
		if (node.data.checked)
			authIds.push(node.data.id);
		if (node.hasChildNodes())
			node.eachChild(function(child) {
						self.getChildNodeAuth(child, authIds, self);
					});
	}
});

/**
 * 用户数据MODE
 */
Ext.define('com.RolePanel.Mode', {
			extend : 'Ext.data.Model',
			fields : [{
						name : 'id',
						type : 'string'
					}, {
						name : 'name',
						type : 'string'
					}, {
						name : 'desc',
						type : 'string'
					}, {
						name : 'type',
						type : 'string'
					}, {
						name : 'authIds',
						type : 'string'
					}, {
						name : 'authNames',
						type : 'string'
					}]

		});

 java 返回树对象字段:

   

public class TreeNode implements java.io.Serializable {

	private static final long serialVersionUID = 1L;
	private String id;
	private String text;// 树节点名称
	private String iconCls;// 前面的小图标样式
	private List<TreeNode> children;// 子节点
	private boolean leaf = false;
	private boolean expanded = true;// 是否展开(true,false)
	private String checked = null;
	private Object attachment;
}

 

0
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics