Skip to content
Browse files

添加图片更新功能

  • Loading branch information...
1 parent 879ec01 commit 6378eec9fbfb80778d00a166a8d9b8c8cc1374af @mouse-lin committed Mar 23, 2012
View
17 app/controllers/students_controller.rb
@@ -18,9 +18,26 @@ def student_total_score
render_json Student.find(params[:s_id]).total_score_by_grade
end
+ #删除学生
def destroy_student
Student.find(params[:id]).delete
render_json "success"
end
+ #保存学生更新
+ def update_student
+ data = {
+ :number => params["number"],
+ :sex => params["sex"],
+ :phone => params["phone"],
+ :home => params["home"],
+ :name => params["name"],
+ }
+ data["image"] = params["photo"] if(params["photo"])
+ Student.find(params["id"]).update_attributes!(data)
+ render_json "success", "text/html"
+ rescue => e
+ render_error e.message,'text/html'
+ end
+
end
View
5 app/models/person.rb
@@ -1,4 +1,7 @@
class Person < ActiveRecord::Base
+ has_attached_file :image,
+ :default_url => "/images/Temp.png",
+ :styles => { :medium => "300x300>",
+ :thumb => "100x100>" }
belongs_to :classes
-
end
View
1 app/views/layouts/application.html.erb
@@ -44,6 +44,7 @@
)
}
);
+ Ext.Ajax.extraParams = { authenticity_token: "<%= form_authenticity_token %>" };
</script>
<div class="dock" id="dock2">
<div class="dock-container2">
View
1 config/routes.rb
@@ -21,6 +21,7 @@
get "get_all_students"
get "student_total_score"
post "destroy_student"
+ post "update_student"
end
member do
post "update_student_comment"
View
70 public/javascripts/PF/pages/settings_index.js
@@ -5,8 +5,9 @@ Pf.settings.homeIndex = {
var studentDetailFormPanel = this.createStudentDetail();
grid.store.load();
grid.on('cellclick', function(grid, rowIndex){
- studentDetailFormPanel.getForm().setValues(grid.store.getAt(rowIndex).data);
- $("#image img").attr("src", '/images/Temp.png');
+ var record = grid.store.getAt(rowIndex).data;
+ studentDetailFormPanel.getForm().setValues(record);
+ $("#image img").attr("src",record["image/url"] );
});
var panel = new Ext.TabPanel({
@@ -56,7 +57,6 @@ Pf.settings.homeIndex = {
createStudentDetail: function(){
var sexData = [['',''],['','']];
var sexCombo = new Ext.form.ComboBox({
- //id: 'targetCombo',
valueField: 'sex',
fieldLabel: "性别",
triggerAction: 'all',
@@ -68,19 +68,69 @@ Pf.settings.homeIndex = {
store: new Ext.data.SimpleStore({
fields: ['sex', 'sexName'],
data: sexData,
- //autoLoad: true,
})
});
+
+ //保存更新按钮
+ function onSave(){
+ var grid = Ext.getCmp("studentShowGrid");
+ var record = grid.getSelectionModel().getSelected();
+ if(!record){ Ext.Msg.alert("提示","请选择学生") }
+ else{
+ Ext.Msg.confirm("提示","是否确定保存更改",function(button){
+ if(button != "no"){
+ Ext.getCmp("studentDetailFormPanel").getForm().submit({
+ clientValidation: true,
+ method: "POST",
+ waitMsg: "保存中",
+ params: { id: record.data.id },
+ url: "/students/update_student",
+ success: function(form,action){
+ Ext.Msg.alert('提示', "保存成功");
+ grid.store.on("load",function(){
+ var new_record = Ext.getCmp("studentShowGrid").getSelectionModel().getSelected();
+ if(new_record)
+ {
+ $("#image img").attr("src",new_record.data["image/url"] );
+ }
+ });
+ grid.store.reload();
+ Ext.getCmp("photoField").reset();
+ },
+ failure: function(){
+ Ext.Msg.alert('提示', "保存失败");
+ }
+ });
+ }
+ })
+ }
+ };
+
+ //判断上传文件
+ function onFileSelect(field){
+ var value = field.getValue();
+ var pattern = /\.jpg$|\.jpeg$|\.bmp$|\.png|\.gif$|\.png&/i;
+ if (!pattern.test(value)){
+ Ext.Msg.show({
+ title:"警告",
+ msg:"必须是JPG,GIF,PNG,BMP格式图片文件!",
+ buttons: Ext.MessageBox.OK ,
+ icon: Ext.MessageBox.INFO
+ });
+ field.setValue("");
+ };
+ };
var formPanel = new Ext.form.FormPanel({
title: "test",
region: "center",
+ id: "studentDetailFormPanel",
autoScroll : true,
fileUpload: true,
frame: true,
labelAlign : 'right',
buttonAlign: 'center',
- buttons: [{ text: "更新", handler: function(){ alert("nihai") } }],
+ buttons: [{ text: "更新", handler: function(){ onSave() } }],
items: [{
layout: 'column',
items:[
@@ -115,8 +165,6 @@ Pf.settings.homeIndex = {
name: "classes/name"
},
sexCombo,
- // fieldLabel: "性别",
- // name: "sex"
{
fieldLabel: "联系电话",
name: "phone"
@@ -130,13 +178,13 @@ Pf.settings.homeIndex = {
xtype: 'fileuploadfield',
allowBlank : true,
emptyText: '选择',
+ id: "photoField",
fieldLabel: '头像',
- name: 'size_sheet',
+ name: 'photo',
buttonText: '上传',
- listeners: { 'fileselected' : {fn: this.onFileSelect, scope: this} }
+ listeners: { 'fileselected' : {fn: onFileSelect, scope: this} }
}
- ]
- }
+ ]}
]
}]
});
View
3 public/javascripts/PF/pages/student.js
@@ -31,6 +31,7 @@ Pf.classes.student = Ext.extend(Ext.grid.EditorGridPanel,{
"phone",
"home",
"classes/name",
+ "image/url",
],
root: 'root',
url: '/students/get_all_students.json',
@@ -67,7 +68,7 @@ Pf.classes.student = Ext.extend(Ext.grid.EditorGridPanel,{
var record = grid.getSelectionModel().getSelected();
if(!record){ Ext.Msg.alert("提示","请选择学生") }
else{
- Ext.Msg.confirm('提示', "是否确定保存?", function(button){
+ Ext.Msg.confirm('提示', "是否确定删除?", function(button){
if(button != "no"){
Pf.util.loadMask.show();
var studentId = record.id;

0 comments on commit 6378eec

Please sign in to comment.
Something went wrong with that request. Please try again.