首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用directjngine在ext-js中无限滚动

使用directjngine在ext-js中无限滚动
EN

Stack Overflow用户
提问于 2012-12-21 17:03:22
回答 2查看 695关注 0票数 2

首先,我请求你对我宽宏大量:我只是一个可怜的ext-js和javascript新手……

我正在努力适应我对ext-js中的infinite scroll grid example的需求。我在sencha ext-js中定义了以下存储:

代码语言:javascript
复制
Ext.define('AM.store.M2MResources', {
    extend: 'Ext.data.Store',
    requires: 'Ext.direct.*', //  use ext-direct for binding
    model: 'AM.model.M2MResource',
    autoLoad: true,

    paramsAsHash:false,
    autoSave: false,

    pageSize: 25,
    // allow the grid to interact with the paging scroller by buffering
    buffered: true,

    proxy: {
        type: 'direct',
        api: {
            // create  : undefined,
            read    : M2MResourceServlet.getM2Mresources,
            // update  : undefined,
            // destroy : undefined 
        },      
        extraParams: {
              total: 100
        },
        root: 'data',
        fields: [{name:'label'}, {name:'address_unique'}, {name:'address_network'}, {name:'status'}],
        paramOrder: 'page, start, limit',
        reader: {       
            type: 'json',           
            root: 'data',
            successProperty: 'success',
            totalProperty: 'totalCount'
        },
        writer: new Ext.data.JsonWriter({
            encode: false,
            writeAllFields: true,
            listful: true
        }
        )
    }
});

使用此directJNgine服务器副本:

代码语言:javascript
复制
public class M2MResourceServlet {
    public static class ResourceData{       
        public String label;
        public String address_unique;
        public String address_network;
        public String status;
    }

    public static class DirectStoreResult{
        ResourceData[] data;
        public int totalCount;
        public boolean success = true;

        public DirectStoreResult(ResourceData[] data, int totalCount) {
            super();
            this.data = data;
            this.totalCount = totalCount;
        }
    }

    static int counter = 0;
    @DirectMethod
    public DirectStoreResult getM2Mresources( Integer page, Integer start, Integer limit ) {
        ArrayList<ResourceData> result = new ArrayList<ResourceData>();

        // fill result list...

        DirectStoreResult r = new DirectStoreResult(result.toArray(new ResourceData []{}),limit);
        return r;
    }
}

然后我指定了以下模型:

代码语言:javascript
复制
Ext.define('AM.model.M2MResource', {
    extend: 'Ext.data.Model',
    fields: [
        {name:'label' },
        {name:'address_unique' },
        {name:'address_network' },
        {name:'status' }
     ]
});

因此,我想将数据加载到下面的网格中:

代码语言:javascript
复制
Ext.define('AM.view.resgrid.M2MResources', {
    extend: 'Ext.grid.Panel',
    alias: 'widget.m2mresources',

    title: 'All Resources Here',    

    verticalScrollerType: 'paginggridscroller',

    store: 'M2MResources',

  initComponent: function() {

      console.log('initComponent function in View has been called...');

      this.columns = [
                      {header: 'friendly name',dataIndex: 'label',flex: 1, sortable: true},
                      {header: 'address unique', dataIndex: 'address_unique',flex: 1},
                      {header: 'address network', dataIndex: 'address_network', flex: 1},
                      {header: 'node status',dataIndex: 'status',flex: 1}
                      ];

      this.callParent(arguments);

      Ext.getStore('M2MResources').guaranteeRange(0, 100);
  }
});

不幸的是,该存储似乎从未加载过数据:网格面板显示“正在加载...”不显示符号和数据项。我怀疑问题出在我的直接方法返回的json中。在客户端,我得到了以下json数据记录:

你能建议一下如何解决这个问题吗?谢谢!

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-12-28 01:54:19

好的,这个问题的合理答案可以在here找到。基本上,服务器端直接方法的形式必须是

代码语言:javascript
复制
@DirectMethod
public PartialResult<ResourceData> getM2Mresources(int start, int limit) {
        ResourceData[] m2mResources = ...
    //...
        return new PartialResult<ResourceData>(Arrays.asList(m2mResources), 
    m2mResources.length);
}

其中,PartialResult类如下所示:

代码语言:javascript
复制
public class PartialResult<T> {

    /** the partial result set */
    private Collection<T> result;

    /** the size of the complete result set */
    private int total;

    public Collection<T> getResult() {
       return this.result;
    }

    public int getTotal() {
       return this.total;
    }

    public void setResult(Collection<T> result) {
       this.result = result;
    }

    public void setTotal(int total) {
       this.total = total;
    }

    public PartialResult(Collection<T> result, int total) {
       super();
       this.result = result;
       this.total = total;
    }

}

我在ResourceData类中添加了一些getter,就像在原始示例中一样。

js存储是

代码语言:javascript
复制
Ext.define('AM.store.M2MResources', {
    extend: 'Ext.data.DirectStore',
    requires: 'Ext.direct.*', //  use ext-direct for binding
    model: 'AM.model.M2MResource',

    autoLoad: true,
    buffered: true,
    pageSize: 20,
    purgePageCount: 0,
    leadingBufferZone: 60,
    trailingBufferZone: 0,

    constructor : function(config) {
        config = config || {};

        config.proxy = {
            type: 'direct',
            directFn : M2MResourceServlet.getM2Mresources,
            reader: {
                type: 'json',
                root: 'result'
            },
            paramOrder: 'start,limit',
            paramsAsHash: false
        };

        this.callParent([config]);
    },

    toString: function() {
        return 'M2MResources: count = ' + this.getCount() + ', totalCount = ' 
                + this.getTotalCount();
    }
});

显然,这正是我想要做的。所以谢谢你亚当·戴维斯!无论如何,如果有人能解释为什么必须使用directFn元素而不是api元素,我将不胜感激……

票数 0
EN

Stack Overflow用户

发布于 2012-12-22 02:29:25

响应数据与您的读卡器配置不匹配。您已经为您的阅读器配置了一个数据根目录:‘,但我在响应中看到的是没有数据节点。您显示的展开节点是:根:'result.data'。我也看不到属性successPropertytotalProperty

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13987091

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档