Add html elements with Javascript using loop

Asked

Viewed 63 times

1

Hello, Through a objeto, I need to create elements HTML using the loop javascript.

In the example code, and as the print, I realize that when adding batches (lotes), an item is also added undefined.

Well, I just need to display the contracts as if they were a list, and for each contract, add the respective lots into one dropdown list (select option), because then I will display the invoices in another list according to the selected lot.

sample code

<div id="div-contracts"></div>
<style>
#div-contracts {
    display: table-cell;
    width: 100%;
}
.loren {
    float:left;
    width: 45px;
}
.ipsulum {
    float:left;
    width: 35%;
}
</style>


<script>
let object = {
    name: 'wagner',
    contracts: [
        {
            id: 1,
            contract: '123',
            batches: [
                {
                    id: 1,
                    batch: '1',
                    invoices: [
                        {
                            value: 10,
                            due: '01/01/2020',
                        },
                        {
                            value: 10,
                            due: '01/02/2020',
                        },
                    ],
                },
                {
                    id: 2,
                    batch: '2',
                    invoices: [
                        {
                            value: 10,
                            due: '01/04/2020',
                        },
                        {
                            value: 10,
                            due: '01/05/2020',
                        },
                    ],
                },
            ]
        },
        {
            id: 1,
            contract: '456',
            batches: [
                {
                    id: 3,
                    batch: '1',
                    invoices: [
                        {
                            value: 15,
                            due: '01/01/2020',
                        },
                        {
                            value: 14,
                            due: '01/02/2020',
                        },
                    ],
                },
            ]
        },
    ]
};

let getById = (id, parent) => parent ? parent.getElementById(id) : getById(id, document);
const DOM =  {
    contract: getById('div-contracts'),
};

function htmlForBatchsOptions(contract) {
    let batchOptions;
    for (const batch of contract.batches) {            
        batchOptions += `<option value="${batch.id}">${batch.batch}</option>`;
    }
    return batchOptions;
    //return document.querySelector('select.sel-batch').innerHTML = batchOptions;
};

for (const contract of object.contracts) {
    DOM.contract.innerHTML +=
    `<div class="contract">
        <div class="loren">
            <span>${contract.contract}</span>
        </div>
        <div class="ipsulum">
            <select class="sel-batch">
                ${htmlForBatchsOptions(contract)}
            </select>
        </div>
    </div>`;
}
</script>

inserir a descrição da imagem aqui

1 answer

1


to solve Undefined problems start the variable with empty value before concatenating it:

Let batchOptions = '';

  • Another suggestion is: use onchange in select: <select class="sel-batch" onchange="getInvoices(this)"> Grab the selected id to find the selected Batche: Function getInvoices(selectObject){ var value = selectObject.value; } The rest I think you can give sequence.

  • Then load the invoices(Invoices) as soon as the page is loaded, even with you; inside the loop I added the filter let batchFilteredByContractId = contract.batches.filter(batch => batch.id === 1); And then I have access to the lot related invoices. Now I’m really struggling to load the invoices(Invoices) from the selected batch (batch), another thing I have difficulty with, is that I need to create an event to click on the contract and load all the data related to the contract I clicked on!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.