How to add custom tr to datatable which is created in my vanilla javascript ?

How to add custom tr to datatable which is created in my vanilla javascript ?

abdul2004abdul2004 Posts: 1Questions: 1Answers: 0
edited August 2022 in Free community support
const tableGainers = document.getElementById('table-market');


        for (const gainers of obj) {

            const tr = document.createElement('tr');
            const contentArr = `<td><img src=${gainers.urlLogo}  alt="logo_coin" class="logo-coin-tb"/>${gainers.symbol}</td>
                             <td>${gainers.lastPrice}</td>
                             ${gainers.priceChangePercent > 0 ? `<td style="color: green"> ${gainers.priceChangePercent}% </td>` : `<td style="color: red"> ${gainers.priceChangePercent}% </td>`}
                             <td>${gainers.volume}</td>
                             <td><button type="button" class="btn-go-trade">Go Trading</button></td>`;

            tr.innerHTML = contentArr;
            tr.className = 'tableRow';
            tr.id = 'tblmarket-row';
            tableGainers.appendChild(tr);
        }

obj is my json array

Edited by Allan - Syntax highlighting. Details on how to highlight code using markdown can be found in this guide.

Answers

  • allanallan Posts: 63,534Questions: 1Answers: 10,475 Site admin

    I'm not clear on what your question is here I'm afraid?

    Allan

  • allanallan Posts: 63,534Questions: 1Answers: 10,475 Site admin

    Oh - just a minute - are you asking how to add that tr to a DataTable? Use row.add() it will accept a tr as long as it is in the structure configured for the table (i.e. correct number of columns, etc).

    Personally I'd just use row.add() with the raw data and use renderers in the DataTable to format it as you need.

    Allan

Sign In or Register to comment.