3

I am running into a problem here. I parse a .CSV file and I need to show them into a DataTables format.

The problem is The markups are all inside javascript as shown below

    $("#upload").bind("click", function () {
        var regex = /^([a-zA-Z0-9\s_\\.\-:])+(.csv|.xlsx|.xls)$/;
        if (regex.test($("#fileUpload").val().toLowerCase())) {
            if (typeof (FileReader) != "undefined") {
                var reader = new FileReader();
                reader.onload = function (e) {
                    var table = $("<table id='comp-monitor' class='table table-condensed table-striped'> \n\
            <tr><th>No.</th><th>DESKRIPSI ASSEMBLY</th><th>Drawing No.</th><th>QTY3</th><th>WEIGHT</th><th>-</th></tr>");
                    var rows = e.target.result.split("\n");
                    var no = 0;
                    for (var i = 0; i < rows.length; i++) {
                        no++;
//                        console.log(i);
                        var row = $("<tr />");
                        var cells = rows[i].split(",");
                        // Column No
                        var cell_no = $("<td />");
                        cell_no.html(no);
                        row.append(cell_no);

                        for (var j = 0; j < cells.length; j++) {
                            var cell = $("<td />");
                            cell.html(cells[j]);
                            row.append(cell);
                        }

                        table.append(row);
                    }
                    $("#dvCSV").html('');
                    $("#dvCSV").append(table);
                }
                reader.readAsText($("#fileUpload")[0].files[0]);

            } else {
                alert("This browser does not support HTML5.");
            }
        } else {
            alert("Please upload a valid CSV file.");
        }

    });

and the jquery initialization is just outside that function

$('#comp-monitor').DataTable();

Somehow the datatables doesn't run and I need some help with this where to put the initialization.

Thanks a bunch,

2
  • 1
    do you get any error msg? Commented Oct 8, 2015 at 7:27
  • no error. its just the data doesnt fill up and show as a datatable format Commented Oct 8, 2015 at 7:28

2 Answers 2

3

You can Initialize the datatable inside javascript itself

$("#upload").bind("click", function () {
    var regex = /^([a-zA-Z0-9\s_\\.\-:])+(.csv|.xlsx|.xls)$/;
    if (regex.test($("#fileUpload").val().toLowerCase())) {
        if (typeof (FileReader) != "undefined") {
            var reader = new FileReader();
            reader.onload = function (e) {
                var table = $("<table id='comp-monitor' class='table table-condensed table-striped'> \n\
            <tr><th>No.</th><th>DESKRIPSI ASSEMBLY</th><th>Drawing No.</th>    <th>QTY3</th><th>WEIGHT</th><th>-</th></tr>");
                var rows = e.target.result.split("\n");
                var no = 0;
                for (var i = 0; i < rows.length; i++) {
                    no++;
//                        console.log(i);
                    var row = $("<tr />");
                    var cells = rows[i].split(",");

                    // Column No
                    var cell_no = $("<td />");
                    cell_no.html(no);
                    row.append(cell_no);

                    for (var j = 0; j < cells.length; j++) {
                        var cell = $("<td />");
                        cell.html(cells[j]);
                        row.append(cell);
                    }

                    table.append(row);
                }
                $("#dvCSV").html('');
                $("#dvCSV").append(table);
//HERE
               $('#comp-monitor').DataTable();

            }
            reader.readAsText($("#fileUpload")[0].files[0]);

        } else {
            alert("This browser does not support HTML5.");
        }
    } else {
        alert("Please upload a valid CSV file.");
    }

});

let me know if this work.

Sign up to request clarification or add additional context in comments.

2 Comments

It doesnt work.. it shows the data table when i enter the page but when i click upload the data doesnt fill up the datatable
@KonzMama You can try Using the onloadend() on reader object and inside that initialize the datatable as reader.onloadend = function(e){ $('#comp-monitor').DataTable(); }
1

I found an answer by using addrow

var t = $('#comp-monitor').DataTable({

    });
    $("#upload").bind("click", function () {
        var regex = /^([a-zA-Z0-9\s_\\.\-:])+(.csv|.xlsx|.xls)$/;
        if (regex.test($("#fileUpload").val().toLowerCase())) {
            if (typeof (FileReader) != "undefined") {
                var reader = new FileReader();
                reader.onload = function (e) {
                    var table = $("#comp-monitor tbody");
                    var rows = e.target.result.split("\n");
                    var no = 0;
                    table.empty();
                    for (var i = 0; i < rows.length; i++) {
                        no++;
                        var row = $("<tr class='odd' role='row' />");
                        var cells = rows[i].split(",");
                            t.row.add([
                                no,
                                cells[0],
                                cells[1],
                                cells[2],
                                cells[3],
                                cells[4]
                            ]).draw(false);
                        table.append(row);
                    }
                }
                reader.readAsText($("#fileUpload")[0].files[0]);

            } else {
                alert("This browser does not support HTML5.");
            }
        } else {
            alert("Please upload a valid CSV file.");
        }
    });

Comments

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.