If you want to display a subset of a list you can control what is
added to your webpage using any of the JSON get functions. The process
flow is similar. Pick your list, pick the appropriate get function (e.g.
CL.getPeopleJSON()
or CL.getGroupJSON()
and
then add the filter on the records returned.
We want to display a list of monographs with a DOI for the Keck Institute for Space Studies.
CL.getGroupJSON()
to use to display
lists
The main challenge is in understanding the records we’re getting back in our list. The based way to do get a sense of how to test the objects representing the records is to look at the JSON feed it self. The structure is an array of objects where each object is a publication. In this case they are monographs. Most modern web browsers have development tools built in. You can use these to pretty print the JSON to make it easier to understand. You can also use a web browser plugin like JSONView to do a similar thing. In our case each object that has a doi also has a “doi” field. We check to each object to see if this field exists (older records might not have it) and to make sure that it is not an empty string. If those two conditions are met then we can choose to display that object on the webpage.
We can break our problem down to two steps
Here is a JSON fragment to illustrate what it looks like. The three dots just mean we’re skipping over some of what is printed out.
[
{
...
"_Key": "91407",
...
"collection": "CaltechAUTHORS",
"creators": {
...
},
...
"doi": "10.26206/HQ9P-YW49",
...
},
...
]
Note the field called “doi”.
Step two, in our web page include the following.
<div id="group-filter-list">
The filter records by the presense of a DOI field.</div>
<script src="https://feeds.library.caltech.edu/scripts/CL.js">
</script>
<script>
/* We get a handle on our page element */
let group_filter_list = document.getElementById("group-filter-list");
/* Now we call getGroupJSON() to display our filtered list */
.getGroupJSON("Keck-Institute-for-Space-Studies", "monograph",
CLfunction(monographs, err) {
if (err != "") {
console.log("ERROR", err);
return;
}/* Now we want to loop through our monographs
and filter for the ones we want to display */
.forEach(function(monograph) {
monographsif ("doi" in monograph && monograph.doi != "") {
let elem = document.createElement("div"),
= document.createElement("h1"),
h1 = document.createElement("a");
anchor
/* Add CSS classes for styling */
.classList.add("monograph");
elem.classList.add("monograph-title");
h1/* Now we collect our content into our composit
HTML elements */
.setAttribute("href", monograph.official_url);
anchor.innerHTML = monograph.title;
anchor/* Assemble our elements */
.appendChild(anchor);
h1.appendChild(h1);
elem/* Finally add our elements to our list */
.appendChild(elem);
group_filter_list
};
});
})</script>
Start of solution output
The custom group list we’ve curated should appear below.
End of solution output