I created an object simulating the JSON return I get from the database.
Problem:
- I can't find
invoices
for thedrop-down list
element of the selectedcontract
when loading a function. - I cannot list the
invoices
when changing an option in the<select>
.
Example
let lblContract = document.querySelector('#contracts');
let UlInvoices = document.querySelector('#ul-invoices');
let contractInAttendance = 1;
const objectContract = {
id: 1,
nome: 'wagner',
contracts: [{
id: 1,
contract: '123456',
batches: [{
id: 1,
contract_id: 1,
batch: '1',
invoices: [{
value: 10,
batch_id: 1,
}]
},
{
id: 2,
contract_id: 1,
batch: '2',
invoices: [{
value: 10,
batch_id: 2,
}]
}
]
},
{
id: 2,
contract: '246789',
batches: [{
id: 3,
contract_id: 2,
batch: '1',
invoices: [{
value: 20,
batch_id: 3,
}]
}]
}
]
}
const revelling = (function() {
function privateInit() {
const contracts = objectContract.contracts;
let contractFilteredById = contracts.filter(contract => contract.id === contractInAttendance);
for (const contract of contracts) {
const selectedContract = contract.id === contractFilteredById[0].id ? 'in-attendance' : '';
let htmlForBatchsOptions = contract.batches.map(batch => `<option value=${batch.id}>${batch.batch}</option>`).join('');
lblContract.innerHTML +=
`<div class="contract-${selectedContract}" style="display: flex;">
<div id="contract-${contract.contract}" data-contract="${contract.id}" class="loren">
<span>${contract.contract}</span>
</div>
<div class="ipsulum" style="margin-left: 5px;">
<select class="sel-batch">
${htmlForBatchsOptions}
</select>
</div>
</div>`;
const batchOption = lblContract.querySelector('select.sel-batch');
batchOption.addEventListener('change', getInvoices);
//batchOption.value = 2;
}
}
function getInvoices() {
const batchValue = event.target.value
console.log(batchValue);
//console.log(this.value);
}
return {
init: privateInit
}
})();
revelling.init();
<label id="contracts"></label>
<ul id="ul-invoices"></ul>
Update:
I did it, but I didn't find it very pleasant and there is certainly a simpler way to do it. I would like another opinion.
let lblContract = document.querySelector('#contracts');
let UlInvoices = document.querySelector('#ul-invoices');
let contractInAttendance = 1;
const objectContract = {
id: 1,
nome: 'wagner',
contracts: [{
id: 1,
contract: '123456',
batches: [{
id: 1,
contract_id: 1,
batch: '1',
invoices: [{
id: 1,
batch_id: 1,
invoice: 1,
due: '2019-01-01',
value: 10
},
{
id: 2,
batch_id: 1,
invoice: 2,
due: '2019-02-01',
value: 10
}
]
},
{
id: 2,
contract_id: 1,
batch: '2',
invoices: [{
id: 3,
batch_id: 2,
invoice: 3,
due: '2019-03-01',
value: 10
}, ]
}
]
},
{
id: 2,
contract: '654321',
batches: [{
id: 3,
contract_id: 2,
batch: '1',
invoices: [{
id: 4,
batch_id: 3,
invoice: 1,
due: '2019-01-01',
value: 20
},
{
id: 5,
batch_id: 3,
invoice: 2,
due: '2019-02-01',
value: 20
}
]
}]
}
]
}
const revelling = (function() {
'use strict';
function privateInit() {
const contracts = objectContract.contracts;
let contractFilteredById = contracts.filter(contract => contract.id === contractInAttendance);
let item = '';
for (const contract of contracts) {
const selectedContract = contract.id === contractFilteredById[0].id ? '-in-attendance' : '';
lblContract.innerHTML +=
`<div class="contract${selectedContract}" style="display: flex;">
<div id="contract-${contract.contract}" data-contract="${contract.id}" class="loren">
<span>${contract.contract}</span>
</div>
<div class="ipsulum" style="margin-left: 5px;">
<select class="sel-batch">
${contract.batches.map(batch => `<option value=${batch.id}>${batch.batch}</option>`).join('')}
</select>
</div>
<span></span>
</div>`;
}
let lblContractSelected = lblContract.querySelector('.contract-in-attendance');
const batchOption = lblContractSelected.querySelector('select.sel-batch');
let batchValue = batchOption.options[batchOption.selectedIndex].value;
batchOption.addEventListener('change', getInvoices);
function getInvoices(event = false) {
if (event)
batchValue = event.target.value
const batches = contractFilteredById[0].batches;
const batch = batches.filter(batch => batch.id == batchValue);
const invoices = batch[0].invoices;
const invoice = invoices.map(function(item, indice) {
return `<tr>
<!--<td>${indice + 1}</td>-->
<td>${item.invoice}</td>
<td>${item.value}</td>
<td>${item.due}</td>
</tr>`;
});
document.querySelector('#tb-invoices tbody').innerHTML = invoice.join('');
}
getInvoices();
}
return {
init: privateInit
}
})();
revelling.init();
.contract-in-attendance > span {
width: 8px;
height: 8px;
margin: 5px;
background: #00b070;
border-radius: 50%;
display:inline-block;
}
<label id="contracts"></label>
<table id="tb-invoices" style="display:none-">
<thead>
<tr>
<th>invoice</th>
<th>value</th>
<th>due</th>
</tr>
</thead>
<tbody>
</tbody>
</table>