Columns Filtering when column is array type

Columns Filtering when column is array type

LapointeLapointe Posts: 375Questions: 70Answers: 4

Sure there is better way to do, but this work

        api.columns( FilterColumnList ).every( function () {
            var column = this;
            var select = $('<select><option value=""></option></select>')
                .appendTo( $(column.footer()).empty() )
                .on( 'change', function () {
                    var val = $.fn.dataTable.util.escapeRegex($(this).val());
                    column
                        .search( val ? '^'+val+'$' : '', true, false )
                        .draw();
                } );
                if ( $( column.header() ).hasClass( 'dt-render' ) ) {
                    var cells = api.cells( null, this );
                    if ( Array.isArray( cells.data()[ 0 ] ) ){  // type array
                        var sArr = cells.data(),
                            dArr = [];
                        // unique set of datas
                        for ( var z=0; z < sArr.length; z++ ){
                            sArr[ z ].forEach( function( e ) {
                                e = Object.values( e );
                                if( $.inArray( e[ 0 ], dArr ) === -1 ) dArr.push( e[ 0 ] );
                            });
                        }
                        dArr.sort(function(a, b){
                            if (isNaN( a )) {
                                var aa = a.toLowerCase();
                                var bb = b.toLowerCase();
                            } else {
                                var aa = Number( a );
                                var bb = Number( b );
                            }
                            if ( aa < bb ) {
                                return -1;
                            }
                            else if ( aa > bb ) {
                                return 1;
                            }
                            return 0;
                        })
                        dArr.forEach( function ( d, j ) {
                            var t = '<option ';
                            if ( (d) ){
                                if  (savedColumnsFilters){
                                    if (savedColumnsFilters[column[0][0]].search.search.replace(/\\/g,"") == '^'+d+'$'){
                                        t += "selected='selected' ";
                                    }
                                }
                                select.append( t + 'value="'+d+'">'+d+'</option>' );
                            }
                        } );
                    } else {
                        cells.render('display').unique().sort().each( function ( d, j ) {
                            var t = '<option ';
                            if ( (d) ){
                                if  (savedColumnsFilters){
                                    if (savedColumnsFilters[column[0][0]].search.search.replace(/\\/g,"") == '^'+d+'$'){
                                        t += "selected='selected' ";
                                    }
                                }
                                select.append( t + 'value="'+d+'">'+d+'</option>' );
                            }
                        } );
                    };
                } else {
                    column.data().unique().sort().each( function ( d, j ) {
                        var t = '<option ';
                        if ( (d) ){
                            if  (savedColumnsFilters){
                                if (savedColumnsFilters[column[0][0]].search.search.replace(/\\/g,"") == '^'+d+'$'){
                                    t += "selected='selected' ";
                                }
                            }
                            select.append( t + 'value="'+d+'">'+d+'</option>' );
                        }
                    });
                }
        });
Sign In or Register to comment.