top of page
Forum Posts
Hugo Ribeiro
Jun 28, 2024
In Apps & Templates
Hi guys,
Im trying to implement the pdf generator but in the final part that im doing its becaming a nightmare i have 2 repeaters that need to be displayed but it dont do it on the pdf here is the code please help:
This one shows the code function
This one shows the pdf in blank
import { session } from 'wix-storage';
import wixData from 'wix-data';
$w.onReady(function () {
// Retrieve `queryResults` from session storage
let queryResults2 = JSON.parse(session.getItem('queryResults'));
console.log("Data on Session", queryResults2);
// Preprocess data to remove repetitions and handle undefined values
let uniqueMesRelatorio = Array.from(new Set(queryResults2.map(item => item.mesRelatorio))).filter(Boolean);
let uniqueAnoRelatorio = Array.from(new Set(queryResults2.map(item => item.anoRelatorio))).filter(Boolean);
// Set the values of input1 and input2 with the unique and non-undefined values
$w('#input1').value = uniqueMesRelatorio.join(', '); // Join if there are multiple values
console.log("Mes de relatorio", uniqueMesRelatorio);
$w('#input2').value = uniqueAnoRelatorio.join(', '); // Join if there are multiple values
console.log("Ano de relatorio", uniqueAnoRelatorio);
});
function search() {
// Getting input values
let mes = $w('#input1').value;
let ano = $w('#input2').value;
// Helper function to get unique items by a key
function getUniqueItems(items, key) {
const uniqueKeys = new Set();
return items.filter(item => {
const isNew = !uniqueKeys.has(item[key]);
if (isNew) {
uniqueKeys.add(item[key]);
}
return isNew;
});
}
// Creating the query
let query = wixData.query("PdfData")
.eq("mesRelatorio", mes)
.and(wixData.query("PdfData").eq("anoRelatorio", ano));
// Executing the query
query.find()
.then(results => {
if (results.items.length > 0) {
// Getting IDs of items found
let itemIds = results.items.map(item => item._id);
// Filtering the datasetPdfData to match the found IDs
return $w('#datasetPdfData').setFilter(wixData.filter().hasSome("_id", itemIds))
.then(() => $w('#datasetPdfData').refresh())
.then(() => results.items);
} else {
console.log("No results found");
// Clearing repeater data
$w('#rptViewDados').data = [];
$w('#rptPresentation').data = [];
return Promise.reject("No results found");
}
})
.then(items => {
// Getting unique items for rptPresentation
let uniqueItems = getUniqueItems(items, 'yourUniqueKey'); // replace 'yourUniqueKey' with the actual key
// Setting the repeater data to show filtered results
$w('#rptViewDados').data = items;
$w('#rptPresentation').data = uniqueItems.slice(0, 1); // Display only 1 unique item
})
.catch(err => {
console.error("Query or data fetch failed", err);
});
}
export function input2_change(event) {
search()
}
export function input1_change(event) {
search()
}
0
3
25
Hugo Ribeiro
More actions
bottom of page