Question: Sort column by value other than cell content

Question

Sort column by value other than cell content

Answers 2
Added at 2016-12-25 10:12
Tags
Question

I'm using jQuery DataTables and I have one column that looks like shown below:

<td><span class="badge"> 123 </span>  <span> customer name </span></td>

i.e, I put first some number (ID), then the actual name which I want to sort by.

How can I tell jQuery DataTables to sort correctly by customer name?

Answers to

Sort column by value other than cell content

nr: #1 dodano: 2016-12-25 15:12

Use data-order attribute on td element as shown in this example.

<td data-order="customer name">
   <span class="badge"> 123 </span>  
   <span> customer name </span>
</td>
nr: #2 dodano: 2016-12-27 03:12

You can do this with jQuery.

<table id="example" cellspacing="0" width="100%">
    <thead>
        <tr>
            <th>ID</th>
            <th>Name</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>12</td>
            <td data-search>Paul</td>
        </tr>
        <tr>
            <td>13</td>
            <td data-search>Nickson</td>
        </tr>
    </tbody>
 </table>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript" charset="utf8" src="jquery.dataTables.min.js"></script>
<script>
     $(document).ready(function() {
         $('#example').DataTable();
     } );
</script>

You have to add jquery.dataTables.min.js after including the jquery. You can find more about this here

Source Show
◀ Wstecz