import csv into datatable
import csv into datatable
Hi,
I have been able to get this plug in to work great with static text (HTML), but I am looking for more of a dynamic solution. I would like to know if it is possible to import a csv file into datatables or if there exists a plugin that already does this? I am sure someone has already faced this issue and solved it. Any suggestions or feedback is greatly appreciated.
Thanks,
I have been able to get this plug in to work great with static text (HTML), but I am looking for more of a dynamic solution. I would like to know if it is possible to import a csv file into datatables or if there exists a plugin that already does this? I am sure someone has already faced this issue and solved it. Any suggestions or feedback is greatly appreciated.
Thanks,
This discussion has been closed.
Replies
Allan
Allan
There is a jquery plugin called csv2table that plays nice with the datatables plugin. If you want to return the csv data as an array of arrays, use the code snippet below.
[code]
/* Usage:
* jQuery.csv()(csvtext) returns an array of arrays representing the CSV text.
* jQuery.csv("\t")(tsvtext) uses Tab as a delimiter (comma is the default)
* jQuery.csv("\t", "'")(tsvtext) uses a single quote as the quote character instead of double quotes
* jQuery.csv("\t", "'\"")(tsvtext) uses single & double quotes as the quote character
*/
;
jQuery.extend({
csv: function(delim, quote, linedelim) {
delim = typeof delim == "string" ? new RegExp("[" + (delim || ",") + "]") : typeof delim == "undefined" ? "," : delim;
quote = typeof quote == "string" ? new RegExp("^[" + (quote || '"') + "]") : typeof quote == "undefined" ? '"' : quote;
lined = typeof lined == "string" ? new RegExp("[" + (lined || "\r\n") + "]+") : typeof lined == "undefined" ? "\r\n" : lined;
function splitline(v) {
// Split the line using the delimitor
var arr = v.split(delim),
out = [], q;
for (var i = 0, l = arr.length; i < l; i++) {
if (q = arr[i].match(quote)) {
for (j = i; j < l; j++) {
if (arr[j].charAt(arr[j].length - 1) == q[0]) { break; }
}
var s = arr.slice(i, j + 1).join(delim);
out.push(s.substr(1, s.length - 2));
i = j;
}
else { out.push(arr[i]); }
}
return out;
}
return function(text) {
var lines = text.split(lined);
for (var i = 1, l = lines.length; i < l; i++) {
lines[i] = splitline(lines[i]);
}
return lines;
};
}
});
[/code]
I worked out my issue with the above code. I hope this helps :)
Allan
[code]
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
Untitled Document
<!--[if IE]>
<![endif]-->
jQuery(function($){
$(".btn1").attr("disabled","");
});
.jQchart-labelY-canvasMyID{
font-size : 12px;
}
/* label Data */
.jQchart-labelData-canvasMyID{
font-size : 10px;
}
.jQchart-labelYunit{
width : 100px;
}
$(function(){
$('#view1').csv2table('csv/tours.csv');
});
[/code]
Oh and excuse me for the extremely noob question, but where exactly would you put that snippet of code that sonicbug posted? I'm a webdesigner, not a developer, so my knowledge is rather limited when it comes to coding.
Thanks