ExtJs教程----Ext.data.Model的Ajax

本文介绍如何使用ExtJS 5框架定义模型、处理表单数据并调用服务器端API保存及读取学生信息。具体步骤包括定义模型、获取表单数据、设置模型值及调用保存方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.定义Model

Ext.define('ExtJs5Example.model.Student', {
    extend: 'Ext.data.Model',
    alias: 'widget.Student',
    fields: [
        { name: 'StudentID', type: 'string' },
        { name: 'Name', type: 'string' },
        { name: 'Age', type: 'string' }
    ],
    proxy: {
        type: 'ajax',
        api: {
            create: '/Home/SaveSudentInfo',//当调用Model.save()方法时会调用这个方法
            read: '/Home/GetStudentInfo'//<span><span class="comment">//在调用Model的静态方法load时,会默认去这里查数据</span><span>  </span></span>
        },
        reader: {
            type: 'json',
            successProperty: 'success',
            rootProperty: 'data',
            totalProperty: 'total',
            messageProperty: 'message'
        },
        listeners: {
            exception: function (proxy, response, operation) {
                Ext.MessageBox.show({
                    title: 'REMOTE EXCEPTION',
                    msg: operation.getError(),
                    icon: Ext.MessageBox.ERROR,
                    buttons: Ext.Msg.OK
                });
            }
        }
    }

});
2.获取form的数据,给Model赋值,调用model的save 方法

  //获取itemId为StudentInfo的form中所有的数据
  var values = Ext.ComponentQuery.query("StudentInfo")[0].getValues();
  //定义model对象
  var record = Ext.create('ExtJs5Example.model.Student', {});
  //将从form中获取的值赋到model对象中
  record.set(values);
  //调用model对象的save方法
  record.save({
                success: function (form, operation) {
                    Ext.MessageBox.show({
                        title: '提示',
                        msg: operation._resultSet.message,
                        icon: Ext.MessageBox.OK,
                        buttons: Ext.Msg.OK
                    });

                },
                failure: function (form, operation) {
                    Ext.Msg.alert('提示', operation._error);
                }
   });


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值