jquery.table.toolkit.js

第一版


具体代码

/// <reference path="jquery-1.8.2.min.js" />

(function ($) {

    var methods = {
        getSelected: function () {

        }
    };

    var methodArr = ['getSelected'];

    $.fn.toolkit = function (options) {
        var defaults = {
            chkIndex: 0,//checkbox 列索引
            keyIndex: 0,//隐藏域列索引
            header: 'th',//标题DOM标签
            debug: false,//是否开启调试模式
            method: '',//调用内部方法
            init: function () { },//初始化函数
            ajaxOpts: {//Ajax 配置参数
                type: 'POST',
                dataType: 'json'
            },
            //actions: [{//操作方法
            //    name: 'delete',
            //    url: '',
            //    s: function () {

            //    },
            //    f: function () {

            //    }
            //}]
            actions: []
        };

        var selected = [];

        var opts = $.extend({}, defaults, options);
        var self = $(this);

        var chkAll = self.find('tr:eq(0) ' + opts.header + ':eq(' + opts.chkIndex + ') :checkbox');
        var chks = self.find('tr:gt(0) td:first-child :checkbox');
        var as = self.find('tr:gt(0) td:last-child a');

        if (!self.data('status')) {
            opts.init();
            chkAll.click(function () {

                selected = [];

                if (this.checked) {
                    chks.prop('checked', true).each(function () {
                        selected.push($(this).next().val());
                    });
                } else {
                    chks.prop('checked', false);
                }
                if (opts.debug) {
                    debug(selected.join(', '));
                }
            });

            chks.live('click', function () {

                var id = $(this).next().val();

                if (this.checked) {
                    selected.push(id);
                } else {
                    var index = $.inArray(id, selected);
                    selected.splice(index, 1);

                }
                if (opts.debug) {
                    debug(selected.join(' - '));
                }
                if (chks.length == chks.filter(":checked").length) {
                    chkAll.prop('checked', true);
                } else {
                    chkAll.prop('checked', false);
                }
            });

            self.attr('data-status', true);

            if (opts.actions.length > 0) {
                as.live('click', function () {
                    var cmdName = $(this).data('cmd');
                    var actionOpts = {};

                    switch (cmdName) {
                        case 'delete':
                            $(opts.actions).each(function () {
                                if ($(this).attr('name') == cmdName) {
                                    actionOpts = this;
                                    return false;
                                }
                            })
                            break;
                        default:
                            break;
                    }

                    doAjax($.extend(opts.ajaxOpts, actionOpts));

                    return false;

                });
            }

        }

        if (opts.method.length == 0) {
            return self;
        }

        if ($.inArray(opts.method, methodArr) == -1) {
            return 'method ' + opts.method + ' is undefined';
        }
        if (opts.method == 'getSelected') {
            chks.filter(":checked").each(function () {
                selected.push($(this).next().val());
            });
            return selected;
        }

    };

    $.fn.toolkit.selected = function () {
        var selected = [];
        var self = $(this);
        var chks = self.find('tr:gt(0) td:first-child :checkbox');
        chks.filter(":checked").each(function () {
            selected.push($(this).next().val());
        });
    }

    // 私有函数:debug    
    function debug(val) {
        if (window.console && window.console.log)
            window.console.log(val);
    };

    function doAjax(opts) {
        opts.b();
        $.ajax({
            url: opts.url,
            data: opts.data,
            dataType: opts.dataType,
            success: function () {
                opts.s()
            },
            fail: function () {
                opts.f();
            }
        });

    };

})(jQuery);

知识共享许可协议
《jquery.table.toolkit.js》 常伟华 创作。
本作品采用知识共享署名-相同方式共享 4.0 国际许可协议 | 3.0 中国大陆许可协议进行许可。

站内公告

A PHP Error was encountered

Severity: Core Warning

Message: PHP Startup: zip: Unable to initialize module Module compiled with module API=20060613 PHP compiled with module API=20090626 These options need to match

Filename: Unknown

Line Number: 0

Backtrace: