jeongho
jeongho

Reputation: 29

Add table row dynamically with JSON

I am having difficulty to add new table row with JSON.

Situation

Currently I am programming drag & drop function. Once file uploaded, file information parse into JSON and return to the View page. What I want is that I want to add new table row dynamically with JSON file information.

Problem How should I fill in the double quotes part with JSON format?

var rowNode = table.row.add( [
            "",
            "",
            "[what should i write here??]",
            "[what should i write here??]",
            "<td id=wtax_file_type></td>",
            "<td id=all_tax></td>",
            "<td id=tax_date></td>",
            "<button class='btn btn-xs btn-default'><i class='icon-file-pdf text-error'></i><span> 테스트.pdf</span></button>",
            "<a class='btn-link color-primary'>[이동]</a>"
        ] ).draw().node();

My JS code

myDropzone.on("success", function(file, res) {


        /* removew preview when upload success*/
        $(file.previewElement).remove();

        console.log(res);

        if (res.result) {
            if (res.data) {
                if (res.data.f_idx) {
                    $('#f_idx').text(res.data.f_idx);
                }
                if (res.data.client_name) {
                    $('#client_name').text(res.data.client_name);
                }
                if (res.data.client_biz_no) {
                    $('#client_biz_no').text(res.data.client_biz_no);
                }
                if (res.data.wtax_file_type) {
                    $('#wtax_file_type').text(res.data.wtax_file_type);
                }
                if (res.data.all_tax) {
                    $('#all_tax').text(res.data.all_tax);
                }
                if (res.data.tax_date) {
                    $('#tax_date').text(res.data.tax_date);
                }
            }
        }

        var fileObj=JSON.parse(res);


       var rowNode = table.row.add( [
            "",
            "",
            "[what should i write here??]",
            "what should i write here??]",
            "<td id=wtax_file_type></td>",
            "<td id=all_tax></td>",
            "<td id=tax_date></td>",
            "<button class='btn btn-xs btn-default'><i class='icon-file-pdf text-error'></i><span> 테스트.pdf</span></button>",
            "<a class='btn-link color-primary'>[이동]</a>"
        ] ).draw().node();


    });

Upvotes: 1

Views: 258

Answers (1)

Sebastien D
Sebastien D

Reputation: 4482

According to datatable's documentation, this could be done like this:

table.row.add( [
        "",
        "",
        "[what should i write here??]",
        "[what should i write here??]",
        $('#wtax_file_type').text(),
        $('#all_tax').text(),
        $('#tax_date').text(),
        "<button class='btn btn-xs btn-default'><i class='icon-file-pdf text-error'></i><span> 테스트.pdf</span></button>",
        "<a class='btn-link color-primary'>[이동]</a>"
    ] ).draw( false );

Upvotes: 2

Related Questions