js函数解读

看源码了。。

XGridDataCellRenderer.prototype.render = function(xgrid, xcell, dom, value, isselected,row, col,dataId){
    var td = dom;
//  if(xcell.valign == "top" || td.style.verticalAlign == "top" || xcell.valign == "bottom" || td.style.verticalAlign == "bottom"){
//      td.style.lineHeight = "normal";
//  }
//  if(!xcell.isHeaderCell && isChrome){//ie ff不支持 td上的position:relative
//      dom.style.position = "relative";
//  }
    var container = dom;
    var content = value;
    this._doc = xgrid._doc || document, cellType = xcell.cellType;
    /* 这里感觉没必要清除掉,后面有innerHTML赋值,会清掉的
    if (!((cellType === XGridCell.TYPE_LOGIC && (xcell.input_mode === '1' || xcell.input_mode === '2'))
            || (xcell.input_type === "code" && (xcell.cmod == 1 || xcell.cmod == 2)))) {
        $(td).empty();//checkbox和radio不清空
    }*/
    var self = this;
    /*var width = XGridDataCellRenderer.getWidth(xcell,xgrid,col);
    var height = XGridDataCellRenderer.getHeight(xcell,xgrid,row);*/
    /*if(isie && height <= 1){//IE9TD有内容时会撑爆
        $(td).parent().css("line-height","0px");
    }*/
    
    var options = xgrid.getConfig()["options"];
    if(cellType == XGridCell.TYPE_INT || cellType == XGridCell.TYPE_FLOAT || cellType == XGridCell.TYPE_STR){
        var reporttype = WebReportHelper.getReportType(xgrid);
        //序号表元
        var indexcell = options["sort_indexcell"];
        if(reporttype == "planar" && xcell.cellName == indexcell){
            if(td.getAttribute("isPlanar")){//变长行
                var indexstart = options["sort_indexcellstart"];
                var rowcount = parseInt(dom.parentNode.rowIndex,10) + parseInt(xgrid.getPlanarInfo("planar_dataStart"),10);
                content = parseInt(rowcount,10) + parseInt(indexstart,10);
            } //else {
                /*
                 * ISSUE IRPT-20889 序号列的合计行值 不应该总是0 应该是设计是填写的值
                 * del by taoxl 20151027
                 */
                //content = 0;//序号所在列的合计行值总为0
            //}
            value = content;
            xgrid._dataStore.setValue(dataId, xcell.cellName, value, {isVirtualData: true});
        }
    }
    if(cellType == XGridCell.TYPE_INT || cellType == XGridCell.TYPE_FLOAT){
        //整型或者浮点
        if(dataIsNotNullObj(value) && typeof(value) == "number"){
            if(xgrid._webTask.getTaskInfo("notShowZero") && value == 0){
                content = "";
            }else{
                //如果数值原本就是科学计数法显示,就不多做处理
                if(content.toString().search(XGridCellRenderer.r_EM) == -1){
                    //var floatlength = xcell.cellScale ? xcell.cellScale : 0;
                    //rpt_needadjust 用来记录该表是否参与舍位平衡
                    var rpt_needadjust = options["needadjust"];
                    var floatlength = XGridDataCellRenderer.getCellScale(xgrid._webTask, xcell,rpt_needadjust);
                    content = parseFloat(content).toFixed(floatlength); 
                    if(cellType == XGridCell.TYPE_FLOAT && parseFloat(content)> 1 && content.toString().length > 17 ){
                        //加上小数点和小数,长度超过17位,就进行科学计数法,不做千分为显示
                        content = self._float2scientific(parseFloat(content),15);
                    }else{
                        var _Havesprt = xcell.havesprt == "true"?true : false;
                        if(_Havesprt){
                            content = self._formatHavesprt(content);
                        }
                    }
                }else{
                    content = content.toString().replace(XGridCellRenderer.r_EM,"E");
                    content =  content.replace(XGridCellRenderer.r_FLOAT, function($0, $1){
                        return parseFloat($0).toFixed(15);
                    });
                }
                content = XGridDataCellRenderer.preAndSuffix(content,xcell.prefix,xcell.suffix);
                content = self._othercodemult(xgrid,td,xcell.cellName,content);
            }
        }
//      XGridDataCellRenderer.resetCellColor(dom, options["numcolor"]);
    } else if(cellType == XGridCell.TYPE_STR) {//把字符串的判断挪到前面来,更快匹配多数表元
        // 字符
        if(xcell.input_type == "code"){//代码组 
            if(xcell.cmod == 1 || xcell.cmod == 2){
                container = null;
                var height = XGridDataCellRenderer.getHeight(xcell,xgrid,row);
                var inputtype = ["","checkbox","radio"];
                /**
                 * IRPT-20701 数据被锁定了,多选表元还可以编辑
                 * 基本表的dataId为空,同一任务两张相同的报表对应表元的radio的typename是相同的
                 * 导致xradio的click事件执行两次
                 * typename加上xgrid._classid
                 */
                /**
                 * IRPT-21257打开填报任务时,变长表的第一行数据显示结果不对 aol 20151130
                 * 切换报表户rowid变了,导致得不到radio的dom
                 */
//              var typename = inputtype[xcell.cmod] + xcell.cellName+(dataId?dataId:"") + xgrid._classid;
                var typename = inputtype[xcell.cmod] + xcell.cellName+(dom.parentNode.rowIndex?dom.parentNode.rowIndex:"") + xgrid._classid;
                if(xgrid._islinkreport){
                    typename = "link_" + typename;
                }
                if(WebReportHelper.isPlanarReport(xgrid)){
                    typename = "planar_" + typename;
                }
                if(!dom.xradio){
                    /*$(td).empty();*/
                    var tempDiv = self._createTempDiv(height,"100%",td);
                    var containerTd = self._createOneCell(tempDiv);
                    containerTd.style.textAlign = xcell.align;
                    containerTd.style.verticalAlign = "middle";
                    var url = com.esen.i.js.webreport.WebTask.ACTION_URL;//"/i/webreportservice.do";
                    var params = new Map();
                    params.put("service","codegroup");
                    params.put("action","listChild");
                    params.put("rid","---");
                    var taskid = xgrid.getOption("taskid");
                    params.put("taskid",taskid);
                    params.put("codegroup",xcell.cnm);//代码组名称
                    params.put("types","0");
                    params.put("level","10");
                    params.put("onlyacceptleaf",xcell.cleaf);//只允许选择叶子节点
                    params.put("codeindex",xcell.cidx);//代码文字显示序号,0是主文字,以此类推
                    
                    if(!dataIsNotNullObj(value)){//空
                        content = "";
                    }
                    dom.xradio = new XRadio(xcell,content,url,params,typename,dataId,dom,xgrid);
                    containerTd.innerHTML = dom.xradio.radioText;
                    /**
                     * IRPT-20633web填报:变长表中的代码单选框和多选框在输入时行宽变得很宽
                     * 创建完radio后,需要重新设置样式
                     */
                    XGridDataCellRenderer.resetRadioMargin(dom);
                    if(xcell.input_box_button_arrange){
                        containerTd.style.textAlign = "left";
                    }
                }else{
                    var objs = $("input[name="+typename+"]");
                    for(var i=0;i<objs.length;i++){
                        objs[i].checked=false;
                        var obj = objs[i];
                        if(dataIsNotNullObj(value) && value){
                            for(var j=0;j<content.length;j++){
                                if(content[j].code == obj.value){
                                    obj.checked=true;
                                    break;
                                }
                            }
                        }
                    }
                    td.style.height = height + "px";
                    self._setDIVHeight(td,height+"px");
                    /*if(dom._lockimg){
                        dom.removeChild(dom._lockimg);
                    }*/
                }
                var tempText = [];
                var valueLength = value ? value.length : 0;
                for(var i=0;i<valueLength;i++){
                    var code = value[i].code;
                    var text = value[i].text;
                    tempText[i] = self._getformatcode(code,text,xcell.cfmt);
                }
                content = tempText.join();
                /*if(height <= 0){
                    $(td).empty();
                }*/
            }else{
                if(dataIsNotNullObj(value) && value){
                    var tempText = [];
                    if(!isString(content)){//代码组可能是“#err002”,为字符串
                        for(var i=0;i<content.length;i++){
                            var code = content[i].code;
                            var text = content[i].text;
                            tempText[i] = self._getformatcode(code,text,xcell.cfmt);
                        }
                    }else{
                        tempText.push(content);
                    }
                    if(xgrid.getOption("codecellindentstr")){
                        if(xgrid.getOption("d2hzcodecellname") == xcell.cellName){
                            tempText[0] = self._codemult(xgrid,td,xcell.cellName,tempText[0],value);
                        }else{
                            tempText[0] = self._othercodemult(xgrid,td,xcell.cellName,tempText[0]);
                        }
                    }
                    content = tempText.join();
                    content = XGridDataCellRenderer.preAndSuffix(content,xcell.prefix,xcell.suffix);
                }
            }
        }else{
            if(dataIsNotNullObj(value) && value){
                content += "";
                content = XGridDataCellRenderer.textformat(content);
                content = XGridDataCellRenderer.preAndSuffix(content,xcell.prefix,xcell.suffix);
                content = self._othercodemult(xgrid,td,xcell.cellName,content);
            }
            /**
             * IRPT-19031 remove by chenf 2015-7-17
             * 表元配色方案会直接将该颜色值作用到表元上,不需要重新如此调用,否则会使得重新设置的表元颜色失效
             */
//          XGridDataCellRenderer.resetCellColor(dom, options["charcell"]);
        }
    } else if(cellType == XGridCell.TYPE_TIME){
        //时间
        if(dataIsNotNullObj(value) && value){
            content = self._formatTime(content);
            content = XGridDataCellRenderer.preAndSuffix(content,xcell.prefix,xcell.suffix);
        }
    }else if(cellType == XGridCell.TYPE_DATE){
        //日期
        if(dataIsNotNullObj(value) && value){
            content = self._formatDate(content,xcell.dateformat);
            content = XGridDataCellRenderer.preAndSuffix(content,xcell.prefix,xcell.suffix);
            content = self._othercodemult(xgrid,td,xcell.cellName,content);
        }
//      XGridDataCellRenderer.resetCellColor(dom, options["datecell"]);
    }else if(cellType == XGridCell.TYPE_LOGIC){//逻辑型
        var input_mode = xcell.input_mode, lfm = xcell.logicformat;
        if (!isNaN(lfm)) lfm = self._getLogicFormat(lfm);
        if (input_mode === "1") {// Checkbox
            container = null;
            if (!dom.checkbox) {
                dom.innerHTML = '<label style="white-space: nowrap; word-wrap: normal; word-break: normal;"><input type="checkbox" style="vertical-align: baseline;" />' + lfm + '</lable>';
                dom.checkbox = dom.firstChild.firstChild;
                $(dom).bind("click", { grid : xgrid, cell : xcell, dataid : dataId || '' }, function(evt) {
                    if (evt.target === this) return;
                    var data = evt.data, grid = data["grid"], cell = data["cell"], dataid = data["dataid"], checkbox = this.checkbox, ck = checkbox.checked;
                    if (!XGridCellEditor.checkXCellCanEdit(cell, this, grid, false, dataid)) {
                        checkbox.checked = !ck;
                    } else if (parseBool(grid.getValue(xcell.cellName, dataid), false) !== ck) {
                        var undoAction = new EnterCellValueAction(grid, cell, this, ck, dataid);
                        if (UndoableAction.checkCanUndo(grid, cell)) grid._undoManager.putAction(undoAction);
                        undoAction.doAction();
                    }
                });
            }
            if (value !== '') dom.checkbox.checked = value;
        } else if (input_mode === "2") {// Radio
            container = null;
            if (!dom.radios) {
                var lfms = lfm.split(','), rname = xgrid._classid + xcell.cellName + (dataId || '');
                dom.innerHTML = '<label style="margin-right: 10px; white-space: nowrap; word-wrap: normal; word-break: normal;"><input type="radio" style="vertical-align: baseline;" name="'
                    + rname + '" />' + lfms[0] + '</label><label><input type="radio" style="vertical-align: baseline;" name="' + rname + '" />' + lfms[1] + '</label>';
                dom.radios = [ dom.firstChild.firstChild, dom.lastChild.firstChild ];
                $(dom).bind("click", { grid : xgrid, cell : xcell, dataid : dataId || '' }, function(evt) {
                    if (evt.target === this) return;
                    var data = evt.data, grid = data["grid"], cell = data["cell"], dataid = data["dataid"], value = grid.getValue(cell.cellName, dataid);
                    var radios = this.radios;
                    if (XGridCellEditor.checkXCellCanEdit(cell, this, grid, false, dataid)) {
                        var ck = radios[0].checked;
                        if (ck === parseBool(value, false)) return;
                        var undoAction = new EnterCellValueAction(grid, cell, this, ck, dataid);
                        if (UndoableAction.checkCanUndo(grid, cell)) grid._undoManager.putAction(undoAction);
                        undoAction.doAction();
                    } else if (value === true || value === false) {
                        radios[ value ? 0 : 1 ].checked = true;
                    } else {
                        radios[ radios[0].checked ? 0 : 1 ].checked = false;
                    }
                });
            }
            if (value === true || value === false) {
                dom.radios[ value ? 0 : 1 ].checked = true;
            } else {
                dom.radios[ dom.radios[0].checked ? 0 : 1 ].checked = false;
            }
        } else if(value !== '' && dataIsNotNullObj(value)) {
            content = self._formatLogic(content,xcell.logicformat);
            content = XGridDataCellRenderer.preAndSuffix(content,xcell.prefix,xcell.suffix);
            content = self._othercodemult(xgrid,td,xcell.cellName,content);
        }
//      XGridDataCellRenderer.resetCellColor(dom, options["logiccell"]);
    }else if(cellType == XGridCell.TYPE_MEMO){
        //备注
        if(dataIsNotNullObj(value) && value){
            var width = XGridDataCellRenderer.getWidth(xcell,xgrid,col);
            var height = XGridDataCellRenderer.getHeight(xcell,xgrid,row);
            content = XGridDataCellRenderer.textformat(content);
            content = XGridDataCellRenderer.preAndSuffix(content,xcell.prefix,xcell.suffix);
            var tempDiv = self._createTempDiv(height,width,td);
            tempDiv.style.width = width + 'px';
            var tempTd = self._createOneCell(tempDiv,width,height);
            tempTd.style.textAlign = xcell.align;
            tempTd.style.verticalAlign = xcell.valign;
            
            //控制自动换行
            tempTd.style.wordBreak = "break-all"; 
            tempTd.style.wordWrap = "break-word";
            container = tempTd;
            /*if(height <= 1){
                $(td).empty();
            }*/
        }
    } else if (cellType == XGridCell.TYPE_IMAGE) {
        // 图片
        container = null;
        var width = XGridDataCellRenderer.getWidth(xcell,xgrid,col);
        var height= XGridDataCellRenderer.getHeight(xcell,xgrid,row);
        var tempDiv = self._createTempDiv(height, width, td);
        if (value) {
            var containerDiv = tempDiv;
            if (xcell.dispstyle == "1") {
                containerDiv = self._createOneCell(tempDiv);
            }
            var imgObj = new Image();
            var cal = function() {
                if (xcell.dispstyle == "1") {// 自适应
                    // if(imgObj.offsetWidth == 0 && imgObj.offsetHeight == 0) return;
                    if (imgObj.offsetWidth > width) {
                        imgObj.height = Math.floor((width * imgObj.offsetHeight) / imgObj.offsetWidth);
                        imgObj.width = width;
                    }
                    if (imgObj.offsetHeight > height) {
                        imgObj.width = Math.floor((height * imgObj.offsetWidth) / imgObj.offsetHeight);
                        imgObj.height = height;
                    }
                    containerDiv.style.verticalAlign = "middle";
                    containerDiv.style.textAlign = "center";
                }
                if (xcell.dispstyle == "2") {// 拉伸
                    imgObj.width = width;
                    imgObj.height = height;
                }
                imgObj.style.visibility = "visible";
                imgObj.border = "";
            };
            imgObj.style.visibility = "hidden";
            imgObj.style.position = "relative";
            containerDiv.appendChild(imgObj);
            imgObj.onload = cal;// IRPT-16665 IE8下onload事件要在src赋值之前注册,否则无法触发
            imgObj.src = com.esen.i.js.webreport.WebTask.ACTION_URL + "?" + content.url + "&filename=" + content.filename;
            xcell.dispstyle = xcell.dispstyle ? xcell.dispstyle : "1";
            /*
             * $(imgObj).live("load",cal); $(imgObj).bind("load",cal);
             */
            self._deleteImg(width, xgrid, xcell, dom, dataId, tempDiv, value);
            content = value.filename;
            /*if (height <= 1) {
                $(td).empty();
            }*/
        } else {
            tempDiv.innerHTML = '<img style="width:0;height:100%;vertical-align:middle" /><img style="vertical-align:middle" src="../ei/webreport/js/xgrid/image/addimage.png" />';
            var imgObj = tempDiv.lastChild;
            if (width < XGridDataCellRenderer.ADDIMAGE_WIDTH || height < XGridDataCellRenderer.ADDIMAGE_HEIGHT) {
                imgObj.width = width > height ? height : width;
                imgObj.height = width > height ? height : width;
            }
            tempDiv.style.cssText += '; text-align: center; vertical-align: middle;';
        }
        XGridDataCellRenderer.resetCellColor(td, "#F9F9F9", false);
    }else if(cellType == XGridCell.TYPE_ATTACHMENT){
        // 附件
        container = null;
        var width = XGridDataCellRenderer.getWidth(xcell,xgrid,col);
        var height = XGridDataCellRenderer.getHeight(xcell,xgrid,row);
        var tempDiv = self._createTempDiv(height,width,td);
        tempDiv.style.textAlign = "center";
        if (value) {
            var nameFile = content.filename || "";
            var fileSuffix = nameFile.substring(nameFile.lastIndexOf(".") + 1);
            fileSuffix = fileSuffix.toUpperCase();
            fileSuffix = self._getfileSuffixName(fileSuffix);
            var imgObj = self._doc.createElement("img");
            imgObj.src = "../ei/webreport/js/xgrid/image/fileImage/" + fileSuffix + ".gif";
            var marginTop = (height - XGridDataCellRenderer.ADDATTACH_WIDTH) / 2;
            imgObj.style.margin = (marginTop > 0 ? marginTop : 4) + "px 0px 0px 2px";
            tempDiv.appendChild(imgObj);
            tempDiv.innerHTML += "<br/>" + nameFile;
            tempDiv.style.lineHeight = "100%";
            self._deleteImg(width, xgrid, xcell, dom, dataId, tempDiv, value);
            content = value.filename;
            /*if (height <= 1) {
                $(td).empty();
            }*/
        } else {
            tempDiv.innerHTML = '<img style="width:0;height:100%;vertical-align:middle" /><img style="vertical-align:middle" src="../ei/webreport/js/xgrid/image/addattach.png" />';
            var imgObj = tempDiv.lastChild;
            if (width < XGridDataCellRenderer.ADDIMAGE_WIDTH || height < XGridDataCellRenderer.ADDIMAGE_HEIGHT) {
                imgObj.width = width > height ? height : width;
                imgObj.height = width > height ? height : width;
            }
            tempDiv.style.cssText += '; text-align: center; vertical-align: middle;';
        }
        XGridDataCellRenderer.resetCellColor(td, "#F9F9F9", false);
    } else if(cellType == XGridCell.TYPE_OLE){
        //ole不支持
        content = "不支持ole类型";
        container = null;
    }else{
        //任意类型,非法类型
        content = "非法类型";
        container = null;
    }
    if(!dataIsNotNullObj(value)){
        content = "";
    }
    //IRPT-15648 仅在不允许在线填报时不显示底纹,其他只读情况需要渲染
    if (xgrid._webTask.getTaskInfo("allowonlinefill") != false) {
        this._calcAndRender(xgrid,xcell,dom,dataId);
    }
    if(this._calcNonVisibleText(xgrid,xcell,dom)){
        /**
         * IRPT-19462 edit chenf 2015.08.11
         * 文本表头中的宏取表元值,取得是表元的displayValue,
         * 这里content,是已经textformat处理过的结果,而计算文本表头中的宏时也会对值进行了textformat处理,
         * 所以这里将值放入displayValue前需要对值进行一次unHTML处理
         * content在逻辑型表元中不为String时没有unHTML方法
         */
        dom.setAttribute("displayValue", content != null ? isString(content) ? content.unHTML().replace(/\&nbsp;/g, ' ') : content : '');
        if (container) {
            if (!!xcell.linkurl_value && dom.getAttribute("cellName") == xcell.cellName && this._needShowLinkUrl(xgrid, xcell)) {
                XGridDataCellRenderer.linkrender(xcell.linkurl_value, container, content, value, td, xgrid);// url链接
            } else {
                if (/*xcell.textwrap && */content) {
                    content = XGridTextCellRenderer.DEFAULT_CONTAINER_HTML.replace(/\${VALIGN}/g, xcell.valign).replace(/\${CONTENT}/, content).replace(/\${TEXTDECORATION}/, getCurrentStyle(container, "text-decoration"));
                    //if (ieVersion <= 8) container.style.cssText += ';position: relative; vertical-align: top;';
                    /**
                     * IRPT-22079IE8套打显示异常
                     */
                    if(ieVersion <= 8){
                        if(container.tagName == "DIV" && xcell.isHeaderCell){
                            container.style.cssText += ';position: absolute; vertical-align: top;';
                        }else{
                            container.style.cssText += ';position: relative; vertical-align: top;';
                        }
                    }
                }
                container.innerHTML = content || "";
                /**
                 * 通过innerHTML赋值之后,需要appendChild(),否则后面的removeChild()会报错
                 */
                if(dom._lockimg){
                    dom.appendChild(dom._lockimg);
                }
            }
        }
        dom.style.cssText += xcell.textwrap ? '; white-space: normal; word-wrap: break-word; word-break: break-all;' : '; white-space: nowrap; word-wrap: normal; word-break: normal;';
    }
    //小计行的黄色背景色
    if(xgrid.isRow4XJ){
        if (xgrid.isRow4XJ(td)) {
            addClassName(td, CLASSNAME_XJ, true);
            dom.style.backgroundColor = dom.backgroundColor = ''; // IRPT-18999 // 多级合计行不设置背景色
        } else {
            removeClassName(td, CLASSNAME_XJ, true);
        }
    }
    /*if(dom._lockimg){
        dom.appendChild(dom._lockimg);
    }*/
    if(xgrid._webTask.getTaskInfo("qbe") !== xgrid.getOption("reportName") && xcell["zzExp"]){
        if(!td.getAttribute("canedit")) return;
        if( xgrid._webTask.getTaskInfo("closeExp")){
            if (!td.style.backgroundImage) {
                td.style.backgroundImage = "url(../ei/webreport/js/xgrid/image/null.gif)";
            }
        }else{
            var cancalc = td.getAttribute("cancalc");
            if (cancalc == "") {
                td.setAttribute("backimg", "");
                return; //cancalc为空表示使用的是公式的斜线样式
            }
            /**
             * IRPT-18447  edit by ligl on 2015.07.28
             * 只有处于舍位平衡状态或舍位平衡数据集下且该表参与舍位平衡这种情况,该表元不显示表元斜线样式
             */
            if((xgrid._webTask.getTaskInfo("swph") || xgrid._webTask._dataConfig["ds"]=="SWPH")&&xgrid.getConfig()["options"]["needadjust"]){
                if (!td.style.backgroundImage) {
                    td.style.backgroundImage = "url(../ei/webreport/js/xgrid/image/null.gif)";
                }
            }else{
                if (!td.style.backgroundImage) {
                    td.style.backgroundImage = cancalc?"url(../ei/webreport/js/xgrid/image/null.gif)" :"";
                }
            }
        }
        td.setAttribute("backimg", td.style.backgroundImage);
    }
};
/**

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

扫码关注云+社区

领取腾讯云代金券