Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DataTables hyperlink on all rows in a column

I am using DataTables to generate a table. There is a column containing order numbers.

For example: ...

I need every row in this column to have a hyperlink to view/order?id=? where ? is the contents of row in the Order No column. For example the first row would be a hyperlink to view/order?id=1321755 etc.

What is the simplest way I can do so?

enter image description here

Here is the code that I am using to initialize the DataTables:

<script type="text/javascript" charset="utf-8">
    $(document).ready(function() {
        $('#example').dataTable( {
            "serverSide": true,
            "ajax": {
                    "url": "../server_processing/orders.php",
                    "type": "POST"
                    },
            "order": [[ 0, "desc" ]]
        } );
    } );
</script>

  <table id="example" class="display" cellspacing="0" width="100%">
    <thead>
      <tr>
        <th>Order No</th>
        ...
      </tr>
    </thead>
    <tbody>
    </tbody>
  </table>
like image 928
user3973427 Avatar asked Aug 26 '14 09:08

user3973427


1 Answers

Check this out: http://datatables.net/reference/option/columns.render

You can add a column render callback when you specify columns definition.

var columnsDef = [
...
{
    "title": "Order No.",
    "render": function (data, type, row, meta) {
        return '<a href="view/order?' + data + '">' + data + '</a>';
    }
},
...
];

$("#table").dataTable({
    ...
    "columns": columnsDef,
    ...
});

The data in that column will be changed to what the render function return.

like image 114
Moozz Avatar answered Sep 22 '22 02:09

Moozz