Nested loop in birt report

I am using Eclipse Birt to generate a report from a JSON file.

My JSON file looks like this:

{  
"cells":[  
  {  
     "type":"basic.Sensor",
     "custom":{  
     "identifier":[  
           {  
              "name":"Name1",
              "URI":"Value1"

           },
           {  
              "name":"Name4",
              "URI":"Value4"
           }
        ],
        "classifier":[  
           {  
              "name":"Name2",
              "URI":"Value2"
           }
        ],
        "output":[  
           {  
              "name":"Name3",
              "URI":"Value3"
           }
        ],

     },
           "image":{  
           "width":50,
           "height":50,
           "xlink:href":"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAIAAAACACAYAAADDPmHLAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAABEJAAARCQBQGfEVAAAABl0RVh0U29mdHdhcmUAd3Vi8f+k/EREURQtsda2Or/+nFLqP6T5Ecdi0aJFL85msz2Qxyf4JIumMAx/ClmWt23GmL1kO54CXANAVH+WiN4Sx7EoNVkU3Z41BDHMeXAxjvOxNr7RJjzHX7S/jAflwBxkJr/RwiOpWZ883Nzd+Wpld7tkBr/SJr7ZHZbHZeuVweSnPfniocMAWYwcGBafH0OoPamFGAaY4ZBZjmmFGAaY4ZBZjmmFGAaY4ZBZjmmFGAaY7/B94QnX08zxKLAAAAAElFTkSuQmCC"
        }
     }
  },
  {  
     "type":"basic.Sensor",

     "custom":{  
        "identifier":[  
           {  
              "name":"Name1",
              "URI":"Value1"

           },
           {  
              "name":"Name4",
              "URI":"Value4"
           }
        ],
        "classifier":[  
           {  
              "name":"Name2",
              "URI":"Value2"
           }
        ],
        "output":[  
           {  
              "name":"Name3",
              "URI":"Value3"
           }
        ],

     },

        "image":{  
           "width":50,
           "height":50,
           "xlink:href":"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAgAAAAIACAYAAAD0eNT6AAAACXBIWXMAAAsTAAALEwEAmpwYAAAKT2lDQ1BQaG90b3Nob3AgSUNDIHByb2ZpbGUAAHjanVNnVFPpFj333vRCS4iAlEtvUhUIIFJCi4AUkSYqIQkQSoghodkVUcERRUUEG8igiAOOjoCMFVEsDIoK2AfkIaKOg6OIisr74Xuja9igAQAAAgAIoAEAAAIACKABAAACAAigAQAAAgAIoAEAAAIACKABAAACAAigAQAAAgAIoAEAAAIADqhvprADeSsau00l5NAAAAAElFTkSuQmCC"
        }
     }
  },
  {  
     "type":"basic.Platform",

     "custom":{  
        "identifier":[  
           {  
              "name":"Name1",
              "URI":"Value1"

           }
        ],
        "classifier":[  
           {  
              "name":"Name2",
              "URI":"Value2"
           }
        ],
        "output":[  
           {  
              "name":"Name3",
              "URI":"Value3"
           }
        ],

        "image":{  
           "width":50,
           "height":50,
           "xlink:href":"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAQAAAA6TH0jqtg6U8EsCdnm3SpevSK7Pb85xABEMBuLAn2hxjRve7SFzYEaB/HhytLQ4ABRwCWBPvBKnRk6U8EkBeOD9f7iwAGHAGEYEmwDxLvzNKfCCDP8NGLQd3lY7D0JwI4kmlwfHhX6dTSXxsRAAHsWR7aUjc7uM5Wg=="
        }
     }
  }
]
 }

      

I have 3 cells and each one contains 1 Image 1 Title 1 Type and 3 Tables, this is what I have done so far: enter image description here

what I am trying to do is a nested loop, I want to have for each object (cell) in my JSON a paragraph numbered like this:

  • 2.x Component cell name:
    • Picture
    • Output table
    • ID table
    • Classifier table

To do this, I need to iterate over each cell and then repeat on each table. Output, id and classifier, and I don't know how to do this, nested loop. as a List that represents the number of cells that contains 3 tables, one image, one name.

** Edit: ** this is a public method for the dataset

// Grab the JSON file and place it in a string
fisTargetFile = new FileInputStream(new File("C:/Users/Sample Reports/moe.json"));
input = IOUtils.toString(fisTargetFile, "UTF-8");

// Store the contents in a variable
jsonData = input;

// Convert the String to a JSON object
myJSONObject = eval( '(' + jsonData + ' )' );

// Get the length of the object
len = myJSONObject.cells.length;

// Counter
count = 0;

      

Fetch method:

if(count < len) {
var name     = myJSONObject.cells[count].attrs.text["text"];
var type    = myJSONObject.cells[count].type;
var icon =myJSONObject.cells[count].attrs.image["xlink:href"];



icon = icon.split(",");

icon= icon[1];
imageDataBytes = icon;

row["name"]     = name;
row["type"]    = type;
row["icon"]    = Base64ToBlob.toBytes(icon);



Logger.getAnonymousLogger().info( row["icon"]);
count++;
return true;
}

return false;

      

+3


source to share


1 answer


You want to use nested tables, there is a good tutorial showing how to link nested tables to an external table: follow this demo closely first, in particular, see how the subtable is related to the external table via a dataset parameter.

Of course your case is more complex because you need to do it with dataset scripts and multiple sub-tables. I already did something like this, you need to create one dataset script for each subtable. The key points are:



  • In the "parameters" section of each subdisk, create one input parameter and name it, for example, "systemID"
  • Create your sub-tables by "drag and drop" each dataset in the external table
  • In the "bindings" section of each subcategory, specify the "systemID" parameter in the ID field of the external table
  • In the "open" subdiscate event, access the parameter value with this expression: inputParams ["systemID"] This way, you can filter related strings in "myJSONObject".
  • It is important that "myJSONObject" is initialized once for all , otherwise the metrics can drop dramatically if they are evaluated at each iteration. For example, evaluate it in "initializing" a report event.

It will not be easy to do this, but these elements should help in achieving this report.

+3


source







All Articles