0

In this code I am trying to render the csv data into tabular form after doing some processing on it. The result I am getting is in form:

enter image description here

whereas, I need to get the result like this:

enter image description here

I tried with ._zip_underscore and it is giving me an exception with underscore.js file. I am not sure how to do that. Please help. Code is below.

JavaScript

function execData(file) 
{
    var reader = new FileReader();
    reader.readAsText(file);
    reader.onload = function (event)
    {
        var csv = event.target.result;
        var data = $.csv.toArrays(csv);
        var data1 = data;
        var header = data1.shift();
        var numeric = 0;
        var str = 0;
        var dtr = new Array();


        for (var i = 0; i < data1[i].length; i ++ )
        {
            // alert("backa");
            for (var m = 0; m < data1[m].length; m ++ )
            {
                for (var j = 0; j < data1.length; j ++ )
                {
                   var r = data1[j][i];
                   dt(r);

                }
            dttop(m);


            }
        }

        function dt(x)
        {
            //  alert("here");
            if (isFinite(x) == true)
            {
                numeric ++ ;

            }

            else
            {
                str ++ ;

            }
        }

        function dttop(f)
        {

            if (numeric > str)
            dtr[f] = "n";
            else
            dtr[f] = "s";
        }



        //  alert(dtr);
        var final = dtr.concat(data1);

        // to render in a tabular format
        var html = '';

        for (var row in final)
        {
            html += '<tr>\n';

            for (var item in final[row])
            {
                html += '<td>' + final[row][item] + '</td>\n';
            }
            // alert(html);
            html += '</tr>\n';
        }

        $('#contents').html(html);
    };
    reader.onerror = function ()
    {
        alert('Unable to read ' + file.fileName);

    };
}

HTML

<div id=inputs class=clearfix>
    <input type=file id=files name=files[] multiple />
</div>
<hr />

<output id=list></output>
<hr />

<table id=contents style="width:100%; height:400px;"></table>

1 Answer 1

1

As far as I understand...

The structure of the data is printed as final[row][item], but the three first rows in dtr contain only one value each.

Place an array in the first position of dtr.

dtr[0] = new Array();

Your function should be now something more like:

function dttop(f)
{
    if (numeric > str)
        dtr[0][f] = "n";
    else
        dtr[0][f] = "s";
}
Sign up to request clarification or add additional context in comments.

2 Comments

is that a right 'dtr[0] = new Array();' as it is giving me a critical error says 'Expected ';''
Which line? The one that gave you the error? I didn't get that error myself so check if it's just a typo.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.