I have two radio buttons, two datepickers, a button and a repeater on my page and the following code that will fill the repeater. Everything works fine in when hit the button after selecting the start and end dates right after page load in Preview mode. However, when I change the selection in radio button I get many lines of errors and it give reference to a particular line of code and which is,
$w("#rptReport").data = results.items;
Note: it runs without errors on first choice radio button, no matter which option I have chosen. On changing the selection, I get the error. Yet, I get the result in repeater.
I try adding the following line of code in button click to see if this will fix the error, but I didn’t.
$w('#rptReport').data = []
Following is the code for button click
export function button7_click() {
$w('#rptReport').data = []
if ($w("#rdoReportFor").value==="Buy Orders") {
findBuyOrdersBtwDates()
}else if ($w("#rdoReportFor").value==="Sell Orders") {
findSellOrdersBtwDates()
}else{
console.log("Both cannot be displayed at the moment, to be coded")
}
}
Following is the complete code of one of the fuctions findBuyOrdersBtwDates().
Code for the function findSellOrdersBtwDates() is not included in the question as both these functions are same except some field names in the database are different.
I do not know if there is a better way to do the same, please tell me the correct way to do it as I am a beginner.
function findBuyOrdersBtwDates() {
let startDate = $w('#startDate').value;
let endDate = $w('#endDate').value;
let yearValue = startDate.getFullYear();
let monthValue = startDate.getMonth();
let dayValue = startDate.getDate();
let startDateSrch = new Date(yearValue, monthValue, dayValue, 0, 0, 0);
yearValue = endDate.getFullYear();
monthValue = endDate.getMonth();
dayValue = endDate.getDate();
let endDateSrch = new Date(yearValue, monthValue, dayValue, 23, 59, 59);
wixData.query("database")
.between('bDate', startDateSrch, endDateSrch)
.find()
.then((results) => {
if (results.items.length > 0) {
let compName = new Array()
let investment = new Array()
for (let i = 0; i < results.length; i++) {
compName[i] = results.items[i].companyName;
investment[i] = results.items[i].bNetPrice;
}
let data = [
[investment],
[compName],
];
$w('#html1').postMessage(data);
console.log(data)
$w('#html1').onMessage(async (event) => {
if (event.data.type === 'ready') {
$w('#html1').postMessage(data);
}
});
$w("#rptReport").data = results.items;
} else {
$w("#message").show()
console.log("item not found between these dates")
}
})
.catch((error) => {
let errorMsg = error.message;
let code = error.code;
});
setTimeout(() => {
$w("#rptReport").onItemReady(($item, itemData, index) => {
$item("#txtBuyDate").text = itemData.bDate.toLocaleDateString();
$item("#txtCompName").text = itemData.companyName.toString();
$item("#txtQty").text = itemData.bQuantity.toString();
$item("#txtUnitPrice").text = itemData.bUnitPrice.toString();
$item("#txtInvestment").text = itemData.bOrderCost.toString();
$w("#columnStrip1").show()
$w("#rptReport").show()
});
}, 500);
}
I hope my question is clear
Thanks in advance.