jquery.easyui.extension.js 11.3 KB
/*
  说明:扩展EasyUI的验证规则
*/
$.extend($.fn.validatebox.defaults.rules, {
    //验证身份证
    idcard: {
        validator: function(value) {
            return /^\d{15}(\d{2}[A-Za-z0-9])?$/i.test(value);
        },
        message: '身份证号码格式不正确'
    },
    //最小长度
    minLength: {
        validator: function(value, param) {
            return value.length >= param[0];
        },
        message: '请输入至少{0}个字符.'
    },
    maxLength: {
        validator: function(value, param) {
            return value.length <= param[0];
        },
        message: '最多只能输入{0}个字符'
    },
    phone: { // 验证电话号码 
        validator: function(value) {
            return /^((\(\d{2,3}\))|(\d{3}\-))?(\(0\d{2,3}\)|0\d{2,3}-)?[1-9]\d{6,7}(\-\d{1,4})?$/i.test(value);
        },
        message: '格式不正确,请使用下面格式:020-88888888'
    },
    mobile: { // 验证手机号码 
        validator: function(value) {
            return /^(13|15|18)\d{9}$/i.test(value);
        },
        message: '手机号码格式不正确'
    },
    currency: { // 验证货币 
        validator: function(value) {
            return /^\d+(\.\d+)?$/i.test(value);
        },
        message: '货币格式不正确'
    },
    intOrFloat: { // 验证整数或小数 
        validator: function(value) {
            return /^\d+(\.\d+)?$/i.test(value);
        },
        message: '请输入数字,并确保格式正确'
    },
    integer: { // 验证整数 
        validator: function(value) {
            return /^[+]?[1-9]+\d*$/i.test(value);
        },
        message: '请输入整数'
    },
    age: { // 验证年龄
        validator: function(value) {
            return /^(?:[1-9][0-9]?|1[01][0-9]|120)$/i.test(value);
        },
        message: '年龄必须是0到120之间的整数'
    },
    chinese: { // 验证中文 
        validator: function(value) {
            return /^[\Α-\¥]+$/i.test(value);
        },
        message: '请输入中文'
    },
    english: { // 验证英语 
        validator: function(value) {
            return /^[A-Za-z]+$/i.test(value);
        },
        message: '请输入英文'
    },
    username: { // 验证用户名 
        validator: function(value) {
            return /^[a-zA-Z][a-zA-Z0-9_]{5,15}$/i.test(value);
        },
        message: '用户名不合法(字母开头,允许6-16字节,允许字母数字下划线)'
    },
    faxno: { // 验证传真 
        validator: function(value) {
            //            return /^[+]{0,1}(\d){1,3}[ ]?([-]?((\d)|[ ]){1,12})+$/i.test(value); 
            return /^((\(\d{2,3}\))|(\d{3}\-))?(\(0\d{2,3}\)|0\d{2,3}-)?[1-9]\d{6,7}(\-\d{1,4})?$/i.test(value);
        },
        message: '传真号码不正确'
    },
    zip: { // 验证邮政编码 
        validator: function(value) {
            return /^[1-9]\d{5}$/i.test(value);
        },
        message: '邮政编码格式不正确'
    },
    ip: { // 验证IP地址 
        validator: function(value) {
            return /^(?:(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\.){3}(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])$/.test(value);
        },
        message: 'IP地址格式不正确'
    },
    mac: {
        validator: function(value) {
            return /^([0-9a-fA-F]{2}-?){6}$/.test(value);
        },
        message: 'MAC地址不正确'
    },
    name: { // 验证姓名,可以是中文或英文 
        validator: function(value) {
            return /^[\Α-\¥]+$/i.test(value) | /^\w+[\w\s]+\w+$/i.test(value);
        },
        message: '请输入姓名'
    },
    date: { // 验证姓名,可以是中文或英文 
        validator: function(value) {
            //格式yyyy-MM-dd或yyyy-M-d
            return /^(?:(?!0000)[0-9]{4}([-]?)(?:(?:0?[1-9]|1[0-2])\1(?:0?[1-9]|1[0-9]|2[0-8])|(?:0?[13-9]|1[0-2])\1(?:29|30)|(?:0?[13578]|1[02])\1(?:31))|(?:[0-9]{2}(?:0[48]|[2468][048]|[13579][26])|(?:0[48]|[2468][048]|[13579][26])00)([-]?)0?2\2(?:29))$/i.test(value);
        },
        message: '日期格式不正确'
    },
    equals: {
        validator: function(value, param) {
            if ($("#" + param[0]).val() != "" && value != "") {
                return $("#" + param[0]).val() == value;
            } else {
                return true;
            }
        },
        message: '两次输入不匹配'
    },
    regex: {
        validator: function(value, param) {
            var regex = new RegExp(param[0]);
            return regex.test(value);
        },
        message: "格式不合法"
    },
    longitude: {
        validator: function(value, param) {
            var regex = /^[\-\+]?(0?\d{1,2}(\.\d{1,6})?|1[0-7]?\d{1}(\.\d{1,6})?|180(\.0{1,6})?)$/;
            return regex.test(value);
        },
        message: "经度格式不合法"
    },
    latitude: {
        validator: function(value, param) {
            var regex = /^[\-\+]?([0-8]?\d{1}(\.\d{1,6})?|90(\.0{1,6})?)$/;
            return regex.test(value);
        },
        message: "纬度格式不合法"
    },
    //不能为空,参数为不能取的值的数组 ["","-1","全部"]
    notEmpty: {
        validator: function(value, param) {
            var isValidate = true;
            for (var i = 0; i < param.length; i++) {
                if (value == param[i]) {
                    return false;
                }
            }
            return true;
        },
        message: "不能为空"
    },
    // 数值范围验证
    range: {
        validator: function(value, param) {
            return value >= param[0] && value <= param[1];
        },
        message: "值必须在{0}与{1}之间"
    }
});

//扩展Tab组件,添加遮罩
(function() {
    $.extend($.fn.tabs.methods, {
        //显示遮罩
        loading: function(jq, msg) {
            return jq.each(function() {
                var panel = $(this).tabs("getSelected");
                if (msg == undefined) {
                    msg = "正在加载数据,请稍候...";
                }
                $("<div class=\"datagrid-mask\"></div>").css({ display: "block", width: panel.width(), height: panel.height() + 30 }).appendTo(panel);
                $("<div class=\"datagrid-mask-msg\"></div>").html(msg).appendTo(panel).css({ display: "block", left: (panel.width() - $("div.datagrid-mask-msg", panel).outerWidth()) / 2, top: (panel.height() - $("div.datagrid-mask-msg", panel).outerHeight()) / 2 });
            });
        },
        //隐藏遮罩
        loaded: function(jq) {
            return jq.each(function() {
                var panel = $(this).tabs("getSelected");
                panel.find("div.datagrid-mask-msg").remove();
                panel.find("div.datagrid-mask").remove();
            });
        }
    });
})(jQuery);

// 扩展Datagrid 使用本地分页功能
(function($) {
    function pagerFilter(data) {
        if ($.isArray(data)) { // is array
            data = {
                total: data.length,
                rows: data
            }
        }
        var target = this;
        var dg = $(target);
        var state = dg.data('datagrid');
        var opts = dg.datagrid('options');
        if (!state.allRows) {
            state.allRows = (data.rows);
        }
        if (!opts.remoteSort && opts.sortName) {
            var names = opts.sortName.split(',');
            var orders = opts.sortOrder.split(',');
            state.allRows.sort(function(r1, r2) {
                var r = 0;
                for (var i = 0; i < names.length; i++) {
                    var sn = names[i];
                    var so = orders[i];
                    var col = $(target).datagrid('getColumnOption', sn);
                    var sortFunc = col.sorter || function(a, b) {
                        return a == b ? 0 : (a > b ? 1 : -1);
                    };
                    r = sortFunc(r1[sn], r2[sn]) * (so == 'asc' ? 1 : -1);
                    if (r != 0) {
                        return r;
                    }
                }
                return r;
            });
        }
        var start = (opts.pageNumber - 1) * parseInt(opts.pageSize);
        var end = start + parseInt(opts.pageSize);
        data.rows = state.allRows.slice(start, end);
        return data;
    }

    var loadDataMethod = $.fn.datagrid.methods.loadData;
    var deleteRowMethod = $.fn.datagrid.methods.deleteRow;
    $.extend($.fn.datagrid.methods, {
        clientPaging: function(jq) {
            return jq.each(function() {
                var dg = $(this);
                var state = dg.data('datagrid');
                var opts = state.options;
                opts.loadFilter = pagerFilter;
                var onBeforeLoad = opts.onBeforeLoad;
                opts.onBeforeLoad = function(param) {
                    state.allRows = null;
                    return onBeforeLoad.call(this, param);
                }
                var pager = dg.datagrid('getPager');
                pager.pagination({
                    onSelectPage: function(pageNum, pageSize) {
                        opts.pageNumber = pageNum;
                        opts.pageSize = pageSize;
                        pager.pagination('refresh', {
                            pageNumber: pageNum,
                            pageSize: pageSize
                        });
                        dg.datagrid('loadData', state.allRows);
                    }
                });
                $(this).datagrid('loadData', state.data);
                if (opts.url) {
                    $(this).datagrid('reload');
                }
            });
        },
        loadData: function(jq, data) {
            jq.each(function() {
                $(this).data('datagrid').allRows = null;
            });
            return loadDataMethod.call($.fn.datagrid.methods, jq, data);
        },
        deleteRow: function(jq, index) {
            return jq.each(function() {
                var row = $(this).datagrid('getRows')[index];
                deleteRowMethod.call($.fn.datagrid.methods, $(this), index);
                var state = $(this).data('datagrid');
                if (state.options.loadFilter == pagerFilter) {
                    for (var i = 0; i < state.allRows.length; i++) {
                        if (state.allRows[i] == row) {
                            state.allRows.splice(i, 1);
                            break;
                        }
                    }
                    $(this).datagrid('loadData', state.allRows);
                }
            });
        },
        getAllRows: function(jq) {
            return jq.data('datagrid').allRows;
        }
    })
})(jQuery);
// 扩展编辑器
$.extend($.fn.datagrid.defaults.editors, {
    my97datepicker: {
        init: function(container, options) {
            var datepicker = $("<input class='Wdate' onclick='showDatePicker()' />").appendTo(container);
            return input;

            function showDatePicker() {
                WdatePicker(options);
            }
        },
        getValue: function(target) {
            return $(target).val();
        },
        setValue: function(target, value) {
            $(target).val(value);
        },
        resize: function(target, width) {
            var input = $(target);
            if ($.boxModel == true) {
                input.width(width - (input.outerWidth() - input.width()));
            } else {
                input.width(width);
            }
        }
    }
});