I didn’t realise you could pass in filters as data. That’s good.
Say thank you to ChatGPT
There was a problem with pagination.
This should work.
/**************************************************************************************** */
/*
Filtrerer ansatte i listen som dukker opp når du skal registrere nye påmelding K+
*/
$(document).on('knack-view-render.view_235', async function(event, view, data) {
const projectId = getProjectIdFromURL();
if (!projectId) {
console.error('Kunne ikke finne prosjekt-ID.');
return;
}
console.log('Prosjekt-ID funnet: ' + projectId);
try {
const orgLinkRecords = await getOrganisationLinks(projectId);
if (orgLinkRecords.length > 0) {
const employees = await getEmployeesForOrganisations(orgLinkRecords);
updateChosenDropdown(employees);
} else {
updateChosenDropdown([]);
console.log('Ingen organisasjonskoblinger funnet for prosjektet.');
}
} catch (error) {
console.error('En feil oppsto:', error);
}
});
async function getOrganisationLinks(projectId) {
const sceneId = 'scene_150';
const viewId = 'view_252';
const filters = {
"match": "and",
"rules": [{
"field": "field_41",
"operator": "is",
"value": projectId
}]
};
const orgLinkRecords = await fetchRecords(sceneId, viewId, filters);
return orgLinkRecords.map(record => record.field_42_raw[0].id);
}
async function fetchRecords(sceneId, viewId, filters, page = 1) {
const userToken = Knack.getUserToken();
const pageSize = 100;
const apiUrl = `https://api.knack.com/v1/pages/${sceneId}/views/${viewId}/records`;
const response = await $.ajax({
url: apiUrl,
type: 'GET',
headers: {
'Authorization': userToken,
'X-Knack-Application-Id': Knack.application_id,
'Content-Type': 'application/json'
},
data: {
page: page,
rows_per_page: pageSize,
filters: JSON.stringify(filters)
}
});
if (response.current_page < response.total_pages) {
return response.records.concat(await fetchRecords(sceneId, viewId, filters, page + 1));
} else {
return response.records;
}
}
async function getEmployeesForOrganisations(orgIds) {
const employees = [];
for (const orgId of orgIds) {
const sceneId = 'scene_150';
const viewId = 'view_251';
const filters = {
"match": "and",
"rules": [{
"field": "field_48",
"operator": "is",
"value": orgId
}]
};
employees.push(...await fetchRecords(sceneId, viewId, filters));
}
return employees;
}
function updateChosenDropdown(employees) {
var $select = $('#view_235-field_72');
$select.empty();
if (employees.length > 0) {
$select.append($('<option>').val('').text('Velg en ansatt'));
employees.forEach(employee => {
$select.append($('<option>').val(employee.id).text(employee.field_246)); // Bruker field_246 som inneholder fullt ansattnavn
});
} else {
$select.append($('<option>').text('Ingen ansatte funnet'));
}
$select.trigger('liszt:updated'); // Bruker 'liszt:updated' som er korrekt for din Chosen-plugin
}
function getProjectIdFromURL() {
const hash = window.location.hash;
const match = hash.match(/view-kp-project-details\/([a-f0-9]+)/);
return match ? match[1] : null;
}
1 Like
Ok good to know. Thank you.
I haven’t come across this yet so I appreciate the heads up.