Right now I have the following html table:
<table id="datatable">
<thead>
<th>fruits</th>
<th>vegs</th>
</thead>
<tbody>
<tr>
<td>apple</td>
<td>potato</td>
</tr>
<tr>
<td>apple</td>
<td>carrot</td>
</tr>
</tbody>
</table>
And I’d like to reference the columns by name like this:
<script type="text/javascript">
$(document).ready(function() {
/* Init the table */
var oTable = $('#datatable').dataTable( );
//get by sTitle
console.log(oTable);
var row = oTable.fnGetData(1)
console.log(row['vegs']);//should return 'carrot'
} );
</script>
Is there anyway to have a javascript function fnGetData() to return an object rather than an array when the datasource is DOM?
So, I’ve researched a bit and found the
datatableplugin is not very smart at handling columns – they are always arrays needed to be accessed with an integer. The only thing that handles columns and their properties is theaoColumnsobject – thanks at @JustinWrobel for finding thefnSettingsmethod to access that object after initialisation. If you haven’t had this, you were stuck with$table.find("thead th").However, now it is easy to get the table as an array of objects: