也算是一个总结吧,备忘,总结了一个增删改查的例子,以后要达到的目标是每个模块一个 js,每个js都把当前模块的相关操作完成,例如增删改差等。

//重要指定本机的ext空白图片
Ext.BLANK_IMAGE_URL = '../js/ext/s.gif';
/**
以下为工具函数 -- start
**/
function transferMap(m, no) {
var v = new Array();
if (no) v.push(['不限', '']);
for (var p in m) {
v.push([m[p], p]);
}
return v;
}
function getSubJobC(key, map) {
for (var i = 0; i < map.length; i++) {
if (map[i].key == key) return map[i].children;
}
return [];
}
function sure(str, l) {
if (!str) str = '1';
str = str + '';
var len = str.length;
for (var i = 0; i < l - len; i++)
str = '0' + str;
return str;
}
function getSimpleCombo(config) {
var config_data = transferMap(config.dataMap, config.unlimit);
var template = {
value: config_data[0][1],
allowBlank: config.allowBlank,
store: new Ext.data.SimpleStore({
fields: ['text', 'value'],
data: config_data
}),
displayField: 'text',
typeAhead: true,
valueField: 'value',
fieldLabel: config.fieldLabel,
mode: 'local',
name: config.name + '_',
hiddenName: config.name,
xtype: 'combo',
triggerAction: 'all',
//emptyText:'选择',
selectOnFocus: true,
forceSelection: true
};
return template;
}
function toArray(ma, v) {
if (!v) v = new Array();
for (var i = 0; i < ma.length; i++) {
v.push([ma[i].label, ma[i].label]);
if (ma[i].children) toArray(ma[i].children, v);
}
return v;
}
/**
以下为工具函数 -- end
**/
/**
建立四棵选择树,以及选择树窗口
**/
function createSingleSelectWin() {
var singleSelectWin = {};
var singleLocation = new yiminghe.checkedTreeSingle({
width: 250,
height: 400,
dataArray: theRegions,
title: '地区大类',
expandable: true,
singleClickExpand: false,
singleExpand: true,
clickFunc: function (n) {
singleLocation2.setDataArray(getSubJobC(n.attributes.id, theRegions), true);
},
level: 0,
allowEmpty: false
});
var singleLocation2 = new yiminghe.checkedTreeSingle({
width: 250,
height: 400,
dataArray: [],
title: '具体地区',
expandable: false,
singleClickExpand: true,
singleExpand: true,
level: 99,
allowEmpty: false
});
var singleJobSelector = new yiminghe.checkedTreeSingle({
width: 250,
height: 400,
dataArray: theJobCategories,
title: '职业类别',
expandable: true,
singleClickExpand: false,
singleExpand: true,
clickFunc: function (n) {
singleJobSelector2.setDataArray(getSubJobC(n.attributes.id, theJobCategories), true);
},
level: 0,
allowEmpty: false
});
var singleJobSelector2 = new yiminghe.checkedTreeSingle({
width: 250,
height: 400,
dataArray: [],
title: '具体职业',
expandable: false,
singleClickExpand: true,
singleExpand: true,
level: 99,
allowEmpty: false
});
var singleSelectorWindow = new Ext.Window({
renderTo: document.body,
width: 520,
layout: 'column',
height: 470,
closeAction: 'hide',
title: '单击选择项目',
modal: true,
items: [{
columnWidth: .5,
layout: 'fit',
height: 400,
items: [singleJobSelector.getExtTree(), singleLocation.getExtTree()]
},
{
columnWidth: .5,
height: 400,
layout: 'fit',
items: [singleJobSelector2.getExtTree(), singleLocation2.getExtTree()]
}],
buttons: [{
text: '确认选定',
handler: function () {
var c;
var tree = singleLocation;
var tree2 = singleLocation2;
if (singleLocation.getExtTree().isVisible()) {
tree = singleLocation;
tree2 = singleLocation2;
c = singleLocation.extField;
} else if (singleJobSelector.getExtTree().isVisible()) {
tree = singleJobSelector;
tree2 = singleJobSelector2;
c = singleJobSelector.extField;
}
var n = tree2.getSelectionNode();
if (!n) {
n = tree.getSelectionNode();
}
if (n) {
if (n.attributes.id == -9999) {
alert('请选择');
return;
}
} else {
alert('请选择');
return;
}
//alert(n.attributes.text);
if (n.attributes.id == 'he_empty') c.setValue('');
else c.setValue(n.attributes.id);
c.setRawValue(n.attributes.text);
singleSelectorWindow.hide();
}
}]
});
singleSelectWin = {
win: singleSelectorWindow,
jobs: [singleJobSelector, singleJobSelector2],
locations: [singleLocation, singleLocation2]
};
return singleSelectWin;
}
/**
建立 添加修改窗口
**/
function createInputWindow() {
var singleSelectWin = createSingleSelectWin();
/**
一些form 字段 对象
**/
var triggerWorkAddress = new Ext.form.ComboBox({
allowBlank: false,
name: 'workAddress_',
fieldLabel: '<span style="color:red">*</span> 工作地点',
emptyText: '按下拉箭头选择',
editable: false,
hiddenName: 'workAddress',
store: []
});
triggerWorkAddress.onTriggerClick = function () {
for (var i = singleSelectWin.jobs.length - 1; i >= 0; i--) singleSelectWin.jobs[i].hide();
for (var i = singleSelectWin.locations.length - 1; i >= 0; i--) singleSelectWin.locations[i].show();
singleSelectWin.win.show();
singleSelectWin.win.toFront();
};
singleSelectWin.locations[0].extField = triggerWorkAddress;
var triggerJob = new Ext.form.ComboBox({
//allowBlank:false,
name: 'jobTitle_',
fieldLabel: '<span style="color:red">*</span> 选择职位',
emptyText: '按下拉箭头选择',
triggerAction: 'all',
selectOnFocus: true,
editable: false,
hiddenName: 'jobTitle',
store: []
});
triggerJob.onTriggerClick = function () {
for (var i = singleSelectWin.jobs.length - 1; i >= 0; i--) singleSelectWin.jobs[i].show();
for (var i = singleSelectWin.locations.length - 1; i >= 0; i--) singleSelectWin.locations[i].hide();
singleSelectWin.win.show();
singleSelectWin.win.toFront();
};
singleSelectWin.jobs[0].extField = triggerJob;
var jobTitleExt = new Ext.form.ComboBox({
xtype: 'combo',
store: new Ext.data.SimpleStore({
fields: ['text', 'value'],
data: toArray(theJobCategories)
}),
displayField: 'text',
typeAhead: true,
valueField: 'value',
mode: 'local',
name: 'jobTitleEx',
triggerAction: 'all',
emptyText: '可直接输入职位名称',
selectOnFocus: true,
fieldLabel: '或 职位关键字',
listeners: {
'blur': function (f) {
if (f.getRawValue().trim() != '') {
triggerJob.setValue('');
triggerJob.setRawValue('');
}
}
}
});
/**
添加修改的form
**/
var inputForm = new Ext.FormPanel({
//width:720,
//height:800,
frame: true,
//renderTo:document.body,
bodyBorder: false,
border: false,
method: 'POST',
defaults: {
anchor: '97%'
//不能加 xtype ,column layout do not work
},
//form 中的按钮
buttons: [{
text: '确定',
//点击按钮所触发的动作
handler: function () {
if (inputForm.form.isValid()) {
//ajax form submit
inputForm.form.submit({
url: 'company_setjob_action.jsp',
success: function (form, action) {
if (!action.result.success) {
Ext.Msg.alert('失败', action.result.data.info);
return;
}
Ext.Msg.alert('注意', '亲爱的用户,您好!您的职位已发布成功,我们将在24小时内进行审核,审核前您可修改职位信息。');
inputWindow.hide();
inputForm.underGrid.reload();
},
waitMsg: '正在保存信息',
waitTitle: '稍后...',
failure: function (form, action) {
Ext.Msg.alert('失败', action.result.data.info);
}
});
} else {
// Ext.Msg.alert('信息', '请填写完成再提交!');
}
}
}],
//form 中的字段对象
items: [
//字段1 start
{
xtype: 'hidden',
name: 'jobId'
},
//字段1 end
//字段集合开始1
{
xtype: 'fieldset',
autoHeight: true,
layout: 'column',
title: '描述这个职位',
bodyStyle: 'padding:15px 15px 0',
//frame:true,
items: [{
columnWidth: .5,
layout: 'form',
items: [triggerJob, getSimpleCombo({
dataMap: theJobTypeMap,
fieldLabel: '<span style="color:red">*</span> 职位性质',
name: 'jobType',
allowBlank: false
}), {
xtype: 'textfield',
name: 'validDays',
allowBlank: false,
fieldLabel: '<span style="color:red">*</span> 职位有效期(天)'
},
{
xtype: 'numberfield',
name: 'desiredSalary',
fieldLabel: '薪资待遇(元)'
},
{
xtype: 'textfield',
name: 'deptName',
fieldLabel: '所属部门'
}]
},
{
columnWidth: .5,
layout: 'form',
items: [jobTitleExt, triggerWorkAddress, {
allowBlank: false,
xtype: 'numberfield',
name: 'desiredCount',
fieldLabel: '<span style="color:red">*</span> 招聘人数'
},
getSimpleCombo({
dataMap: theSalaryTypeMap,
fieldLabel: '薪资类型',
name: 'desiredSalaryType',
allowBlank: false
}), {
xtype: 'textarea',
allowBlank: false,
name: 'jobDesc',
fieldLabel: '<span style="color:red">*</span> 职位描述',
width: 200
}]
}]
},
//字段集合结束1
{
xtype: 'fieldset',
autoHeight: true,
layout: 'column',
title: '对应聘者要求',
bodyStyle: 'padding:15px 15px 0',
//frame:true,
items: [{
columnWidth: .5,
layout: 'form',
items: [{
xtype: 'numberfield',
name: 'requiredExperience',
fieldLabel: '工作年限(年)'
},
{
xtype: 'numberfield',
name: 'requiredMaxAge',
minValue: 15,
maxValue: 60,
value: 60,
allowBlank: false,
fieldLabel: '<span style="color:red">*</span> 年龄上限(岁)'
}]
},
{
columnWidth: .5,
layout: 'form',
items: [getSimpleCombo({
dataMap: theDegreeMap,
fieldLabel: '<span style="color:red">*</span> 学历',
name: 'requiredDegree',
allowBlank: false
}), {
xtype: 'numberfield',
name: 'requiredMinAge',
minValue: 15,
maxValue: 60,
value: 15,
allowBlank: false,
fieldLabel: '<span style="color:red">*</span> 年龄下限(岁)'
}]
}]
}]
});
//将添加修改form加入到添加修改窗口
var inputWindow = new Ext.Window({
frame: true,
width: 760,
height: 480,
autoScroll: true,
title: '详情窗口',
closeAction: 'hide',
//maximizable:true,
//modal: true ,
renderTo: document.body,
items: [inputForm]
});
return {
win: inputWindow,
form: inputForm
};
//inputWindow.show();
}
Ext.onReady(
//主函数 ,页面载入后运行
function () {
Ext.QuickTips.init();
//建立添加修改窗口
var inputWin = createInputWindow();
//列的元数据
var columnMetaData = [
/*
一个元数据开始
*/
{
//json id
dataIndex: 'jobId',
//表头
header: '职位',
//是否排序
sortable: true,
//显示宽度
width: 100
}
/*
一个元数据结束
*/
, {
dataIndex: 'jobTitle',
header: '职位名称',
width: 150,
//是否在表格中显示
show: true,
sortable: true,
renderer: function (val) {
return '<span style="color: red;">' + getLabel(theJobCategories, val) + '</span>';
}
},
{
dataIndex: 'appliedCount',
header: '接收简历数量',
width: 90,
sortable: true,
show: true
},
{
dataIndex: 'repliedCount',
header: '已回复简历数量',
sortable: true,
width: 100
},
{
dataIndex: 'viewedCount',
header: '招聘者查看次数',
width: 90,
sortable: true,
show: true
},
{
dataIndex: 'sendCount',
header: '短信发送数量',
sortable: true,
width: 90,
show: true
},
{
dataIndex: 'issueTime.time',
header: '发布时间',
sortable: true,
width: 150,
show: true,
renderer: function (val) {
var t = new Date();
t.setTime(val)
return '<span style="color: green;">' + t.toLocaleString() + '</span>';
}
},
{
dataIndex: 'jobStatus',
sortable: true,
header: '职位状态',
width: 70,
show: true,
renderer: function (val) {
return theJobStatusMap[val];
}
}];
//表格工具栏
var toolBar = [
/*
一个工具栏元素开始
*/
{
//工具栏名称
text: '修改',
//工具栏图标
iconCls: 'bmenu',
//处理函数
handler: function () {
//得到选中数据
var record = this.getSelectionRecord();
if (!record) {
Ext.MessageBox.alert('注意', '请选择表格数据');
return;
}
//添加修改窗口显示
inputWin.win.show();
//ajax 装载原来数据
inputWin.form.load({
url: 'company_getjob_action.jsp?jobId=' + record.get('jobId'),
success: function (form, action) {
if (!action.result.success) {
Ext.Msg.alert('失败', action.result.data.info);
return;
}
var jobTitle = action.result.data.jobTitle;
if (jobTitle) {
var triggerJob = inputWin.form.getForm().findField("jobTitle");
var textJob = getLabel(theJobCategories, jobTitle);
//alert(textJob);
//alert(jobTitle);
if (textJob != jobTitle) {
triggerJob.setValue(jobTitle);
triggerJob.setRawValue(textJob);
inputWin.form.getForm().findField("jobTitleEx").reset();
}
//自己输入的
else {
triggerJob.reset();
inputWin.form.getForm().findField("jobTitleEx").setValue(jobTitle);;
}
}
var workAddress = action.result.data.workAddress;
if (workAddress) {
var triggerWorkAddress = inputWin.form.getForm().findField("workAddress");
triggerWorkAddress.setValue(workAddress);
triggerWorkAddress.setRawValue(getLabel(theRegions, workAddress));
}
},
waitMsg: '正在读取详情',
waitTitle: '请稍后...',
failure: function (form, action) {
if (action.result && !action.result.success) {
Ext.Msg.alert('失败', action.result.data.info);
return;
}
Ext.Msg.alert('失败', action.response.responseText);
}
});
//window.location = '../company_setjob.jsp?jobId=' + record.get('jobId');
}
}
/*
一个工具栏元素结束
*/
,
//一个分割线
'-', {
text: '删除',
iconCls: 'bdel',
handler: function () {
var record = this.getSelectionRecord();
if (!record) {
Ext.MessageBox.alert('注意', '请选择表格数据');
return;
}
var my_grid = this;
Ext.Msg.confirm(getLabel(theJobCategories, record.get('jobTitle')), '确定删除 该职位 ?', function (btn, text) {
if (btn == 'yes') {
my_grid.loadMask.show();
Ext.Ajax.request({
url: 'company_deljob_action.jsp',
success: function (r) {
my_grid.loadMask.hide();
var result = Ext.decode(r.responseText);
if (result.failure) {
Ext.Msg.alert('注意', result.info);
} else {
my_grid.reload();
}
},
failure: function (r) {
my_grid.loadMask.hide();
Ext.Msg.alert('注意', r.responseText);
},
disableCaching: true,
params: {
jobId: record.get('jobId')
}
});
}
});
}
},
//一个分割线
'-', {
text: '该职位人才',
iconCls: 'blist',
handler: function () {
var record = this.getSelectionRecord();
if (!record) {
Ext.MessageBox.alert('注意', '请选择表格数据');
return;
}
var my_grid = this;
window.location = '../rcss.jsp?jobCategory=' + record.get('jobTitle');
}
},
//一个分割线
'-', {
text: '发布新职位',
iconCls: 'badd',
handler: function () {
//清除原来添加修改窗口数据
inputWin.form.getForm().reset();
//显示
inputWin.win.show();
// window.location = '../company_newjob.jsp';
}
}];
//右键菜单
var contextMenu = [toolBar[0], toolBar[2], toolBar[4], '-', toolBar[6]];
var doubleClickAction = toolBar[0].handler;
//表格对象
var grid = new Ext.ux.yiminghe.JsonGridPanel({
//列元数据
columns: columnMetaData,
//每页显示条数
pageSize: 10,
//表格宽度
width: 700,
//数据源json页面
dataSource: 'company_jobs_action.jsp',
//表格名字
title: '职位列表 , 您目前可用的招才币为 ' + MONEY + ' 枚!',
//页面div的名字
renderTo: 'grid-example',
//单选
single: true,
//显示控制
autoHeight: true,
viewConfig: {
forceFit: true
},
//选择模型
sm: new Ext.grid.RowSelectionModel({
singleSelect: this.single
}),
//表格工具栏
tbar: toolBar,
//右键菜单
rowcontextmenu: contextMenu,
//双击操作
doubleClick: doubleClickAction
});
grid.reload();
inputWin.form.underGrid = grid;
});
JsonGrid自己简单封装了一下
Ext.namespace("Ext.ux.yiminghe")
Ext.ux.yiminghe.JsonGridPanel = function (config) {
var fieldsData = [];
for (var i = 0; i < config.columns.length; i++) {
fieldsData.push(config.columns[i].dataIndex);
}
//config.baseParams=config.baseParams||[];
//config.baseParams.limit=config.pageSize;
//config.baseParams.start=0;
config.store = new Ext.data.JsonStore({
url: config.dataSource,
//读取数据的url,
root: 'rows',
baseParams: config.baseParams,
totalProperty: 'results',
//总共的页数
fields: fieldsData
});
config.store.lastOptions = {
params: {
start: 0,
limit: config.pageSize
}
};
var columns = [new Ext.grid.RowNumberer()];
for (var i = 0; i < config.columns.length; i++) {
if (config.columns[i].show) columns.push(config.columns[i]);
}
config.columns = columns;
Ext.ux.yiminghe.JsonGridPanel.addMenuScope(config.tbar, this);
config.store.on('loadexception', function (othis, options, response, e) {
Ext.Msg.alert('注意', response.responseText);
return;
});
if (config.dynamicFunc) config.store.on('beforeload', function (store_, options) {
config.dynamicFunc(options.params);
});
if (config.maskAll) config.loadMask = new Ext.LoadMask(Ext.getBody(), {
msg: "加载中..."
});
else config.loadMask = true;
config.bbar = new Ext.PagingToolbar({ //在grid底层添加分页工具栏
pageSize: config.pageSize,
//显示的数据条数
store: config.store,
//选择翻页按钮时的加载到grid的数据
displayInfo: true,
items: config.pageBarItems,
plugins: config.pageBarPlugins
}) //end bbar;
Ext.apply(this, config);
Ext.ux.yiminghe.JsonGridPanel.superclass.constructor.call(this);
if (this.doubleClick) {
this.on('rowdblclick', function (grid2, rowIndex, ee) {
var store = grid2.getStore();
var oRecord = store.getAt(rowIndex);
this.doubleClick(oRecord);
ee.stopEvent();
},
this);
}
if (this.rowcontextmenu) {
Ext.ux.yiminghe.JsonGridPanel.addMenuScope(this.rowcontextmenu, this);
this.on('rowcontextmenu', function (grid2, rowIndex, ee) {
if (!this.rowcontextmenu_) {
this.rowcontextmenu_ = Ext.menu.MenuMgr.get({
id: 'rowcontextmenu_-ctx',
items: this.rowcontextmenu
});
}
this.getSelectionModel().selectRow(rowIndex);
this.rowcontextmenu_.showAt(ee.getXY());
ee.preventDefault();
},
this);
}
}
Ext.extend(Ext.ux.yiminghe.JsonGridPanel, Ext.grid.GridPanel, {
getSelectionRecords: function () {
if (this.getSelectionModel().getSelections()[0]) return this.getSelectionModel().getSelections();
else {
// alert('请选择数据!');
return false;
}
},
reload: function () {
this.getStore().reload();
},
getSelectionRecord: function () {
if (this.getSelectionModel().getSelections()[0]) return this.getSelectionModel().getSelections()[0];
else {
// alert('请选择数据!');
return false;
}
}
});
Ext.ux.yiminghe.JsonGridPanel.addMenuScope = function (obj, sco) {
if (!obj) return;
for (var i = 0; i < obj.length; i++) {
if (typeof obj[i] == 'string') continue;
obj[i].scope = sco;
if (obj[i].menu) yiminghe.dynamicTable.addMenuScope(obj[i].menu, sco);
}
}其中的单选树,一个panel中套两个树,可以分级选择
if (!yiminghe) var yiminghe = {};
yiminghe.checkedTreeSingle = function (config) {
this.checks_ = {};
this.level = config.level || 0;
this.div = config.div || '';
this.title = config.title || '';
this.expandable = config.expandable || false;
this.expandedInitial = config.expandedInitial || true;
this.singleExpand = config.expandable || false;
this.singleClickExpand = config.singleClickExpand || true;
this.allowEmpty = config.allowEmpty || false;
this.clickFunc = config.clickFunc;
this.tree = new Ext.tree.TreePanel({
useArrows: true,
autoScroll: true,
// rootVisible:false,
style: 'text-align:left',
animate: false,
width: config.width,
height: config.height,
//enableDD:true,
//containerScroll: true,
singleExpand: this.singleExpand
});
this.root = new Ext.tree.TreeNode({
text: this.title,
draggable: false,
expanded: this.expandedInitial,
singleClickExpand: this.singleClickExpand,
id: yiminghe.checkedTreeSingle.getId()
});
//alert(config.height);
this.setDataArray(config.dataArray);
this.tree.setRootNode(this.root);
// render the tree
//this.tree.render();
//if (this.root.firstChild)
//this.tree.expandPath(this.root.firstChild.getPath());
};
yiminghe.checkedTreeSingle.getId = function () {
/*var id = 1;
return function() {
return id++;
};*/
return -9999;
};
yiminghe.checkedTreeSingle.prototype = {
setDataArray: function (newArray, render) {
var childNodes = this.root.childNodes.slice(0);
for (var i = 0; i < childNodes.length; i++) {
this.root.removeChild(childNodes[i]);
}
if (this.allowEmpty) {
var child = new Ext.tree.TreeNode({
text: '不限制',
draggable: false,
singleClickExpand: true,
id: 'he_empty'
});
if (this.clickFunc) child.on('click', this.clickFunc);
this.root.appendChild(child);
}
this.append(newArray, this.root, this.clickFunc);
if (render) {
this.tree.getSelectionModel().clearSelections();
this.root.expand();
}
},
getSelectionNode: function () {
return this.tree.getSelectionModel().getSelectedNode();
},
getExtTree: function () {
return this.tree;
},
hide: function () {
this.isV = false;
this.tree.hide();
},
isVisible: function () {
this.isV;
},
show: function () {
this.isV = true;
this.tree.show();
},
render: function () {
if (this.div) this.tree.render(this.div);
},
append: function (a_array, node, func, level) {
level = level || 0;
if (level > this.level) return;
if (a_array) for (var i = 0; i < a_array.length; i++) {
var child = new Ext.tree.TreeNode({
text: a_array[i].label,
draggable: false,
expandable: this.expandable,
singleClickExpand: this.singleClickExpand,
id: a_array[i].key
});
if (this.clickFunc) child.on('click', this.clickFunc);
node.appendChild(child);
if (a_array[i].children) this.append(a_array[i].children, child, func, level + 1);
}
},
destroy: function () {
this.checks_ = {};
this.tree.body.update('');
}
};
本文介绍了一个使用JavaScript实现的增删改查示例,通过封装工具函数和创建表单及表格组件来展示职位信息的管理流程。涉及Ext框架的使用,包括组合框、窗口、表单字段和表格组件。
816

被折叠的 条评论
为什么被折叠?



