split the comma separated values of a column (1) Tags and make a dropdown filter with single value

split the comma separated values of a column (1) Tags and make a dropdown filter with single value

Haris ZafarHaris Zafar Posts: 2Questions: 1Answers: 0
edited July 2020 in Free community support

split the comma-separated values of a column (1) Tags and make a dropdown filter with a single value
like Column 1 Tags -> Row 1 have values (Tag A, Tag B, Tag C) && And Row 2 have (Tag A, Tag C, Tag D)
And dropdown filter looks alike
1st index -> Tag A, Tag B, Tag C
2nd index -> Tag A, Tag C, Tag D

instead of
1st index -> Tag A
2nd index -> Tag B
3rd Index -> Tag C
4th Index -> Tag D and So on

Here is the code I am using
...
initComplete: function () {
this.api().
columns([1]).every( function () {
var column = this;
var select = jQuery('<select id="coulumn 1"><option value=""></option></select>')
.appendTo( jQuery(column.header()).empty() )
.on( 'change', function () {
var val = jQuery.fn.dataTable.util.escapeRegex(
jQuery(this).val()
);
column
.search( val ? '^'+val+'$' : '', true, false )
.draw();
} );
column.data().unique().sort().each( function ( d, j ) {
select.append( '<option value="'+d+'">'+d+'</option>' )
} );
} );
}
...

Answers

  • kthorngrenkthorngren Posts: 21,537Questions: 26Answers: 4,988

    I'm not sure how to specifically answer your questions without seeing a test case with your specific data. But the code the builds the select is this:

                        column.data().unique().sort().each( function ( d, j ) {
                            select.append( '<option value="'+d+'">'+d+'</option>' )
                        } );
    

    You can modify it to suit your needs. If you still need help please build a simple test case with an example of your data so we can help you with your code.
    https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case

    Kevin

  • Haris ZafarHaris Zafar Posts: 2Questions: 1Answers: 0

    @kthorngren Here is the image link
    ibb.co/kDPpSYk
    maybe it will help you to Understand the problem

    Thanks

  • kthorngrenkthorngren Posts: 21,537Questions: 26Answers: 4,988

    The best way to get help is if you build a simple test case with your data.
    https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case

    Kevin

  • yajuvendra1990yajuvendra1990 Posts: 1Questions: 0Answers: 0
    edited November 2020

    here are the solution

    initComplete: function () {
        this.api().
        columns([1]).every( function () {
            var column = this;
            var select = jQuery('<select id="coulumn1"><option value=""></option></select>')
            .appendTo( jQuery(column.header()).empty() )
            .on( 'change', function () {
            var val = jQuery.fn.dataTable.util.escapeRegex(
                jQuery(this).val()
            );
            column
            .search( this.value )
            .draw();
            } );
            column.data().unique().sort().each( function( d, j ) {      
                   var nameArr = d.split(",");
                    nameArr.forEach(function(number) {                
                        var optionExists = ($("#coulumn1 option[value="+number+"]").length > 0);
                        if(!optionExists){
                            select.append( '<option value="'+number+'">'+number+'</option>' );
                        }                    
                    });                           
            } );
        } );
    }
    
  • colincolin Posts: 15,240Questions: 1Answers: 2,599

    Looks good, thanks for posting,

    Colin

  • JayedJayed Posts: 2Questions: 0Answers: 0

    Hey @colin, I am a fan of your codes. I understand many things by studying your codes
    I am using this for filter

               initComplete: function () {
                    this.api().columns([2,3,4,5,6,7,8]).every( function () {
                        var column = this;
                        console.log(this.index())
                        var select = $('<select><option value="">All</option></select>')
                            .appendTo(  $('thead tr:eq(1) th:eq(' + this.index()  + ')') )
                            .on( 'change', function () {
                                var val = $.fn.dataTable.util.escapeRegex(
                                    $(this).val()
                                );
    
                                column
                                    .search( val ? '^'+val+'$' : '', true, false )
                                    .draw();
                            } );
    
                        column.data().unique().sort().each( function ( d, j ) {
                            select.append( '<option value="'+d+'">'+d+'</option>' )
                        } );
                    } );
                }
    

    I have comma separated values in 4th column. How I can use separate values as a option of dropdown filter?

  • colincolin Posts: 15,240Questions: 1Answers: 2,599

    We're happy to take a look, but as per the forum rules, please link to a test case - a test case that replicates the issue will ensure you'll get a quick and accurate response. Information on how to create a test case (if you aren't able to link to the page you are working on) is available here.

    Cheers,

    Colin

  • JayedJayed Posts: 2Questions: 0Answers: 0
    edited June 2021

    Below is the test case @colin :
    http://live.datatables.net/hibidibu/1/edit?html,css,js,output
    Please check the filter in 2nd and 3rd columns and comma separated values

  • kthorngrenkthorngren Posts: 21,537Questions: 26Answers: 4,988

    See if this thread gets you started. See the example from the thread. For a complete solution it will probably require two loops; the first building an array of the split items making sure the array only contains unique data. The second looping through in sorted order to build the selects.

    Kevin

This discussion has been closed.