How to read data from CSV?
How to read data from CSV?
MickMan
Posts: 33Questions: 5Answers: 0
Hi,
i need to read data from a csv stored on the same server of the datatable.
I'm pretty familiar with json ajax source, but can't do this.
I read a lot of example about parsing, array of arrays, conversion to json and so on, but didn't get it.
The csv i need to read is something like this:
Title1,Title2,Title3
Data1,Data2,Data3
Data1,Data2,Data3
Data1,Data2,Data3
This is the code that i use to read a json.
How can i use a CSV instead? Someone willing to help?
Thanks.
// DataTable
$.fn.dataTable.ext.errMode = 'none';
var table = $('#webapp').DataTable(
{
"order": [],
dom: '<"top"<"div-no-mobile"><"div-only-mobile"><"clear">>rt<"bottom"<"div-no-mobile"><"div-only-mobile"><"clear">>',
responsive: {
details: {
display: $.fn.dataTable.Responsive.display.childRowImmediate,
type: '',
renderer: function(api, rowIdx, columns) {
var data = $.map(columns, function(col, i) {
if (col.hidden) {
if (col.data) {
return '<tr data-dt-row="' + col.rowIndex + '" data-dt-column="' + col.columnIndex + '">' +
'<td><b>' + col.title + ':' + '</b></td> ' +
'<td>' + col.data + '</td>' +
'</tr>'
}
}
}).join('');
return data ? $('<table/>').append(data) : false;
}
}
},
"ajaxSource": "URL WITH JSON DATA",
"displayLength": 99999,
"columns": [
{ "data": "Title1" },
{ "data": "Title22" },
{ "data": "Title3"}
],
"scrollY": "72vh",
"scrollCollapse": true,
initComplete: function () {
// Apply the search
this.api().columns().every( function () {
var that = this;
$( 'input', this.footer() ).on( 'keyup change clear', function () {
if ( that.search() !== this.value ) {
that
.search( this.value )
.draw();
}
} );
} );
}
});
Replies
See if this Stack Overflow thread helps to convert the CSV to JSON. Then you can use
data
, replacingajaxSource
, orrows.add()
to populate the Datatable with the JSON data.Kevin