jexcel failed to read DB data

367 Views Asked by At

I can successfully load declared hard coded list of array but if I am going to read the data from DB through jquery then pass to jexcel with same object type result it gives me this error:

.jexcel is not a function

I could even print the same result but still having this problem, here my sample code:

$(document).ready(function () {    
        $.get("/Db/getModule", { 'table': 0 })
            .done(function (res) {
                console.log(res.data);  
                displayOutput(res.data);
            });
    });


    function displayOutput(data) {
        $('#my').jexcel({ data: data, colWidths: [100, 100, 100, 100, 100, 100] });
    }

And here is the working reference code:

data = [
    ['jExcel', 'Jquery spreadsheet, javascript spreadsheet, jquery', 181],
    ['Handsontable', 'Another nice javascript spreadsheet plugin', 9284],
    ['Datatables', 'DataTables is a table enhancing plug-in for the jQuery library.', 5164],
];

console.log(data);

$('#my').jexcel({ data: data, colWidths: [100, 100, 100] });

sample console output from reference and my result: enter image description here

Any suggestion/comments TIA

2

There are 2 best solutions below

4
Basil On

.jexcel is not a function: You have to keep in your mind that this error tells you that the jquery file of the jexcel is not loaded when you called it.

But if you are sure then i have a Suggestion:
Check Your server result if the JSON object is coming as text not an object then you have to serialize the text to JSON then send it to the display function using:

var res = JSON.parse(res.data);
displayOutput(res);
0
Paul On

You don't need to load the data, them load jexcel. You can specify to jexcel the data is from an external source. So, update your code as below:

$(document).ready(function () {
    $('#my').jexcel({ url: '/Db/getModule', colWidths: [100, 100, 100, 100, 100, 100] });
});