global.practice.js
11.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/// <reference path="jquery-1.11.1.min.js" />
/// <reference path="jquery-easyui-1.4.1/jquery.easyui.min.js" />
/// <reference path="global.modelctls.js" />
/// <reference path="global.js" />
function PracticeDialog(options) {
this._options = options || {};
this._callback = this._options.callback;
this._userId = this._options.userId;
this._pagination = false;
this._dgPractice = null;
this._menu = null;
this._dialog = null;
this._initialize = function (dgPractice, menu) {
var thispd = this;
$(menu).menu({
onClick: function (item) {
if (item.text == '添加') {
thispd.addPractice();
} else if (item.text == '编辑') {
thispd.editPractice();
} else if (item.text == '删除') {
thispd.removePractice();
}
}
});
var data_json = new Array();
$.ajax({
type: "POST",
url: CONF_BACK_SERVERURL + global.modelctls.practice.list,
headers:{
"token":$.cookie('ftoken')
},
data:{userId: thispd._userId },
dataType: 'json',
async:"false",
success:function (result) {
for(var i=0;i<result.length;i++){
var data_row = {content:result[i].content,id:result[i].id};
data_json = {rows:data_row}
}
console.log(result);
}
});
$(dgPractice).datagrid({
idField: 'id', fit: true, fitColumns: true, showHeader: true, singleSelect: true, nowrap: false,
border: false, checkOnSelect: false, selectOnCheck: false, scrollbarSize: 0, rownumbers: true,
pagination: thispd._pagination, pageNumber: 1, pageSize: 20, pageList: [10, 20, 50], striped: true,
method:"GET",
url: CONF_BACK_SERVERURL + global.modelctls.practice.list,
header:[{key:'token',value:$.cookie('token')}],
queryParams: {userId:thispd._userId},
columns: [[{
field: 'content', title: '内容', width: 100, formatter: function (value, row, index) {
var text = row.content;
return '<span title="' + text + '">' + text + '</span>';
}
}, {
field: 'id', title: '操作', width: 80, fixed: true, formatter: function (value, row, index) {
var tmp = '<a onclick="{0}(\'{1}\',\'{2}\')" href="#">{3}</a>';
var deleteStr = global.strfmt(tmp, 'window._practiceDialog.removePractice', row.id, index, '删除');
var editStr = global.strfmt(tmp, 'window._practiceDialog.editPractice', row.id, index, '编辑');
return editStr + ' ' + deleteStr;
}
}]],
onDblClickRow: function (rowIndex, rowData) {
$(thispd._dialog).dialog('close');
if (thispd._callback) {
thispd._callback(rowData);
}
},
onHeaderContextMenu: function (e, field) {
e.preventDefault();
var item = $(menu).menu('findItem', '删除');
$(menu).menu('hideItem', item.target);
item = $(menu).menu('findItem', '编辑');
$(menu).menu('hideItem', item.target);
$(menu).menu('show', {
left: e.pageX,
top: e.pageY
});
},
onRowContextMenu: function (e, rowIndex, rowData) {
e.preventDefault();
var item = $(menu).menu('findItem', '删除');
$(menu).menu('showItem', item.target);
item = $(menu).menu('findItem', '编辑');
$(menu).menu('showItem', item.target);
$(this).datagrid('selectRow', rowIndex);
$(menu).menu('show', {
left: e.pageX,
top: e.pageY
});
}
});
};
this.showDialog = function () {
var thispd = this;
var dgPractice = $('<table/>');
var menu = $('<div></div>').append('<div data-options="name:\'add\',iconCls:\'icon-add\'">添加</div>')
.append('<div data-options="name:\'add\',iconCls:\'icon-edit\'">编辑</div>')
.append('<div data-options="name:\'add\',iconCls:\'icon-remove\'">删除</div>');
var tools = $('<div/>');
var btnAdd = $('<a href="javascript:void(0)" style="float:left"></a>').appendTo(tools);
var btnOk = $('<a href="javascript:void(0)"></a>').appendTo(tools);
var btnCancel = $('<a href="javascript:void(0)"></a>').appendTo(tools);
thispd._dgPractice = dgPractice;
thispd._menu = menu;
window._practiceDialog = thispd;
var dialog = thispd._dialog = $('<div style="padding-left:0px;padding-top:0px;"/>').append(dgPractice).append(menu).dialog({
buttons: tools,
title: '选择习惯用语', width: 450, height: 300, modal: true, onOpen: function () {
thispd._initialize(dgPractice, menu);
$(btnAdd).linkbutton({
iconCls: 'icon-add', text: '添加',
onClick: function () { thispd.addPractice(); }
});
$(btnCancel).linkbutton({
iconCls: 'icon-cancel', text: '取消',
onClick: function () { $(dialog).dialog('close'); }
});
$(btnOk).linkbutton({
iconCls: 'icon-ok', text: '确定',
onClick: function () {
var row = $(dgPractice).datagrid('getSelected');
if (!row) {
$.messager.alert('提示', '请选择习惯用语');
} else {
$(dialog).dialog('close');
if (thispd._callback) {
thispd._callback(row);
}
}
}
});
},
onClose: function () {
thispd._dialog = null;
delete window._practiceDialog;
$(this).dialog('destroy');
}
});
};
this.removePractice = function (id, index) {
var thispd = this;
var datagrid = thispd._dgPractice;
if (id) $(datagrid).datagrid('selectRecord', id);
var row = $(datagrid).datagrid('getSelected');
$.messager.confirm('确认', '您确认想要删除记录吗?', function (r) {
if (!r) return;
$.post(global.contextPath + global.modelctls.practice.remove, { id: row.id }, function (result) {
if (result.status == 'ok') {
thispd.loadPractice();
} else {
console.error(result);
$.messager.alert('错误', '删除失败!');
}
}).error(function (e) {
console.error(e);
$.messager.alert('错误', '发送Http请求失败!');
});
});
};
this.editPractice = function (id, index) {
var thispd = this;
var datagrid = thispd._dgPractice;
if (id) $(datagrid).datagrid('selectRecord', id);
var row = $(datagrid).datagrid('getSelected');
var input = $('<input />');
var dialog = $('<div style="padding-left:20px;padding-top:20px;"/>').append(input).dialog({
title: '编辑习惯用语', width: 330, height: 230, modal: true,
onOpen: function () {
$(input).textbox({
height: 120,
width: 280,
multiline: true,
value: row.content,
});
}, onClose: function () { $(this).dialog('destroy'); },
buttons: [{
text: '保存', iconCls: 'icon-ok', handler: function () {
row.content = $(input).textbox('getValue');
if (row.content.trim() == '') {
$.messager.alert('提示', '请输入内容!');
return;
}
$.post(global.contextPath + global.modelctls.practice.save, row, function (result) {
if (result.status == 'ok') {
thispd.loadPractice();
$(dialog).dialog('close');
} else {
console.error(result);
$.messager.alert('错误', '保存失败!');
}
}).error(function (e) {
console.error(e);
$.messager.alert('错误', '发送Http请求失败!');
});
}
}, {
text: '取消', iconCls: 'icon-cancel', handler: function () { $(dialog).dialog('close'); }
}]
});
};
this.addPractice = function addPractice() {
var thispd = this;
var input = $('<input />');
var dialog = $('<div style="padding-left:20px;padding-top:20px;"/>').append(input).dialog({
title: '添加习惯用语', width: 330, height: 230, modal: true,
onOpen: function () {
$(input).textbox({
height: 120,
width: 280,
multiline: true
});
}, onClose: function () { $(this).dialog('destroy'); },
buttons: [{
text: '添加', iconCls: 'icon-ok', handler: function () {
var content = $(input).textbox('getValue');
if (content.trim() == '') {
$.messager.alert('提示', '请输入内容!');
return;
}
/*$.post(global.contextPath + global.modelctls.practice.save, { content: content, userId: thispd._userId }, function (result) {
if (result.status == 'ok') {
thispd.loadPractice();
$(dialog).dialog('close');
} else {
console.error(result);
$.messager.alert('错误', '保存失败!');
}
}).error(function (e) {
console.error(e);
$.messager.alert('错误', '发送Http请求失败!');
});*/
$.ajax({
type: "POST",
url: CONF_BACK_SERVERURL + global.modelctls.practice.save,
headers:{
"token":$.cookie('ftoken')
},
data:{ content: content, userId: thispd._userId },
dataType: 'json',
success:function (result) {
if (result.status == 'ok') {
thispd.loadPractice();
$(dialog).dialog('close');
} else {
console.error(result);
$.messager.alert('错误', '保存失败!');
}
}
});
}
}, {
text: '取消', iconCls: 'icon-cancel', handler: function () { $(dialog).dialog('close'); }
}]
});
};
this.loadPractice = function () {
var thispd = this;
var datagrid = thispd._dgPractice;
$(datagrid).datagrid('reload');
};
}