ExtJS-Ext.data.Store采用Ext.data.proxy.Ajax获取数据后并进行处理

本文介绍如何在ExtJS中使用Ext.data.Store配合Ext.data.proxy.Ajax获取及处理后台数据,通过两种方法展示了如何利用extractResponseData进行数据转换。

Ext.data.Store采用Ext.data.proxy.Ajax获取数据后并进行处理

需求:

使用Extjs中的Ext.data.Store时,利用Ext.data.proxy.Ajax的配置来获取后台返回数据,想对返回的响应数据进行二次处理。

解决方法

Ext.data.proxy.Ajax配置中存在一个属性配置 extractResponseData(模板方法,以允许子类指定如何为阅读器获取响应。),参数是response,需要返回处理后的数据

API中截图如下:
Ext.data.proxy.Ajax.extractResponseData
API内容:

extractResponseData ( response ) : Object TEMPLATE PRIVATE
Template method to allow subclasses to specify how to get the response for the reader.
PARAMETERS
response :  Object
The server response
RETURNS :Object
The response data to be used by the reader
This is a template method. a hook into the functionality of this class. Feel free to override it in child classes.

响应参数结构:
在这里插入图片描述

代码内容

写法一:

Ext.create('Ext.data.TreeStore', {
    model: 'treeModel',
    proxy: {
        type: 'ajax',
        method: 'POST',
        url: 'getMenuTreeData.action',
        reader: {
            type: 'json'
        },
        extractResponseData: function(response) {
            // 将响应的数据进行转换,具体的返回数据,可以自行打印响应参数进行查看
            var result = Ext.util.JSON.decode(response.responseText);
            // 将处理后的数据,重新赋值给响应的数据
            response.responseText = Ext.util.JSON.encode(dealMenuTreeData(result))
            // 返回响应数据
            return response
        },
    },
    autoLoad: true
});

写法二

Ext.create('Ext.data.TreeStore', {
    model: 'treeModel',
    proxy: {
        type: 'ajax',
        method: 'POST',
        url: 'getMenuTreeData.action',
        reader: {
            type: 'json'
        },
        extractResponseData: function(response) {
            // 将响应的数据进行转换,具体的返回数据,可以自行打印响应参数进行查看
            var result = Ext.util.JSON.decode(response.responseText);
            // 直接返回处理后的数据,我这里的数据是数组类型
            return dealMenuTreeData(result)
        },
    },
    autoLoad: true
});

参考

  1. https://www.jianshu.com/p/8e17ae3431ed
  2. http://t.zoukankan.com/lcchuguo-p-5207381.html
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值