How do I cycle through several entries in the Taffy JavaScript DB?

BACKGROUND:
I am using taffyDB (http://taffydb.com/) with materializecss’ accordion function. The following JavaScript works: when I click on the id=show1970 accordion drawer, it opens and displays the content in the DB for partNo and partName in a table row. (My question is after the code.)

function show1970(){
    var show = localStorage.setItem('show','year1970');
    getData();
 }
 
 function getData(){
 	"use strict";
 var db = TAFFY([
 {
 yearHistory: 1970, // id="output1970" div to display innerHTML
 heading:"year1970",
 partNo:"1234",
 partName:"qwerty"
 }
 ]);
 
  var show = localStorage.getItem('show'); // year1970
 
 db({heading:show}).each(function (name){
    var partNo    = name.partNo;
    var partName  = name.partName;
    var yearHistory   = name.yearHistory;
    var output = "output" + yearHistory;  // to match id="output1970" div to display innerHTML
        output = document.getElementById(output); 
        output.innerHTML=''; // refresh screen between taps
 
output.innerHTML+="<table class='striped'><tbody><tbody>";
output.innerHTML+="<tr><td style='padding:0 10px;'>" + partNo + "</td><td>" + partName + "</td></tr>";
output.innerHTML+="</tbody></table>";
 });
 }
 
document.getElementById("year1970").addEventListener('click', show1970);

QUESTION:
What if there were two entries in the DB for 1970? The DB would look like this:

function getData(){
 	"use strict";
 var db = TAFFY([
 {
 yearHistory: 1970, // id="output1970" for accordion this drawer
 heading:"year1970",
 partNo:"1234",
 partName:"qwerty1234"
 },
 yearHistory: 1970, // id="output1970" for accordion this drawer
 heading:"year1970",
 partNo:"567",
 partName:"qwerty567"
 }
 ]);

This output has to be part of a “var i=0; i++” script so it would cycle through all the entries for 1970 and output them in their own row in:

output.innerHTML+="<tr><td style='padding:0 10px;'>" + partNo + "</td><td>" + partName + "</td></tr>";

I don’t know how to grab the DB data in the “i=0, i++” cycling format for the taffyDB. Can anyone help?

I have a solution that works for me, but it is far from elegant.

Well simply, I wouldnt do it with a for loop. Or the outer Each, for that matter. You want a single table with multiple rows, not multiple tables with single rows, right?
According to the database’s (rather sparse) documentation, the result of db({yearHistory: 1970}) would be an array of objects.

output.innerHTML +=  db({heading:show}).reduce(function (result,x) { return result+"<tr><td style='padding: 0 10px;'>"+x.partNo+"</td><td>"+x.partName+"</td></tr>\r\n" },"");

EDIT: Wrong query.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.