hello world!!!!!

写下自己的一些心得,写下自己问题的方式,写下程序之路的艰辛,希望能够有朝一日成为大牛。
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

前端规范性开发

Posted on 2016-11-07 10:23  陈力  阅读(292)  评论(0编辑  收藏  举报

 

 

 

 

 

一:datatable

datatable 手动加载

workTableApi = IQCBase.initManualDT({
tableID: workTableId,
areaName: area,
target: controller,
perfix: container,
queryPanel: '#' + from,
queryObjectPerfix: queryPerfix,
//detailUrl: url + '/ItemDetail',
scrollX: true,
scrollY: '80',
columns: [
{
data: 'ID', title: '操作', width: '30px', className: 'text-center', render: function (data, type, full, meta) {
var lnk = '<a href="javascript:void(0)" data-name="ID" data-id="{2}" data-row="{1}" title="{3}">{0}</a>';
var text = '删除';
return IQCBase.format(lnk, text, meta.row, data || '', text);
}
},
],
onDraw: function (e, settings, json) {
$workTable.find('input[data-type=num]').numeric({ 'decimalPlaces': 2, 'negative': false });
var lnkObjects = $workTable.find('tbody tr a[data-name="ID"]');
lnkObjects.off('click').on('click', function () {
var $this = $(this);
delRow($this);
});
},
onInit: function () {
var id = $('#@(Perfix)EditId').val();// $form.find('input[name=txtID]').val();


if (id) {
$.post('/IQC/OQCInspectionSampleV3/Query2WipItems', { ID: id }, function (data) {
if (data && data.length > 0) {
lineNumber = data.length + 1;
items = data;
//for(var i=0;i<data.length;i++)
//{
// data[i].ITEM_SEQ=i;
//}
IQCBase.addRows(workTableApi, data, true);
inputs = $workTable.find('.keys').get();
}
});
}
}

});

datatable 自动加载

自定配置加载datatables,这样可以灵活增加列,或者删减列

var @(Perfix)table = IQCBase.initAutoDT({
areaName: 'IQC',
target: 'OQCInspectionSample',
perfix: '@(Perfix)',
queryPanel: '#@(Perfix)QueryPanel',
queryObjectPerfix: 'qr',
scrollX: true,
scrollY: iContentH,
processing: false,
autoBindLink: true,
serverMethod: "POST",
columns: [
IQCBase.checkColumn,
IQCBase.snColumn,
{
data: "BARCODE", title: '机身条码', render: function (data, type, full, meta) {
var data = data || '';
return '<a href="javascript:void(0)" onclick="OpenTab(\'/IQC/OQCInspectionSampleV2/Detail?ID=' + full.ID + '\',\'成品抽检检验维护(' + data + ')\')">' + data + '</a>';
}
},


],
onDraw: function (e, settings, json) {

},
onXHR: function (e, settings, json, xhr) {
if (json.data.length > 0) {
document.getElementById('@(Perfix)Excel').removeAttribute('disabled');
}
else {
document.getElementById('@(Perfix)Excel').setAttribute('disabled', 'disabled');
}
}
});

 

  

 

二:
时间选择加载时分
提交时询问
判断是否为空,或者是否为NULL,判断是否是数值类型

 //share property & method
    var pt = IQCBase.prototype;
    pt.UEConfig = { toolbars: [['bold', 'justifyleft', 'justifyright', 'justifycenter', 'justifyjustify', 'inserttable', 'subscript', 'superscript', 'fontfamily']] };
    pt.UEConfigDisable = { toolbars: [], wordCount: false, readonly: false };
    pt.DateTimePickerCfg = {
        language: 'zh-CN',
        weekStart: 1,
        minView: 2,
        todayBtn: 1,
        autoclose: 1,
        todayHighlight: 1,
        startView: 2,
        forceParse: 1,
        showMeridian: 1
    };
    pt.DateTimePickerCfg2 = {
        language: 'zh-CN',
        weekStart: 1,
        minView: 0,
        todayBtn: 1,
        autoclose: 1,
        todayHighlight: 1,
        startView: 2,
        forceParse: 1,
        showMeridian: 1
    };
    //格式化日期
    //expl: formatDate("2010-04-30", "yyyy-MM-dd HH:mm:ss")
    pt.formatDate = function formatDate(date, format) {
        if (!date) return;
        if (!format) format = "yyyy-MM-dd";
        switch (typeof date) {
            case "string":
                date = new Date(date.replace(/-/, "/"));
                break;
            case "number":
                date = new Date(date);
                break;
        }
        if (!date instanceof Date) return;
        var dict = {
            "yyyy": date.getFullYear(),
            "M": date.getMonth() + 1,
            "d": date.getDate(),
            "H": date.getHours(),
            "m": date.getMinutes(),
            "s": date.getSeconds(),
            "MM": ("" + (date.getMonth() + 101)).substr(1),
            "dd": ("" + (date.getDate() + 100)).substr(1),
            "HH": ("" + (date.getHours() + 100)).substr(1),
            "mm": ("" + (date.getMinutes() + 100)).substr(1),
            "ss": ("" + (date.getSeconds() + 100)).substr(1)
        };
        return format.replace(/(yyyy|MM?|dd?|HH?|ss?|mm?)/g, function () {
            return dict[arguments[0]];
        });
    }
    pt.format = function (format) {
        var args = pt.toArray(arguments, 1);
        return format.replace(/\{(\d+)\}/g, function (m, i) {
            return args[i];
        });
    };
    pt.toArray = function (iterable, start, end) {
        if (!iterable || !iterable.length) { return []; }
        var array = [], i;
        start = start || 0;
        end = end ? ((end < 0) ? iterable.length + end : end) : iterable.length;
        for (i = start; i < end; i++) {
            array.push(iterable[i]);
        }
        return array;
    };
    pt.htmlEncode = function (value) {
        var entities = {
            '&': '&',
            '>': '>',
            '<': '<',
            '"': '"'
        }, keys = [], p, regex;

        for (p in entities) {
            keys.push(p);
        }

        regex = new RegExp('(' + keys.join('|') + ')', 'g');

        return (!value) ? value : String(value).replace(regex, function (match, capture) {
            return entities[capture];
        });
    };
    pt.getDate = function (val) {
        var date = eval("new " + val.substr(1, val.length - 2));
        return date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate() + ' ' + date.getHours() + ':' + date.getMinutes() + ':' + date.getSeconds();
    };
    pt._radomNum = {};
    pt.getRadomNum = function () {
        var b;
        while (true) {
            b = Math.random().toString().substr(2, 7) / 100;
            if (b.toString().length === 8 && pt._radomNum[b] === undefined) {
                return pt._radomNum[b] = b;
            }
        }
    };
    pt.parseFloat = function (v) {
        var pre = parseFloat(v).toPrecision(14);
        return parseFloat(pre);
    };
    pt.max = function (array) {
        var max = array[0], i, ln, item;
        for (i = 0, ln = array.length; i < ln; i++) {
            item = array[i];
            if (item > max) { max = item; }
        }
        return max;
    };
    pt.min = function (array) {
        var min = array[0], i, ln, item;
        for (i = 0, ln = array.length; i < ln; i++) {
            item = array[i];
            if (item < min) {
                min = item;
            }
        }
        return min;
    };
    pt.getMaxMin = function () {
        var max = pt.max(arguments),
            min = pt.min(arguments);
        return [max, min];
    };
    pt.supportsSort = (function () {
        var a = [1, 2, 3, 4, 5].sort(function () { return 0; });
        return a[0] === 1 && a[1] === 2 && a[2] === 3 && a[3] === 4 && a[4] === 5;
    }());
    pt.sort = function (array, sortFn) {
        var length = array.length, i = 0, comparison, j, min, tmp;
        for (; i < length; i++) {
            min = i;
            for (j = i + 1; j < length; j++) {
                if (sortFn) {
                    comparison = sortFn(array[j], array[min]);
                    if (comparison < 0) { min = j; }
                }
                else if (array[j] < array[min]) { min = j; }
            }
            if (min !== i) {
                tmp = array[i];
                array[i] = array[min];
                array[min] = tmp;
            }
        }
        return array;
    };
    pt.avg = function () {
        var arr = arguments, len = arguments.length, sum = 0;
        for (var i = 0; i < len; i++) {
            if ($.isNumeric(arr[i])) { sum += parseFloat(arr[i]); }
        }
        return IQCBase.parseFloat(sum / 2);
    };
    pt.isNull = function () {
        var arr = arguments, result = true;
        for (var i = 0; i < arr.length; i++) {
            var v = arr[i];
            result &= pt.isEmpty(v);
        }
        return result;
    };
    pt.isEmpty = function (value) {
        return (value === null) || (value === undefined) || (value === '');
    };
    pt.isNumeric = function () {
        var arr = arguments, result = true;
        for (var i = 0; i < arr.length; i++) {
            result &= $.isNumeric(arr[i]);
        }
        return result;
    };

    pt.getUrlParam = function (name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
        var r = window.location.search.substr(1).match(reg);
        if (r != null) return unescape(r[2]); return null;
    };
    pt.leftPad = function (string, size, character) {
        var result = String(string);
        character = character || " ";
        while (result.length < size) {
            result = character + result;
        }
        return result;
    };
    pt.now = function () {
        var date = new Date();
        return date.getFullYear() + "-"
            + pt.leftPad(date.getMonth() + 1, 2, '0') + "-"
            + pt.leftPad(date.getDate(), 2, '0') + " "
            + pt.leftPad(date.getHours(), 2, '0') + ":"
            + pt.leftPad(date.getMinutes(), 2, '0') + ":"
            + pt.leftPad(date.getSeconds(), 2, '0');
    };
    pt.appendValue = function (obj, val, splitChar) {
        var spChar = splitChar || ',';
        var currentVal = obj.val(), currentArray = [];
        if (currentVal && currentVal.length > 0)
            currentArray = currentVal.split(',');
        if (currentVal.indexOf(val) == -1)
            currentArray.push(val);
        obj.val(currentArray.join(','));
    };
    pt.getDateFormat = function (value, rec) {
        return eval("new " + value.substr(1, value.length - 2)).toLocaleDateString();
    };

   

  

 

三:
弹出对话框
弹出查询对话框

 //提示信息
    pt.confirm = function (text, cb) {
        var initTemplate = function () {
            if ($('#IQCBase_ConfirmModal').length == 1)
                return true;
            $('body').append(' \
<div id="IQCBase_ConfirmModal" class="modal fade" data-backdrop="static" tabindex="-1" role="dialog" aria-hidden="true"> \
    <div class="modal-dialog">\
        <div class="modal-content">\
            <div class="modal-header">\
                <a class="close" id="close" data-dismiss="modal">×</a>\
                <h3 class="modal-title">系统提示</h3>\
            </div>\
            <div class="modal-body">\
                <p style="font-size:1.2em"></p>\
            </div>\
            <div class="modal-footer">\
                <button class="btn btn-primary" data-dismiss="modal" aria-hidden="true" data-keyup="enter">确定</button> \
                <button class="btn btn-danger" data-dismiss="modal" aria-hidden="true">取消</button> \
            </div>\
        </div>\
    </div>\
</div>');
        }
        var bootStrapConfirm = function () {
            if (!$.fn.modal.Constructor)
                return false;

            $('body').off('click', '#IQCBase_ConfirmModal .btn-primary');
            $('body').off('click', '#IQCBase_ConfirmModal .btn-danger');

            function confirm() { cb(true); }
            function deny() { cb(false); }

            $('body').on('click', '#IQCBase_ConfirmModal .btn-primary', confirm);
            $('body').on('click', '#IQCBase_ConfirmModal .btn-danger', deny);

            return true;
        }
        initTemplate()
        if (bootStrapConfirm()) {
            $('#IQCBase_ConfirmModal .modal-body p').text(text);
            $('#IQCBase_ConfirmModal').modal();
        } else {
            alert('bootstrap was not found');
        }
    };
    pt.showMsg = function (content, title, cb) {
        if (!$.fn.modal.Constructor) {
            alert('bootstrap was not found');
            return false;
        }
        var initTemplate = function () {
            if ($('#IQCBase_AlertModal').length == 1)
                return true;
            $('body').append(' \
<div id="IQCBase_AlertModal" class="modal fade" data-backdrop="static" tabindex="-1" role="dialog" aria-hidden="true">\
    <div class="modal-dialog">\
        <div class="modal-content">\
            <div class="modal-header">\
                <a class="close" id="close" data-dismiss="modal">×</a>\
                <h3 class="modal-title">提示</h3>\
            </div>\
            <div class="modal-body">\
                <p style="font-size:1.2em"></p>\
            </div>\
            <div class="modal-footer">\
                <button class="btn btn-primary" data-keyup="enter" data-dismiss="modal">确认</button>\
            </div>\
        </div>\
    </div>\
</div>');
        }


四:弹出选择对话框


五:
统一获取查询参数
统一获取提交参数

 pt.getQueryParams = function (parent, queryObjectPerfix, d, ableOrder) {
        var returnParam = { param: {}, paramIsNull: false },
            formTextData = $(parent).find("[name^='" + queryObjectPerfix + "'][type!='checkbox'][type!='radio']"),
            formCheckData = $(parent).find("input[name^='" + queryObjectPerfix + "']:checked");
        if (d) {
            if (!ableOrder) returnParam.param = { start: d.start, length: d.length };
            else returnParam.param = { start: d.start, length: d.length, order: d.order, columns: d.columns };
        }
        for (var i = 0; i < formTextData.length; i++) {
            var e = formTextData[i];
            var val = $(e).val();
            if (val) {
                returnParam.param[e.name.replace(queryObjectPerfix, '')] = val.toString();
                returnParam.paramIsNull |= val.toString().length > 0;
            }
        }
        //处理Checkbox
        var checkboxNames = [];
        formCheckData.each(function (i, e) {
            var name = e.name.replace(queryObjectPerfix, '');
            if (!returnParam.param[name]) {
                returnParam.param[name] = [];
                checkboxNames.push(name);
            }
            returnParam.param[name].push($(e).val());
        });
        $.each(checkboxNames, function (i, name) {
            returnParam.param[name] = returnParam.param[name].join(',');
        });

        return returnParam;
    };
    pt.getSubmitData = function (parent) {
        var param = {};
        var formData = parent.find("input[type!='checkbox'][type!='radio'][name^='txt']");
        for (var i = 0; i < formData.length; i++) {
            var e = formData[i]; var oe = $(e);
            var required = oe.attr('data-required');
            if (required && oe.val().length < 1) {
                pt.showMsg(required);
                return null;
            }
            param[e.name.replace('txt', '')] = $(e).val();
        }
        //处理Checkbox
        var formCheckData = parent.find("input[type='checkbox'][name^='txt']:checked");
        var checkboxNames = [];
        formCheckData.each(function (i, e) {
            var name = e.name.replace('txt', '');
            if (!param[name]) {
                param[name] = [];
                checkboxNames.push(name);
            }
            param[name].push($(e).val());
        });
        $.each(checkboxNames, function (i, name) {
            param[name] = param[name].join(',');
        });
        //处理Radio
        var formRadioData = parent.find("input[type='radio'][name^='txt']:checked");
        formRadioData.each(function (i, e) {
            var name = e.name.replace('txt', '');
            param[name] = $(e).val();
        });
        //处理select
        var formSelectData = parent.find("select[name^='txt']");
        for (var i = 0; i < formSelectData.length; i++) {
            var e = formSelectData[i]; var oe = $(e);
            var required = oe.attr('data-required');
            if (required && oe.val().length < 1) {
                pt.showMsg(required);
                return null;
            }
            var name = e.name.replace('txt', '');
            param[name] = $(e).val();
        }
        //处理textarea
        var formTextareaData = parent.find("textarea[name^='txt']:not([data-notset])");
        for (var i = 0; i < formTextareaData.length; i++) {
            var e = formTextareaData[i]; var oe = $(e);
            var required = oe.attr('data-required');
            if (required && oe.val().length < 1) {
                pt.showMsg(required);
                return null;
            }
            param[e.name.replace('txt', '')] = $(e).val();
        }
        //处理UEditor
        //var formTextareaData = parent.find("textarea[name^='txt'][data-notset]");
        //for (var i = 0; i < formTextareaData.length; i++) {
        //    var e = formTextareaData[i]; var oe = $(e);
        //    console.log(e);
        //    var required = oe.attr('data-required');
        //    if (required && oe.val().length < 1) {
        //        pt.showMsg(required);
        //        return null;
        //    }
        //    var name = e.name.replace('txt', '');
        //    param[name] = $(e).val();
        //}
        return param;
    };