user3599803
user3599803

Reputation: 7024

Sort column by value other than cell content

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?

Upvotes: 2

Views: 111

Answers (2)

Erangad
Erangad

Reputation: 861

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

Upvotes: 0

Gyrocode.com
Gyrocode.com

Reputation: 58880

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>

Upvotes: 2

Related Questions