2

I am not sure this is complex to be solve but it is related between Problem 1 and 2. I have no idea to solve this at this moment. I am stuck here for 4 days. :-(

  1. I have thousand entries to be export to excel.
  2. I have <table> tag which will have multiple rows inside each datatable row.
  3. I need to export all the datatable rows (including <table> rows).
  4. I try to use build-in export function but the result is not as requested (it become straight line), so I need to create another function (exportNotFound) which will fetch all datatable rows to be export in excel.

Some References:

  1. https://datatables.net/examples/api/counter_columns.html (Not working as expected)
  2. Add row number column to jquery datatables (Tried but after export got white blank excel)

At last I just do as simple as below to have a numbering for each row, however the numbering is not sorting properly. At console the numbering is sorting properly.

Problem:

  1. How to sort the number properly?
  2. Why the current page (by default show 10 items) become blank and the page load too many rows instead of 10 items after trigger exportNotFound function?

Console

enter image description here

Current result table after export. Sorting number is not properly.

enter image description here

Blank items dropdown after trigger exportNotFound function.

enter image description here

Suddenly it load additional multiple rows after trigger exportNotFound function.

enter image description here

<table id="table2">
    <thead>
        <tr>
            <th>Not Found in Grand</th>
        </tr>
    </thead>
</table>

JS

var result2 = [];
var count2 = 1;
$.each(response.not_found, function(k1, v1) {
    if (v1.router != null){
        var obj = {};
        obj['result_not_found'] = v1.router;
        obj['count2'] = count2++;
        result2.push(obj);
    }
});
resultTable2(result2);

function resultTable2(result2){
    $('#table2').DataTable({
        buttons: [{
            action: function () {
                exportNotFound();
            },
        }],
        columns: [
            {
                "data": "result_not_found",
                render: function(data, type, row) {
                    var a = "<table>";
                        a += "<tr><td><span>("+row.count2+")</span>SERVICE NO:</td><td>"+data.service_number+"</td></tr>";
                        a += "<tr><td>MSE ID:</td><td>"+data.mse_id+"</td></tr>";
                        a += "<tr><td>MSE INTERFACE:</td><td>"+data.mse_interface+"</td></tr>";
                        a += "<tr><td>VLAN:</td><td>"+data.vlan+"</td></tr>";
                        a += "<tr><td>BANDWIDTH:</td><td>"+data.bandwidth+"</td></tr>";
                    return a + "</table></br>";
                }
            }
        ],
    });
}

function exportNotFound() {
    var BOM = "\uFEFF";
    var table = $('#table2').DataTable();
    var pages = table.page.info().pages;
    table.page.len(-1).draw();
    var html = $("#table2")[0].outerHTML;
    table.page.len(pages).draw();
    window.open('data:application/vnd.ms-excel,' + encodeURI(BOM + html));
}
0

0

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.