No data available in table

No data available in table

cris82micris82mi Posts: 6Questions: 2Answers: 0
edited July 2016 in Free community support

Hi,
I'm try to fill a table dinamically with the followin code, but I get the message "No data available in table" the variable t in correctly filled, can please help me?

var t = $('#opensig').DataTable();
            <% for (int u = 0; u < ticket_pos.length; u++) {%>
            <%if (country_pos[u] == null) {
                    country_pos[u] = "";
                }%>
           t.row.add(['<%=ticket_pos[u]%>','<%=strategy_pos[u]%>', '<%=symb_pos[u]%>', '<%=opentime_pos[u]%>', '<%=tip_pos[u]%>', '<%=size_pos[u]%>', '<%=price_pos[u]%>', '<%=actualprice_pos[u]%>', '<%=swap_pos[u]%>', '<%=profit_pos[u]%>', '<%=type_pos[u]%>', '<%=detail_pos[u]%>','<%=exposition_pos[u]%>', '<%=dateupdate_pos[u]%>', '<%=country_pos[u]%>']).draw();
           
            <% }%>

            $(document).ready(function () {
                $('#opensig').DataTable({
                    data: t,
                    AutoWidth: false,
                    columns: [
                        {title: "Order ID"},
                        {title: "Strategy ID"},
                        {title: "Symbol"},
                        {title: "Open Time"},
                        {title: "Long / Short"},
                        {title: "Size"},
                        {title: "Open Price"},
                        {title: "Actual Price"},
                        {title: "Swap"},
                        {title: "Profit"},
                        {title: "Type"},
                        {title: "Detail"},
                        {title: "Field"},
                        {title: "Exposition"},
                        {title: "Date Update"},
                        {title: "Country"}
                    ],
                    columnDefs: [{
                            orderable: false,
                            targets: 0
                        }],
                    autoFill: false,
                    colReorder: false,
                    select: true,
                    dom: 'Blfrtip',
                    buttons: ['copy', 'csv', 'excel', 'pdf', 'print', 'colvis']
                });
            });

Answers

  • Tom (DataTables)Tom (DataTables) Posts: 139Questions: 0Answers: 26

    There are a few issues-

    • You might want to use draw() outside of the loop as you will take a performance hit calling it for every row added.
    • On line 12 you are passing in a datatable as your data for the table which makes no sense as you want to pass in an array of data.
    • There are two initialisations of the datatable.

    If you can access ticket_pos as a JSON array you can use that as your data property.

    Thanks

    Tom

  • cris82micris82mi Posts: 6Questions: 2Answers: 0

    Hi Tom,
    I'm trying with a JSON array but I get an error, could you please help me?

    Error:
    WEB_Geomap_Servlet:250 Uncaught ReferenceError: json_data1 is not defined

            <% String[][] opentab = new String[ticket_pos.length][15];
    
                for (int u = 0; u < ticket_pos.length; u++) {
    
                    opentab[u][0] = ticket_pos[u];
                    opentab[u][1] = strategy_pos[u];
                    opentab[u][2] = symb_pos[u];
                    opentab[u][3] = opentime_pos[u];
                    opentab[u][4] = tip_pos[u];
                    opentab[u][5] = Double.toString(size_pos[u]);
                    opentab[u][6] = Double.toString(price_pos[u]);
                    opentab[u][7] = Double.toString(actualprice_pos[u]);
                    opentab[u][8] = Double.toString(swap_pos[u]);
                    opentab[u][9] = Double.toString(profit_pos[u]);
                    opentab[u][10] = type_pos[u];
                    opentab[u][11] = detail_pos[u];
                    opentab[u][12] = Double.toString(exposition_pos[u]);
                    opentab[u][13] = dateupdate_pos[u];
                    opentab[u][14] = country_pos[u];
                }%>
    
    
    
            <%JSONArray json_data1 = new JSONArray(opentab);%>
    
    
            $(document).ready(function () {
                $('#opensig').DataTable({
                    data: json_data1,
                    AutoWidth: false,
                    columns: [
                        {title: "Order ID"},
                        {title: "Strategy ID"},
                        {title: "Symbol"},
                        {title: "Open Time"},
                        {title: "Long / Short"},
                        {title: "Size"},
                        {title: "Open Price"},
                        {title: "Actual Price"},
                        {title: "Swap"},
                        {title: "Profit"},
                        {title: "Type"},
                        {title: "Detail"},
                        {title: "Exposure"},
                        {title: "Date Update"},
                        {title: "Country"}
                    ],
                    columnDefs: [{
                            orderable: false,
                            targets: 0
                        }],
                    autoFill: false,
                    colReorder: false,
                    select: true,
                    dom: 'Blfrtip',
                    buttons: ['copy', 'csv', 'excel', 'pdf', 'print', 'colvis']
                });
            });
    
  • Tom (DataTables)Tom (DataTables) Posts: 139Questions: 0Answers: 26

    Could you console.log json_data1 just before the datatables initialisation.

    You could try putting json_data1 into a javascript variable as it looks like it isn't at the moment.

    eg
    var json_data1 = <% new JSONArray( ... ); %>;

    You might need to echo the JSON Array or otherwise print it out.

    Thanks

    Tom

This discussion has been closed.