快乐生活 轻松工作

采用Jquery,在table上实现对行的单击、双击、返回选中行数据的功能,可作为GridView控制客户端的扩展

(function ($) {
    $.fn.gridview = function (settings) {
        var dfop =
            {
                SelectedRow: false,
                onrowclick: false,
                ondblclick: false,
                 onbuttonclick: false

               }
        $.extend(dfop, settings);
        var me = $(this);
         

        InitEvent(me.attr("id"));
        function InitEvent(gridviewid) {
           $("#" + gridviewid + ">tbody>tr:gt(0)").each(function () {
               if (dfop.onbuttonclick) {
                    $(this).find('.btntext').click(buttonclick);
                }
                $(this).click(rowclick).dblclick(rowdblclick);
            });

        }

        function buttonclick() {
            dfop.onbuttonclick.call(this, $(this));
            return false;
        }
        function rowclick(e) {
            if (dfop.SelectedRow) {
                if ($(dfop.SelectedRow).hasClass("gridviewSelectItem")) {
                    $(dfop.SelectedRow).removeClass('gridviewSelectItem');
                    if ($(dfop.SelectedRow).index() % 2 == 0) {
                        $(dfop.SelectedRow).addClass("gridviewItem");
                    } else {
                        $(dfop.SelectedRow).addClass("gridviewAlterItem");
                    }
                }
            }
            dfop.SelectedRow = $(this)[0];
            if ($(this).hasClass("gridviewItem")) {
                $(this).removeClass('gridviewItem');
            } else {
                $(this).removeClass('gridviewAlterItem');
            }
            $(this).addClass("gridviewSelectItem");
            if (dfop.onrowclick) {
                dfop.onrowclick.call(this, dfop.SelectedRow);
            }
        }

        function rowdblclick(e) {
            if (dfop.ondblclick) {
                dfop.ondblclick.call(this, dfop.SelectedRow);
            }
        }

      me[0].t = {
            getCurrentRow: function () {
                return dfop.SelectedRow;
            }
        };
        return me;
    }

    $.fn.getCurrentRow = function () {
        if (this.length > 0 && this[0].t) {
            return this[0].t.getCurrentRow();
        }
        return null;
    }
})(jQuery);

posted on 2013-03-15 19:02  思考 学习 总结  阅读(675)  评论(0)    收藏  举报