Rahul Gopi
Rahul Gopi

Reputation: 412

Search and match CSV file values with javascript

I've uploaded a CSV-file to an HTML page via javascript. The CSV rows are: name and email-address, e.g. rambo,[email protected].

How to SEARCH the 'name' from these loaded CSV-file?

Also, one of the data is an email-address and I want to send a mail to that email-address. Is that value retrieved to a variable?

My code to search each elements:

function Search() {
 var fileUpload = document.getElementById("fileUpload");
var regex = /^([a-zA-Z0-9\s_\\.\-:])+(.csv|.txt)$/;
if (regex.test(fileUpload.value.toLowerCase())) {
    if (typeof (FileReader) != "undefined") {
        var reader = new FileReader();
        reader.onload = function (e) {
          var table = document.createElement("table");
var rows = e.target.result.split("\n");
for(var i = 0; i < rows.length; i++)
{   
    var row = table.insertRow(-1); 
    var cells = rows[i].split(",");
    for(var j = 0; j < cells.length; j++)
    {
        var cell = row.insertCell(-1); 
      //  cell.innerHTML = cells[j];


    // Here repeated checkboxes:
    var radio = document.createElement('input');
    radio.type = 'checkbox';
    radio.name = 'check';

} 

var ser=document.getElementById("texts");
    if(cells[i].indexOf(ser))
    {
     alert("matches");
       cell.innerHTML = cells[i];
    }
    else
    {
     alert("unmatches");
    }

    var cell = row.insertCell(-1);
    cell.appendChild(radio);
    //cell.appendChild(button);
}
 var button = document.createElement('button');
    button.textContent = 'Send';
cell.appendChild(button);
button.onclick = function(){ alert();};

var dvCSV = document.getElementById("dvCSV");
dvCSV.innerHTML = "";
dvCSV.appendChild(table);
        }

        reader.readAsText(fileUpload.files[0]);

    } 
   }
}

Upvotes: 2

Views: 4786

Answers (1)

Stephan Weinhold
Stephan Weinhold

Reputation: 1643

Ad search: indexOf() is your friend here. This should give you a figure:

var table = $('#your-table'),
    searchstring = 'your-searchstring';
searchstring.toLowerCase();
for (var i = 0, cell; cell = table.cells[i]; i++) {
    if (cell.indexOf(searchstring)) {
        // I don't know what you want to do with the search-results...
        // ..but you can do it here.
    }
}

Ad email-address: you can add the address to a variable in your CSV-import:

var cells = rows[i].split(","),
    address = cells[1];

I'd suggest making an array addresses and fill it each row.

Upvotes: 1

Related Questions