Search Panes *asc* ordering

129 Views Asked by At

I am using search panes for product filtering, i need to order the data inside the filters in asc order at least. At the moment they are in a random order. The data is stored within a table (table> tbody> tr > td > div > span (**DATA**)). The issue i came across is that the data in the filters is a string type. I have tried all if not most config options for ordering, but none have work so far. I tried some custom javascript for it too, but it hasnt worked neither.

I have attached below some screenshots of the code and the code itself. If anyone has any ideas or needs more information let me know.

Thank you

//$('.variants-table').DataTable().destroy()
    $('.variants-table').DataTable({
        "bPaginate": false,
        bProcessing: false,
        columnDefs:[{
            searchPanes: {
                collapse: true,
                collapse: false
            },
            targets: [1]
        }],
        searchPanes: {
            cascadePanes: true,
            threshold: 1,
            controls: false,
            orderable: true,
            collapse: true,
            clear: false,
            dtOpts: {
                order: [[0, "desc"]],
                select: {
                    style: 'multi'
                },
            },
            columns: [1, 2, 3, 4, 5],
        },
        initComplete: function (settings, json) {
            $('.dtsp-panes').append(
                $(document.createElement('button')).prop({
                    type: 'button',
                    innerHTML: `<i class="bi bi-arrow-down"></i> View Results`,
                    class: 'btn btn-primary view-results d-none',

                }).attr('onClick', 'smoothScroll()')

            );
            $('.dtsp-topRow').on('click', function () {
                $(this).toggleClass('bg-secondary')
            })
        },
        dom: 'Plfrtip',
        "drawCallback": function (settings) { if ($(window).width() < 1024) { mobileProductFilter(); } }
    })

Filter Image

I tried most of the config options for the search panes, it just doesnt order it.

1

There are 1 best solutions below

3
andrewJames On

Use a DataTables column renderer to change your string values such as 8mm to numeric values such as 8.

One simple way to do this is to strip off the final 2 characters from each value - assuming these values all end in mm.

data.substring(0, data.length - 2)

If this assumption is false, then different JavaScript logic would be needed.


You also need to include logic to ensure that the above conversion only happens for the type === 'sort' rendered value. This is the value that DataTables uses for sorting data in a column - and in the related search pane. It is different from the data value that DataTables uses for display, and for filtering. This ability to store different representations of the same data is referred to as orthogonal data in DataTables.


Finally, ensure the rendered data is interpreted by DataTables as a numeric value and not as a string:

type: 'num'

See columns.type


A demo, showing all of the above steps:

$(document).ready(function() {

  $('#example').DataTable({
    dom: 'Pfrtip',
    columnDefs: [{
      type: 'num',
      render: function(data, type, row, meta) {
        if (type === 'sort') {
          return data.substring(0, data.length - 2);
        } else {
          return data;
        }
      },
      targets: [1, 2, 3]
    }],
    searchPanes: {
      threshold: 1,
      controls: false,
      columns: [1, 2, 3]
    }
  });


});
<!doctype html>
<html>

<head>
  <meta charset="UTF-8">
  <title>Demo</title>

  <link href="https://cdn.datatables.net/1.13.4/css/jquery.dataTables.min.css" rel="stylesheet" />
  <link href="https://cdn.datatables.net/searchpanes/2.1.2/css/searchPanes.dataTables.min.css" rel="stylesheet" />
  <link href="https://cdn.datatables.net/select/1.6.2/css/select.dataTables.min.css" rel="stylesheet" />

  <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
  <script src="https://cdn.datatables.net/1.13.4/js/jquery.dataTables.min.js"></script>
  <script src="https://cdn.datatables.net/searchpanes/2.1.2/js/dataTables.searchPanes.min.js"></script>
  <script src="https://cdn.datatables.net/select/1.6.2/js/dataTables.select.min.js"></script>

  <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>Item</th>
          <th>Max Diameter</th>
          <th>Min Diameter</th>
          <th>Length</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td>Item 123</td>
          <td>8mm</td>
          <td>6mm</td>
          <td>25.4mm</td>
        </tr>
        <tr>
          <td>Item 321</td>
          <td>5.99mm</td>
          <td>3.99mm</td>
          <td>19.05mm</td>
        </tr>
        <tr>
          <td>Item 456</td>
          <td>15.99mm</td>
          <td>13.99mm</td>
          <td>9.05mm</td>
        </tr>
      </tbody>
    </table>

  </div>


</body>

</html>

This approach also means the data in the main table is sortable by numeric size, even though the values displayed are strings (containing mm):

end result