• ObjectStore onFetch方法获取记录总数


    转自:http://blog.csdn.net/earthhour/article/details/38686029

    ObjectStore onFetch方法获取记录总数

    require(['dojo/when']);
    var json = new JsonRest({target: url});
    
        store = new ObjectStore({objectStore: json});
        
        store.onFetch = function(results){
            // 方法一:
        	results.then(function(rs){
        		console.log('rs len->',rs.length);
        	});
            // 方法二:
        	when(results, function(results){
        		console.log('when->',results.length);
        		console.dir(results);
        	});
        };
    

       

    dojo小例子(14)对提供给grid显示的数据进行预处理

    有时我们从后台获取的数据不一定能满足页面显示的需求,比如后台数据中某个字段值是“1/0”,但是显示时我们希望显示为“是/否”。这就需要对数据进行转换,也就是预处理。

    可以有两种方法,假设使用场景是JsonRest+ObjectStore组合为grid提供懒加载数据。

    方法一、对store中的数据进行预处理。

    [javascript] view plaincopy在CODE上查看代码片派生到我的代码片
     
    1. var json = new JsonRest({  
    2.     target: url  
    3. });  
    4. var store = new ObjectStore({objectStore: json});  
    5. store.onFetch = function(results){  
    6.     results.forEach(function(item){  
    7.         // 在这里预处理数据  
    8.         if(item.admin == '1'){  
    9.            item.admin = '是';  
    10.         }else{  
    11.            item.admin = '否';  
    12.         }  
    13.     });  
    14. };  

    方法二、在grid的structure中预处理。

    [javascript] view plaincopy在CODE上查看代码片派生到我的代码片
     
    1. var layout = [  
    2.     [{  
    3.         'name': 'Column 1',  
    4.         'field': 'id',  
    5.         'width': '100px'  
    6.     }, {  
    7.         'name': 'Column 2',  
    8.         'field': 'admin',  
    9.         'width': '100px',  
    10.         get: function(colIndex, item) {  
    11.             // processing data  
    12.             if (item.admin == '1') {  
    13.                 return '是';  
    14.             } else {  
    15.                 return '否';  
    16.             }  
    17.         }  
    18.     }, {  
    19.         'name': 'Column 3',  
    20.         'field': 'desc',  
    21.         'width': '200px'  
    22.     }]  
    23. ];  

    另外,遇到带内部对象的json数据,例如:

    {"name":"陈","age":"24","phone":{"company":"123","home":"456","private":"789"}} 

    我们想把内部对象的属性company显示到grid,如果这样写'field': 'phone.company'肯定行不通,所以也适合用上面提到的两种方法之一进行转换。

    假如采用第二种方法:

    [javascript] view plaincopy在CODE上查看代码片派生到我的代码片
     
    1.    {  
    2.     'name': 'Column 2',  
    3.     'field': 'phone.company',  
    4.     'width': '100px',  
    5.     get: function(colIndex, item) {  
    6.         // processing data  
    7.         return item.phone.company;  
    8.     }  
    9. }  

    2014-5-13更新:

    采用第二种方法时,对grid刷新会不起作用,就是由于get方法导致。代码如下:

    [javascript] view plaincopy在CODE上查看代码片派生到我的代码片
     
    1. Grid.resize();  
    2. Grid._refresh();  

    目前原因不清楚。所以如果需要用到grid刷新,还是采用第一种方式


  • 相关阅读:
    剑指offer51-正则表达式匹配
    剑指offer50-构建乘积数组
    剑指offer49-数组中的重复
    PHP系列笔记——Zend_Controller工作流程
    多态与重载
    读取文件数据的大概流程
    基于HTTP协议下载文件的实现
    C++中的面向对象笔记
    firebreath注册接口
    python读取excelxlsx,写入excel
  • 原文地址:https://www.cnblogs.com/yoyogis/p/4074063.html
Copyright © 2020-2023  润新知