all files / dui/src/tpls/ table-tpl.js

96.15% Statements 25/26
66.67% Branches 8/12
100% Functions 5/5
96.15% Lines 25/26
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89                                                    22×     22× 22×         22× 22× 22× 22× 22×               22× 45× 15×                     22×       22× 45× 45×           22×       22×        
/**
 * 原始table模板
 *
 * @file table模板,用原始的tr、td布局
 * @author Brian Li(lbxxlht@163.com)
 *
 * 数据结构
 * <Field>
 *  {
 *      width: {number},    // 列宽度
 *      title: {string},    // 列标题
 *      field: {string},     // 列域名称
 *      renderer: {Function} // 列渲染函数
 *  }
 */
var define = typeof define === 'function' && define.amd ? define : function (factory) {
    typeof module === 'object' ? (module.exports = factory(require)) : '';
};
 
define(function (require) {
 
    var util = require('../core/util');
 
    /**
     * 输出接口
     *
     * @param {Object} param 表格配置源
     * @param {Array.<Field>} param.fields 列配置源
     * @return {Array.<string>} 表格模板
     */
    function tpl(param) {
        Iif (param == null) {
            return [''];
        }
        calcColumnWidth(param.fields);
        var result = [
            '<div class="table-content-box">',
            '<div class="table-content-scrollbox">',
            '<table class="table-content" cellspacing="0" cellpadding="0">'
        ];
        result = result.concat(produceRow(param.fields));
        result.push('</table>');
        result.push('</div>');
        result.push('</div>');
        return result;
    }
 
    /**
     * 根据field配置,进行column的栅格化
     *
     * @param {Array.<Field>} field 列配置源
     */
    function calcColumnWidth(field) {
        for (var n = 0; n < field.length; n++) {
            if (!isNaN(field[n].width)) {
                field[n].width = util.calcLatticeSize(field[n].width);
            }
        }
    }
 
    /**
     * 生成行模板
     *
     * @param {Array.<Field>} field 列配置源
     * @return {Array.<string>} 行模板
     */
    function produceRow(field) {
        var result = [
            '{{for(var n=0;n<it.length;n++){var item=it[n];}}',
            '<tr data-ui-row="{{=n}}" class="tr">'
        ];
        for (var n = 0; n < field.length; n++) {
            var width = field[n].width ? ' width' + field[n].width : '';
            result = result.concat([
                '<td data-ui-row="{{=n}}" data-ui-column="' + n + '" class="td' + width + '">',
                '{{=item[' + n + ']}}',
                '</td>'
            ]);
        }
        result = result.concat([
            '</tr>',
            '{{}}}'
        ]);
        return result;
    }
 
    return tpl;
});