jQuery Datatables和ColumnFilterWidget集成问题

时间:2014-05-29 09:07:15

标签: javascript jquery html dom datatables

我正在尝试将ColumnFilterWidget插件放在数据表表的标头中。

以下是我所做的更改:

        /**
        * Menu-based filter widgets based on distinct column values for a table.
        *
        * @class ColumnFilterWidgets
        * @constructor
        * @param {object} oDataTableSettings Settings for the target table.
        */
        var ColumnFilterWidgets = function( oDataTableSettings ) {
                var me = this;
                var sExcludeList = '';
//              me.$WidgetContainer = $( '<div class="column-filter-widgets"></div>' );
                me.$WidgetContainer = $( '<tr class="head"></tr>' );
                me.$MenuContainer = me.$WidgetContainer;
                me.$TermContainer = null;
                me.aoWidgets = [];
                me.sSeparator = '';
                if ( 'oColumnFilterWidgets' in oDataTableSettings.oInit ) {
                        if ( 'aiExclude' in oDataTableSettings.oInit.oColumnFilterWidgets ) {
                                sExcludeList = '|' + oDataTableSettings.oInit.oColumnFilterWidgets.aiExclude.join( '|' ) + '|';
                        }
                        if ( 'bGroupTerms' in oDataTableSettings.oInit.oColumnFilterWidgets && oDataTableSettings.oInit.oColumnFilterWidgets.bGroupTerms ) {
                                me.$MenuContainer = $( '<div class="column-filter-widget-menus"></div>' );
                                me.$TermContainer = $( '<div class="column-filter-widget-selected-terms"></div>' ).hide();
                        }
                }

                // Add a widget for each visible and filtered column
                $.each( oDataTableSettings.aoColumns, function ( i, oColumn ) {
                        var $columnTh = $( oColumn.nTh );
                        var $WidgetElem = $( '<th><div class="column-filter-widget"></div></th>' );
                        if ( oColumn.bVisible && sExcludeList.indexOf( '|' + i + '|' ) < 0 ) {
                                me.aoWidgets.push( new ColumnFilterWidget( $WidgetElem, oDataTableSettings, i, me ) );
                        }
                        me.$MenuContainer.append( $WidgetElem );
                } );
                if ( me.$TermContainer ) {
                        me.$WidgetContainer.append( me.$MenuContainer );
                        me.$WidgetContainer.append( me.$TermContainer );
                }
                oDataTableSettings.aoDrawCallback.push( {
                        name: 'ColumnFilterWidgets',
                        fn: function() {
                                $.each( me.aoWidgets, function( i, oWidget ) {
                                        oWidget.fnDraw();
                                } );
                        }
                } );

                return me;
        };

我在Datatable中添加了一个额外的<tr class='head'>,后来我试图将Filters附加到附加到它们的那个上,但是它正在创建新的TR标记,然后在其中附加过滤器

我甚至将我的dom数据表改为:dom:'<"clear">Cf<"clear">ltWrip', 所以表格元素应该在那里,以便它可以在头部内插入过滤器。

1 个答案:

答案 0 :(得分:2)

找到答案

如果其他人需要它,就是这样。

在html中添加<TR id='Filter.$i'>元素 使用for循环并将计数器值附加到ID。 然后修改了column.filterwidget plugn js

 var ColumnFilterWidgets = function( oDataTableSettings ) {
                var me = this;
                var sExcludeList = '';
                me.$WidgetContainer = $( '<div class="column-filter-widgets"></div>' );
                me.$MenuContainer = me.$WidgetContainer;
                me.$TermContainer = null;
                me.aoWidgets = [];
                me.sSeparator = '';

                if ( 'oColumnFilterWidgets' in oDataTableSettings.oInit ) {
                        if ( 'aiExclude' in oDataTableSettings.oInit.oColumnFilterWidgets ) {
                                sExcludeList = '|' + oDataTableSettings.oInit.oColumnFilterWidgets.aiExclude.join( '|' ) + '|';
                        }
                        if ( 'bGroupTerms' in oDataTableSettings.oInit.oColumnFilterWidgets && oDataTableSettings.oInit.oColumnFilterWidgets.bGroupTerms ) {
                                me.$MenuContainer = $( '<div class="column-filter-widget-menus"></div>' );
                                me.$MenuContainer = $( '<div class="column-filter-widget-menus"></div>' );
                                me.$TermContainer = $( '<div class="column-filter-widget-selected-terms"></div>' ).hide();
                        }
                }

                var cnt= 0;
                // Add a widget for each visible and filtered column
                $.each( oDataTableSettings.aoColumns, function ( i, oColumn ) {
                        var $columnTh = $( oColumn.nTh );
                        cnt ++;
                        var $WidgetElem = $( '<div class="column-filter-widget" id=col'+cnt+'></div>' );
                        if ( oColumn.bVisible && sExcludeList.indexOf( '|' + i + '|' ) < 0 ) {
                                me.aoWidgets.push( new ColumnFilterWidget( $WidgetElem, oDataTableSettings, i, me ) );
                        }
                        var Tcol = document.getElementById('A');
                                console.log('---------'+i);

                        //me.$MenuContainer.append( $WidgetElem );
                        $('#Filter'+i).append( $WidgetElem );
                } );

                if ( me.$TermContainer ) {
                        me.$WidgetContainer.append( me.$MenuContainer );
                        me.$WidgetContainer.append( me.$TermContainer );
                }
                oDataTableSettings.aoDrawCallback.push( {
                        name: 'ColumnFilterWidgets',
                        fn: function() {
                                $.each( me.aoWidgets, function( i, oWidget ) {
                                        oWidget.fnDraw();
                                } );
                        }
                } );

                return me;
        };

希望这有帮助。