How to filter by column x?

How to filter by column x?

pydpyd Posts: 2Questions: 1Answers: 0
edited November 2021 in DataTables 1.10

I can't figure out how to select what column to use when filtering a dataset.
With my current code, it automaticly selects the first column and then populates a select-box with each unique value,but I need to use column #2.

TLRD; How to populate and filter by other column than the first one?

I'm sure this is stupid simple, but...
Current code:

        <script type="text/javascript">
            $(document).ready(function()
                {
                    oTable = $('#contacts-datatable').DataTable(
                    {
                        pageLength: 100,
                        dom: "lrt",
                        order: [
                            [0, "asc"]
                        ],
                        columnDefs: [
                        {
                            targets: [0],
                            visible: true,
                            searchable: true
                        }],

                        initComplete: function()
                        {
                            this.api().columns(':lt(1)').every(function()
                            {
                                var column = this;
                                var select = $('<select class="titleOption "><option value=""></option></select>')
                                    .appendTo($(".filterSomething"))
                                    .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>')
                                });
                            });
                        }
                    });
                    $('#contactSearchBox').keyup(function(){
                        oTable.search($(this).val()).draw() ;
                    })
                });
        </script>

Edited by Colin - Syntax highlighting. Details on how to highlight code using markdown can be found in this guide.

This question has an accepted answers - jump to answer

Answers

  • colincolin Posts: 15,240Questions: 1Answers: 2,599
    Answer ✓
    this.api().columns(':lt(1)').every(function()
    

    is saying columns less than 1, i.e. 0, so the first column. To make that the second column, use

    this.api().columns(2).every(function()
    

    though as it's a single column, you don't really need the loop,

    Colin

  • pydpyd Posts: 2Questions: 1Answers: 0

    @colin : Awesome, thanks! I need to brush up on my JS... :)

Sign In or Register to comment.