0

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

Syntax Rommel
  • 932
  • 2
  • 16
  • 40

2 Answers2

0

.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);
Basil
  • 1,613
  • 12
  • 25
  • I tried to see the typeof the reference and my result weird coz they are now both the same object. – Syntax Rommel Jan 08 '20 at 06:33
  • I attached my sample result above see the changes. the first line in console is the reference and the 2nd one is from DB result – Syntax Rommel Jan 08 '20 at 06:37
  • So the result type is not the issue because it's coming as object the only thing that we get from your error that the **jexcel** is not loaded when you call it. – Basil Jan 08 '20 at 06:40
  • yes I think. I am trying to find more reference but most example is just came from the hard coded object. Still thinking other way how could I read my data from DB then pass it to jexcel. – Syntax Rommel Jan 08 '20 at 08:09
0

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] });
});
Paul H.
  • 387
  • 4
  • 8