How to use java String[][] matrix as Dataset for Datatable?

How to use java String[][] matrix as Dataset for Datatable?

cris82micris82mi Posts: 6Questions: 2Answers: 0

Hi,
I would like to use a String[][] java matrix as Dataset for my DataTable.
I'm try the following but doesn't work, could you please help me?

I get Error:

WEB_Geomap_Servlet: 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']
            });
        });

Replies

  • allanallan Posts: 63,161Questions: 1Answers: 10,406 Site admin

    I'm afraid we can't help with the Java aspect here as this isn't a Java forum (and I know zero Java myself!), but if you console.log( json_data1 ); just before the table initialisation, what does it show in the browser's console?

    Allan

  • cris82micris82mi Posts: 6Questions: 2Answers: 0

    Hi Alan,
    thank you for you're reply, if I put the command console.log(json_data1); I get the error json_data1 is not defined.

    In any case I try to better explain my problem:

    I would like to have both the following feature simultaneously:

    1. Dinamically feel datatable dataset from Java;
    2. Visualize the export buttons in table, buttons: ['copy', 'csv', 'excel', 'pdf', 'print', 'colvis']

    At the moment I'm able to dinamically feel the dataset from Java using the following code:

        <script type="text/javascript" class="init">
            $(document).ready(function () {
                var t = $('#opensig').DataTable();
    
            <% for (int u = 0; u < ticket_pos.length; 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]%>', '<%=field_pos[u]%>', '<%=exposition_pos[u]%>', '<%=dateupdate_pos[u]%>', '<%=country_pos[u]%>']).draw();
    
            });
        </script>
    

    But with this sintax I dont't have any Idea of how insert the button for (CSV, PDF.... export)

    Otherwise if I use the code posted in the original message, I correctly see the buttons but the table is empty.

    I really appreciate any support or any idea of how to solve the problem.
    Thank you,
    Cristian.

  • allanallan Posts: 63,161Questions: 1Answers: 10,406 Site admin

    But with this sintax I dont't have any Idea of how insert the button for (CSV, PDF.... export)

    Do it like in the examples. Just because you are adding the data using row.add() doesn't mean how you initialise Buttons would change (it doesn't).

    Also I would strongly suggest you don't call draw() every single time in the loop. Just do it once after the loop.

    Allan

  • cris82micris82mi Posts: 6Questions: 2Answers: 0

    Hi Alan,
    with your suggestion everythings works properly.
    Many thanks for your's support.
    Cristian.

This discussion has been closed.