Print items in listview

i'm using a custom component to do a print button that prints items inside my listview. this is my iframe code

<script>
    // Function to generate cart items and total for printing
    function generatePrintContent() {
        // Replace this section with your dynamic cart item generation
        let cartItems = "";
        for (let i = 0; i < listView1.data.length; i++) {
            const title = listView1.data[i].retoolInternal_rowKey.title;
            const size = listView1.data[i].retoolInternal_rowKey.unit_of_measure.toLowerCase();
            const qty = document.querySelector("#textInput30").value;
            const price = (listView1.data[i].retoolInternal_rowKey.selling_price * parseFloat(textInput31)).toLocaleString();
            cartItems += `<tr>
                            <td>${title} (${size})</td>
                            <td class="alignright">$ ${price}</td>
                          </tr>`;
        }

        // Get the total value
        const total = statistic4.value;

        // Generate the HTML content for printing
        const printContent = `
            <h2>Cart Items</h2>
            <table class="invoice-items">
                <tbody>
                    ${cartItems}
                    <tr class="total">
                        <td class="alignright" width="80%">Total</td>
                        <td class="alignright">$ ${total}</td>
                    </tr>
                </tbody>
            </table>
        `;

        return printContent;
    }

    // Function to handle the print button click
    function printCart() {
        const printWindow = window.open('', '', 'width=600,height=600');
        printWindow.document.open();
        printWindow.document.write(`
            <html>
            <head>
                <title>Print Cart</title>
            </head>
            <body>
                ${generatePrintContent()}
            </body>
            </html>
        `);
        printWindow.document.close();
        printWindow.print();
        printWindow.close();
    }

    // Add an event listener to the Print button
    document.getElementById("printButton").addEventListener("click", printCart);
</script>

please can someone help me out.

![image|690x236](upload://rhOHC4C0IhwVYLtwzl5aIBacEIv.png)

seems the normal js doesnt work without using customcomponent or am i wrong.


function generatePrintContent() {
    
    let cartItems = "";
    for (let i = 0; i < listView1.data.length; i++) {
        const title = listView1.data[i].retoolInternal_rowKey.title;
        const size = listView1.data[i].retoolInternal_rowKey.unit_of_measure.toLowerCase();
        const qty = document.querySelector("#textInput30").value;
        const price = (listView1.data[i].retoolInternal_rowKey.selling_price * parseFloat(textInput31)).toLocaleString();
        cartItems += `<tr>
                        <td>${title} (${size})</td>
                        <td class="alignright">$ ${price}</td>
                      </tr>`;
    }

    // Get the total value
    const total = statistic4.value;

    // Generate the HTML content for printing
    const printContent = `
        <h2>Cart Items</h2>
        <table class="invoice-items">
            <tbody>
                ${cartItems}
                <tr class="total">
                    <td class="alignright" width="80%">Total</td>
                    <td class="alignright">$ ${total}</td>
                </tr>
            </tbody>
        </table>
    `;

    return printContent;
}

// Function to handle the print button click
function printCart() {
    const printWindow = window.open('', '', 'width=600,height=600');
    printWindow.document.open();
    printWindow.document.write(`
        <html>
        <head>
            <title>Print Cart</title>
        </head>
        <body>
            ${generatePrintContent()}
        </body>
        </html>
    `);
    printWindow.document.close();
    printWindow.print();
    printWindow.close();
}



@27shutterclicks

Hi @WolfSheep,

All JS in Retool is run in a sandbox, so a lot of window functions will not be able to escape out. Some users will use a custom component to get around this limitation. (window.print() will not work in a JS query, but it will work to print the contents of a custom component when run in a custom component's script).