Hi there,
I’m currently building a help center on Weweb, in which I have topics each with several articles on the subject.
I’ve used Airtable for my “Topics” and “Articles” databases, in which I’ve created a view of published articles only. My “Articles” table has a column for other articles in the same category. I then used this view to create a collection page to present my articles.
My problem is that when I want to display the other articles in my page, it returns the IDs of all the articles in my column in Airtable. However, I’d like to display only the articles that are in “Published” status.
I’ve tried using the filter, but it returns the values for all the articles directly and I can’t do much with it.
So I guess I’d have to use custom JS to filter, but that’s where my knowledge ends.
Could someone help me cross-reference these IDs with the “Articles” collection so as to keep only those that are published?
Hi everyone,
I found my answer using Javascript.
For those who are interested, here is the code I used:
return globalContext.page?.['data']?.['Articles']
?.map(id => collections['7a4d046a-879b-4bd8-886c-926bbb64d4c9']?.['data']?.find(item => item.Record_ID === id && item.Status === 'Publié'))
.filter(item => item);
Explanations
globalContext.page?.['data']?.['Articles']
return the list of other articles from my current collection page
collections['7a4d046a-879b-4bd8-886c-926bbb64d4c9']
is my collection “Articles” returning all articles filtered by status “Published”.
Using the function map()
to browse each ID in the returned array
find()
searches the “Articles” collection for an object whose “Record_ID” column corresponds to the ID, and whose “Status” column is equal to “Published”.
.filter(item => item)
removes “falsy” results (such as undefined), which may occur if no matching item is found or if the item has the wrong status.