Parsing And Displaying CSV Files In jQuery - csv.js

Parsing And Displaying CSV Files In jQuery - csv.js
File Size: 87.3 KB
Views Total:
Last Update:
Publish Date:
Official Website: Go to website
License: MIT
   

csv.js is a jQuery based CSV parser that parses your CSV files and converts the CSV strings into JavaScript arrays and objects for further use. Ideal for dynamically generating data tables and data charts/graphs from CSV files. Supports both browser and node.js.

Installation:

# NPM
$ npm install jquery-csv --save

How to use it:

1. Insert the main JavaScript file csv.js after jQuery JavaScript library.

<script src="https://code.jquery.com/jquery-1.12.4.min.js" 
        integrity="sha384-nvAa0+6Qg9clwYCGGPpDQLVpLNn0fRaROjHqs13t4Ggj3Ez50XnGQqc/r8MhnRDZ" 
        crossorigin="anonymous">
</script>
<script src="src/jquery.csv.js"></script>

2. The JavaScript to parse a single line of CSV data into an array of values.

$.csv.toArray(csv)

3. The JavaScript to parse CSV data into a JavaScript 2D (two-dimensional) array.

$.csv.toArrays(csv)

4. The JavaScript to parse CSV data into an array of objects.

$.csv.toObjects(csv)

5. Possible plugin options.

$.csv.functionName(csv, {

  // separator
  separator:',',

  // delimiter
  delimiter:'"',

  // shows headers
  headers:true

});

6. Event handlers.

$.csv.functionName(csv, {

  onPreParse: function(){},

  onParseEntry: function(){},

  onParseValue: function(){},

  onPostParse: function(){},

});

7. Use the plugin in node.js.

var fs = require('fs');
var $ = jQuery = require('jQuery');
require('src/jquery.csv.js');

var sample = 'sample.csv';
fs.readFile(sample, 'UTF-8', function(err, csv) {
  $.csv.toArrays(csv, {}, function(err, data) {
    for(var i=0, len=data.length; i<len; i++) {
      console.log(data[i]);
    }
  });
});

Changelog:

v1.0.4 (2019-06-21)

  • Bugfixed

v1.0.0 (2019-05-03)

  • fixed a bug where onParseValue was mistakenly being applied to the toObjects header fields
  • fixed a bug where onPreParse wasn't updating the input csv string
  • fixed errors to call throw Error instead of throw new Error
  • fixed bug where that broke the Node.js callback convention when an empty options object was supplied

v0.9.1 (2019-05-01)

  • Cleanup

2019-02-27

  • v0.8.12

This awesome jQuery plugin is developed by typeiii. For more Advanced Usages, please check the demo page or visit the official website.