Creating a Datatable with a string

Creating a Datatable with a string

prox108prox108 Posts: 27Questions: 12Answers: 0

Hello, I created a table inside a string.
"<table><thead></thead><tbody><tr>cabezera 1<th>cabezera 2</th></tr><tr><td>data1</td><td>data1</td></tr></body></table>"

Do you know how can I create the table with this string?

This question has accepted answers - jump to:

Answers

  • kthorngrenkthorngren Posts: 21,327Questions: 26Answers: 4,949

    Your table syntax is incorrect:

    <table>
       <thead></thead>
       <tbody>
          <tr>
             cabezera 1
             <th>cabezera 2</th>
          </tr>
          <tr>
             <td>data1</td>
             <td>data1</td>
          </tr>
          </body>
    </table>
    

    Looks like the first tor you want as the header so it should be moved into the thead. Also the first cell ( cabezera 1 ) is missing the th tags. It should look like this:

    <table>
       <thead>
          <tr>
             <th>cabezera 1</th>
             <th>cabezera 2</th>
          </tr>
       </thead>
       <tbody>
          <tr>
             <td>data1</td>
             <td>data1</td>
          </tr>
          </body>
    </table>
    

    The Datatable can be initialized with new DataTable('table');. Running example:
    https://live.datatables.net/benigafi/1/edit

    Kevin

  • prox108prox108 Posts: 27Questions: 12Answers: 0

    What I am trying to do is initialitaze the Datable with this string, I am not trying to create it directly inside a html file but create inside a js file using the regular definition of Datatable. Do you know what propiete has datable to define all the strcuture of the datable?

  • kthorngrenkthorngren Posts: 21,327Questions: 26Answers: 4,949
    Answer ✓

    You can use jQuery or Javascript methods to insert the table into the document then initialize the Datatable. There isn't a configuration option to have Datatables insert the string for you.

    Kevin

  • prox108prox108 Posts: 27Questions: 12Answers: 0

    It sad, but thanks!

  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin
    Answer ✓

    Its a single line of jQuery:

    $('<table>...</table>').appendTo('body');
    

    Our if you don't want to use jQuery, set the innerHTML property of an element - also a single line of code.

    That is not something DataTables should provide.

    Allan

  • prox108prox108 Posts: 27Questions: 12Answers: 0

    I know, I alredy used some similar code

    $('#thistorico').html(d);

Sign In or Register to comment.