GRAILS集成EXTJS的Scaffolding實現

功能:根據Domain定義自動生成CRUD

效果圖:
[img]http://dl.iteye.com/upload/attachment/452688/ab1e3247-d61d-3fb6-9d41-36f8c9e1210e.png[/img]
代碼:

<% import grails.persistence.Event %><% import org.codehaus.groovy.grails.plugins.PluginManagerHolder %><%=packageName%>
<% boolean hasHibernate = PluginManagerHolder.pluginManager.hasGrailsPlugin('hibernate') %><%
def output(p,cp)
{
if (p.type == String.class) {
out << ",xtype: 'textfield'"
if (cp.blank == false) {
out << ", allowBlank: false, blankText: '\${cgDomainProperties.${p.name}.chinese}爲必填項'" //,msgTarget: 'side'"
}
if (cp.maxSize != null) {
out << ", maxLength: ${cp.maxSize}, maxLengthText: '\${cgDomainProperties.${p.name}.chinese}至多包含${cp.maxSize}個字符'"
}
if (cp.minSize != null) {
out << ", minLength: ${cp.minSize}, minLengthText: '\${cgDomainProperties.${p.name}.chinese}至少包含${cp.minSize}個字符'"
}
} else if (p.type == Date.class) {
out << "'datefield',format:'Y-m-d'"
}
}
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<g:extjs />
<g:set var="entityName" value="\${cgDomainProperties.cgChinese}" />
<title><g:message code="\${entityName}管理" /></title>
</head>
<script>
Ext.onReady(function(){
Ext.QuickTips.init();

var ${domainClass.propertyName}CreateForm = new Ext.form.FormPanel({
labelAlign: 'right',
labelWidth: 80,
frame: true,
url: '/foundation/${domainClass.propertyName}/createJSON',
defaultType: 'textfield',
items: [
{fieldLabel:'id',name: 'id',xtype: 'numberfield',hidden:true,hideLabel:true},<% excludedProps = Event.allEvents.toList() << 'version' << 'id' << 'dateCreated' << 'lastUpdated'
persistentPropNames = domainClass.persistentProperties*.name
props = domainClass.properties.findAll { persistentPropNames.contains(it.name) && !excludedProps.contains(it.name) }
Collections.sort(props, comparator.constructors[0].newInstance([domainClass] as Object[]))
display = true
props.eachWithIndex { p, i ->
if (!Collection.class.isAssignableFrom(p.type)) {
if (hasHibernate) {
cp = domainClass.constrainedProperties[p.name]
display = (cp ? cp.display : true)
}
if (display) { %>
{fieldLabel: '\${cgDomainProperties.${p.name}.chinese}',name: '${p.name}'<%
output(p,cp)
%>}<% if(props.size()>i+1){out<<","} %><% } } } %>
]
});

var ${domainClass.propertyName}CreateWin = new Ext.Window({
el: '${domainClass.propertyName}CreateWin',
closable:false,
layout: 'fit',
width: 400,
title: '創建\${entityName}',
height: 300,
closeAction: 'hide',
items: [${domainClass.propertyName}CreateForm],
buttons: [{
text:'創建',
handler: function(){
${domainClass.propertyName}CreateForm.getForm().submit({
success:function(${domainClass.propertyName}CreateForm, action){
Ext.foundation.msg('信息', action.result.msg);
${domainClass.propertyName}CreateWin.hide();
store.reload();
},
failure:function(){
Ext.foundation.msg('錯誤', "創建\${entityName}失敗!");
}
});
}
},{
text: '取 消',
handler: function(){
${domainClass.propertyName}CreateWin.hide();
}
}]
});

var ${domainClass.propertyName}UpdateForm = new Ext.form.FormPanel({
labelAlign: 'right',
labelWidth: 80,
frame: true,
url: '/foundation/${domainClass.propertyName}/updateJSON',
defaultType: 'textfield',
items: [
{fieldLabel:'id',name: 'id',xtype: 'numberfield',hidden:true,hideLabel:true},<% excludedProps = Event.allEvents.toList() << 'version' << 'id' << 'dateCreated' << 'lastUpdated'
persistentPropNames = domainClass.persistentProperties*.name
props = domainClass.properties.findAll { persistentPropNames.contains(it.name) && !excludedProps.contains(it.name) }
Collections.sort(props, comparator.constructors[0].newInstance([domainClass] as Object[]))
display = true
props.eachWithIndex { p, i ->
if (!Collection.class.isAssignableFrom(p.type)) {
if (hasHibernate) {
cp = domainClass.constrainedProperties[p.name]
display = (cp ? cp.display : true)
}
if (display) { %>
{fieldLabel: '\${cgDomainProperties.${p.name}.chinese}',name: '${p.name}',xtype: <% if(p.type==String.class){ out << "'textfield'"} else if(p.type==Date.class){ out << "'datefield',format:'Y-m-d'"}%>}<% if(props.size()>i+1){out<<","} %><% } } } %>
]
});

var ${domainClass.propertyName}UpdateWin = new Ext.Window({
el: '${domainClass.propertyName}UpdateWin',
closable:false,
layout: 'fit',
width: 400,
title: '修改\${entityName}',
height: 300,
closeAction: 'hide',
items: [${domainClass.propertyName}UpdateForm],
buttons: [{
text:'更新',
handler: function(){
${domainClass.propertyName}UpdateForm.getForm().submit({
success:function(${domainClass.propertyName}UpdateForm, action){
Ext.foundation.msg('信息', action.result.msg);
${domainClass.propertyName}UpdateWin.hide();
store.reload();
},
failure:function(){
Ext.foundation.msg('錯誤', "更新\${entityName}失敗!");
}
});
}
},{
text: '取 消',
handler: function(){
${domainClass.propertyName}UpdateWin.hide();
}
}]
});

var ${domainClass.propertyName}DetailForm = new Ext.form.FormPanel({
labelAlign: 'right',
labelWidth: 80,
frame: true,
url: '/foundation/${domainClass.propertyName}/detailJSON',
defaultType: 'textfield',
items: [
{fieldLabel:'id',name: 'id',xtype: 'numberfield',hidden:true,hideLabel:true},<% excludedProps = Event.allEvents.toList() << 'version' << 'id'
persistentPropNames = domainClass.persistentProperties*.name
props = domainClass.properties.findAll { persistentPropNames.contains(it.name) && !excludedProps.contains(it.name) }
Collections.sort(props, comparator.constructors[0].newInstance([domainClass] as Object[]))
display = true
props.eachWithIndex { p, i ->
if (!Collection.class.isAssignableFrom(p.type)) {
if (hasHibernate) {
cp = domainClass.constrainedProperties[p.name]
display = (cp ? cp.display : true)
}
if (display) { %>
{fieldLabel: '\${cgDomainProperties.${p.name}.chinese}',name: '${p.name}',readOnly: true, xtype: <% if(p.type==String.class){ out << "'textfield'"} else if(p.type==Date.class){ out << "'datefield',format:'Y-m-d'"}%>}<% if(props.size()>i+1){out<<","} %><% } } } %>
]
});

var ${domainClass.propertyName}DetailWin = new Ext.Window({
el: '${domainClass.propertyName}DetailWin',
closable:false,
layout: 'fit',
width: 400,
title: '\${entityName}明細',
height: 300,
closeAction: 'hide',
items: [${domainClass.propertyName}DetailForm],
buttons: [{
text: '確定',
handler: function(){
${domainClass.propertyName}DetailWin.hide();
}
}]
});

var tb = new Ext.Toolbar();
tb.render('toolbar');

tb.add({
text: '新建',
icon: '/foundation/images/skin/database_add.png',
handler:function(){
${domainClass.propertyName}CreateWin.show(this);
}
},{
text: '修改',
icon: '/foundation/images/skin/database_edit.png',
handler: function(){
var id = (grid.getSelectionModel().getSelected()).id;
${domainClass.propertyName}UpdateForm.getForm().load({
url:'/foundation/${domainClass.propertyName}/detailJSON?id='+id,
success:function(form,action){},
failure:function(){
Ext.foundation.msg('錯誤', "服務器出現錯誤,稍後再試!");
}
});

customerUpdateWin.show();
}
},{
text: '刪除',
icon: '/foundation/images/skin/database_delete.png',
handler: function(){
var count=sm.getCount();
if(count==0)
{
Ext.foundation.msg('注意', "請選擇要刪除的記錄");
}else {
var records = sm.getSelections();
var id=[];
for(var i=0;i<count;i++)
{
id.push(records[i].id);
}
Ext.MessageBox.confirm('信息', '您確定刪除' + id + '記錄?', function(btn) {
if (btn == 'yes') {
Ext.Ajax.request({
url: '/foundation/${domainClass.propertyName}/deleteJSON',
params: {id:id},
success: function(result) {
var json_str = Ext.util.JSON.decode(result.responseText);
//Ext.foundation.msg('信息', json_str.msg);
MessageShow('信息',json_str.msg);
store.reload();
},
failure:function() {
Ext.foundation.msg('錯誤', '服務器出現錯誤,稍後再試!');
}
});
}
});

}
}
},{
text: '詳細',
icon: '/foundation/images/skin/database_save.png',
handler: function(){
var id = (grid.getSelectionModel().getSelected()).id;
${domainClass.propertyName}DetailForm.getForm().load({
url:'/foundation/${domainClass.propertyName}/detailJSON?id='+id,
success:function(form,action){},
failure:function(){
Ext.foundation.msg('錯誤', '服務器出現錯誤,稍後再試!');
}
});
customerDetailWin.show();
}
},'->',
{
xtype: 'textfield',
name: 'searchBar',
emptyText: '請輸入搜索條件'
},{
text: '搜索',
icon: '/foundation/images/skin/database_search.png',
handler: function(){
}
}
);

tb.doLayout();

var sm= new Ext.grid.CheckboxSelectionModel()
var cm = new Ext.grid.ColumnModel([
sm,<% excludedProps = Event.allEvents.toList() << 'version'
allowedNames = domainClass.persistentProperties*.name << 'id' << 'dateCreated' << 'lastUpdated'
props = domainClass.properties.findAll { allowedNames.contains(it.name) && !excludedProps.contains(it.name) && !Collection.isAssignableFrom(it.type) }
Collections.sort(props, comparator.constructors[0].newInstance([domainClass] as Object[]))
props.eachWithIndex { p, i ->
if (i < 10) {
if (p.isAssociation()) { %><% } else { %>
{header:'\${cgDomainProperties.${p.name}.chinese}',dataIndex:'${p.name}'} <% if(props.size()>i+1){out<<","} %><% } } } %>
]);

var store= new Ext.data.Store({
autoLoad:true,
proxy: new Ext.data.HttpProxy({url:'/foundation/${domainClass.propertyName}/listJSON'}),
reader: new Ext.data.JsonReader({
totalProperty:'total',
root:'root'
},[<% props.eachWithIndex { p, i ->
if (i < 10) {
if (p.isAssociation()) { %><% } else { %>
{name:'${p.name}'}<% if(props.size()>i+1){out<<","} %><% } } } %>
])
});

var grid= new Ext.grid.GridPanel({
renderTo: 'grid',
store: store,
enableColumnMove:false,
enableColumnResize:true,
stripeRows:true,
enableHdMenu: false,
trackMouseOver: true,
loadMask:true,
cm: cm,
sm: sm,
height: 270,
viewConfig: {
forceFit:true
},

bbar: new Ext.PagingToolbar({
pageSize: 10,
store: store,
displayInfo: true,
displayMsg: '顯示第{0}條到第{1}條記錄, 共{2}條',
emptyMsg: '沒有記錄'
})
});

store.load({params:{start:0,limit:10}});

var window = new Ext.Window({
// contentEl : Ext.getBody(),
id: 'msgWindow',
width : 200,
height : 150,
shadow : false,
html : "試試試試...",
title : "溫馨提示:"
});

function show() {
this.el.alignTo(Ext.getBody(), 't');
this.el.slideIn('t', {
easing : 'easeOut',
callback : function() {
this.close.defer(1000, this); //定時關閉窗口
},
scope : this,
duration : 1
});

}

function hide() {
if (this.isClose === true) { //防止點擊關閉和定時關閉處理
return false;
}
this.isClose = true;
this.el.slideOut('t', {
easing : 'easeOut',
callback : function() {
this.un('beforeclose', hide, this);
this.close();
},
scope : this,
duration : 1
});
return false;
}

window.on('show', show, window);
window.on('beforeclose', hide, window)
//window.show();

function MessageShow(title, content)
{
var win = Ext.getCmp('msgWindow');
win.setTitle(title);
//win.html=content;

win.show();
}
});
</script>
<body>
<div id="toolbar"></div>
<div id="grid"></div>
<div id="${domainClass.propertyName}CreateWin"></div>
<div id="${domainClass.propertyName}UpdateWin"></div>
<div id="${domainClass.propertyName}DetailWin"></div>
</body>
</html>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章