0

I am using data table, And the column values are (tag1, tag2, tag3) Comma separated I have created a dropdown filter for that column

and the values of that dropdown are like the column values (tag1, tag2, tag3) Comma separated

But I need a single value for each option in the drop-down

tag1

tag2

tag3

and so on

Here is the code

initComplete: function () {
    this.api().columns([1]).every(function () {
        var column = this;
        var select = jQuery('<select id="test-haris"><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>')
        });
    });
}
3
  • This question looks very similar to this other question you asked a few hours ago. Commented Jul 25, 2020 at 17:40
  • And I see you have also asked the same question on the DataTables site forum. I think we are all struggling to understand the question. Can you provide a sample of your JSON data - and, based on that, describe what you want to see in each drop-down? Maybe that will help to clarify the problem. Commented Jul 25, 2020 at 17:50
  • @andrewjames please see this image ibb.co/kDPpSYk Maybe it will help to understand Commented Jul 25, 2020 at 18:02

1 Answer 1

2

The following approach builds a select list (a drop-down) from the contents of the first column.

For each cell in that column, it splits the comma-separated items into separate pieces of text, and then creates a sorted, unique list for the drop-down.

When you search by selecting an item from the drop-down, it checks if the selected item is contained anywhere in the text of each cell in that column. It uses a custom DataTables filter for this.

In my case, I placed the drop-down in the footer of the table - you can change that.

The table looks like this:

enter image description here

And here is the drop-down:

enter image description here

When an item is selected from the drop-down, you can see the filtering in effect:

enter image description here

The code for this solution is as follows - I have split it up into separate sections/functions to try to make the structure and approach clearer:

<!doctype html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Demo</title>
  <script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
  <script src="https://cdn.datatables.net/1.10.21/js/jquery.dataTables.min.js"></script>
  <link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.21/css/jquery.dataTables.min.css">
  <link rel="stylesheet" type="text/css" href="https://datatables.net/media/css/site-examples.css">
</head>

<body>

<div style="margin: 20px;">

    <table id="example" class="display dataTable cell-border" style="width:100%">
        <thead>
            <tr>
                <th>Name</th>
                <th>Position</th>
                <th>Office</th>
                <th>Age</th>
                <th>Start date</th>
                <th>Salary</th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td>Tiger , John, Nixon , </td>
                <td>System Architect</td>
                <td>Edinburgh</td>
                <td>61</td>
                <td>2011/04/25</td>
                <td>$320,800</td>
            </tr>
            <tr>
                <td>John, Garrett , Winters , </td>
                <td>Accountant</td>
                <td>Tokyo</td>
                <td>63</td>
                <td>2011/07/25</td>
                <td>$170,750</td>
            </tr>
            <tr>
                <td>Ashton, Winters , Cox</td>
                <td>Junior Technical Author</td>
                <td>San Francisco</td>
                <td>66</td>
                <td>2009/01/12</td>
                <td>$86,000</td>
            </tr>
            <tr>
                <td>Cedric , Kelly , Kelly</td>
                <td>Senior Javascript Developer</td>
                <td>Edinburgh</td>
                <td>22</td>
                <td>2012/03/29</td>
                <td>$433,060</td>
            </tr>
        </tbody>
        <tfoot>
            <tr>
                <th>Name</th>
                <th>Position</th>
                <th>Office</th>
                <th>Age</th>
                <th>Start date</th>
                <th>Salary</th>
            </tr>
        </tfoot>
    </table>

</div>

<script type="text/javascript">

$(document).ready(function() {
  
  // the DataTable object:  
  var table = $('#example').DataTable( {
    select: false // or, whatever you need in here.
  } );

  // Setup - add a select list to first footer cell:
  $('#example tfoot th').slice(0, 1).each( function () {
    var dropdown = buildDropdown();
    $(this).html( dropdown );
  } );


  // add a change event to the select list:
  $('#mySelect').change(function() {
    table.draw();
  });


  // create a custom search function for the select list,
  // which finds if the selected item is contained in the cell:
  $.fn.dataTable.ext.search.push(
    function( settings, data, dataIndex ) {
      var selectedValue = $('#mySelect').val();
      console.log(selectedValue);
      if (data[0].includes(selectedValue)) {
        return true;
      } else {
        return false;
      }
    }
  );


  function buildDropdown() {
    var selectHtml;
    // this will hold array of distinct values:
    var items = [];
    table.columns([0]).data().each(function (data, index) {
      data.forEach(function (newItems, index) {
        newItems.split(',').forEach(function (newItem, index) {
          if ( newItem.trim() !== '' && items.indexOf(newItem) === -1) {
            items.push(newItem.trim());
          }
        });
      });
    });
    // sort and remove duplicates:
    var uniqueSortedItems = [...new Set(items)].sort();

    selectHtml = '<select id="mySelect"><option value=""></option>';
    uniqueSortedItems.forEach(function(item) { 
      selectHtml = selectHtml + '<option value="' + item + '">' + item + '</option>';
    });
    selectHtml = selectHtml + '</select>';

    return selectHtml;
  }


} );

</script>

</body>
</html>

I think this is what you are trying to achieve - but you will need to integrate this into your specific solution, of course.

You will also need to decide what you want to do about the global search function (if you are using it), because it may interfere with the custom search used for the first column.

Sign up to request clarification or add additional context in comments.

1 Comment

Yes, exactly that's what I want to achieve Let me modify it accordingly Thanks :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.