日期:2014-05-16  浏览次数:20330 次

javascript封装ext js grid渲染函数
 *模拟map
 */
Map = function(){
    var mapAddM = {
        /**
         * entry函数
         * @param {Object} key
         * @param {Object} val
         */
        entry: function(key, val, flag){
            this.key = key;
            this.value = val;
            this.flag = flag;
        },
        //put方法
        put: function(key, val, flag){
            this.store[this.store.length] = new this.entry(key, val, flag);
        },
        //get方法
        get: function(key){
            for (var i = 0; i < this.store.length; i++) {
                if (this.store[i].key === key) 
                    return this.store[i].value;
            }
        },
        //get方法
        getFlag: function(key){
            for (var i = 0; i < this.store.length; i++) {
                if (this.store[i].key === key) 
                    return this.store[i].flag;
            }
        },
        //remove方法
        remove: function(key){
            for (var i = 0; i < this.store.length; i++) {
                this.store[i].key === key && this.store.splice(i, 1);
            }
        },
        //keyset
        keySet: function(){
            var keyset = new Array;
            for (var i = 0; i < this.store.length; i++) 
                keyset.push(this.store[i].key);
            return keyset;
        },
        //valset
        valSet: function(){
            var valSet = new Array;
            for (var i = 0; i < this.store.length; i++) 
                valSet.push(this.store[i].value);
            return valSet;
        },
        //flagSet
        flagSet: function(){
            var flagSet = new Array;
            for (var i = 0; i < this.store.length; i++) 
                flagSet.push(this.store[i].flag);
            return flagSet;
        },
        //clear
        clear: function(){
            this.store.length = 0;
        },
        //size 
        size: function(){
            return this.store.length;
        },
        /**
         *  迭代子
         */
        iterator: function(){
            //TODO 待实现
            var obj = this.keySet();//所有的key集合
            var idx = 0;
            var me = {
                /**
                 * 当前key
                 */
                current: function(){
                    return obj[idx - 1];
                },
                /**
                 * 第一个key
                 */
                first: function(){
                    return obj[0];
                },
                /**
                 * 最后一个key
                 */
                last: function(){
                    return obj[obj.length - 1];
                },
                /**
                 * 是否还有下一个元素
                 */
                hasNext: function(){
                    idx++;
                    if (idx > obj.length || null == obj[obj.length - 1]) 
                        return false;
                    return true;
                }
            };
            return me;
        }
    };
    for (var method in mapAddM) {
        this.store = new Array;
        Map.prototype[method] = mapAddM[method];
    }
}

/**
 * 生成column
 * @param {Object} map
 */
function createColumns(map){
    var columns = [];
    var i = 0;
    for (var it = map.iterator(); it.hasNext();) {
        var currentKey = it.current();//本次循环的key
        var currentVal = map.get(currentKey);//当前value
        var currentFlag = map.getFlag(currentKey);//判断是否隐藏该列
        var columni = {};
        columni.dataIndex = currentKey;
        columni.header = currentVal;
        columni.sortable = true;
        columni.width = 115;
        if ((currentFlag && (currentFlag == "hide"))) {
            columni.hidden = true;
        }
        columns.push(columni);
        i++;
    }
    return columns;
}

/**
 * 生成keyset集合
 * @param {Object} map
 */
function createReaderColum