How to access nested object value with handlebar?

121 Views Asked by At

I'm new to handlebar and I'm trying to achieve something like this by passing treeMap to JSReport, but I can only get the map key and failed to get the map value.

| Product Code | Product Name | Mon | Tue | Wed | Thur | Fri | Sat | Sun |
| ------------ | ------------ | --- | --- | --- | ---- | --- | --- | --- |
|     123      |   product A  |  0  |  0  |  3  |   4  |  1  |  0  | 1  |
|     456      |   product B  |  0  |  1  |  1  |   0  |  1  |  0  | 1  |
|              |     Total    |  0  |  1  |  4  |   4  |  2  |  0  | 2  |

Here's the structure of data that I pass to :

Map<String, itemSalesSummary> productCodeMapSummary

Structure of itemSalesSummary:
     private String productCode;
     private String productName;
     private Map<String, Integer> weekdayMapQty;

Here's how I'm getting the data on JSREPORT

{{#each productCodeMapSummary}}
{{#xlsxAdd "xl/worksheets/sheet1.xml" "worksheet.sheetData[0].row"}}
    {{#if (isSubtotal @key)}}
        <row>
           <c t="str" ><v></v></c>
           <c t="str" ><v>{{{@key}}}</v></c>
            {{#each this.weekdayMapQty}}
                <c t="n" ><v>{{{this}}}</v></c>
            {{/each}}
       </row>
       {{else}}
       <row>
           <c t="str" ><v>{{{@key}}}</v></c>
           <c t="str" ><v>{{{this.productName}}}</v></c>
            {{#each this.weekdayMapQty}}
                <c t="n" ><v>{{{this}}}</v></c>
            {{/each}}
       </row>
    {{/if}}
{{/xlsxAdd}}
{{/each}}

Handlebars.registerHelper("isSubtotal", function(key) {
  return key==="subtotal";
});

I only get the key displayed, and failed to get other value and didn't get any error message

I've also tried using handlebar #with, still no map value and no error message

{{#each productCodeMapSummary}}
{{#xlsxAdd "xl/worksheets/sheet1.xml" "worksheet.sheetData[0].row"}}
    {{#if (isSubtotal @key)}}
      {{#with this as |obj|}}
        <row>
           <c t="str" ><v></v></c>
           <c t="str" ><v>{{{@key}}}</v></c>
            {{#each obj.weekdayMapQty}}
                <c t="n" ><v>{{{this}}}</v></c>
            {{/each}}
       </row>
       {{else}}
       <row>
           <c t="str" ><v>{{{@key}}}</v></c>
           <c t="str" ><v>{{{obj.productName}}}</v></c>
            {{#each obj.weekdayMapQty}}
                <c t="n" ><v>{{{this}}}</v></c>
            {{/each}}
       </row>
      {{/with}}
    {{/if}}
{{/xlsxAdd}}
{{/each}}

Handlebars.registerHelper("isSubtotal", function(key) {
  return key==="subtotal";
});

Then I tried registering a helper (only getting productCode and productName for now), I am able to get the key and get an error message "cannot read property of undefined". so I used debugging tool on Eclipse to check the data that I created, it's exactly like the above... here's my attempt

{{#each saleOverviewMap}}
{{#xlsxAdd "xl/worksheets/sheet1.xml" "worksheet.sheetData[0].row"}}
    <row>
    {{{printTableContent @key this}}}
   </row>
{{/xlsxAdd}}
{{/each}}


Handlebars.registerHelper("printTableContent", function(value) {
   var content = "";
   if(key == "total"){
      content = '<c s="5" t="str" ><v></v></c>'
                    +'<c s="5" t="str" ><v>'
                    +key
                    +'</v></c>'
   }else{
      content = '<c s="5" t="str" ><v>'
                    +key
                    +'</v></c>'
                    +'<c s="5" t="str" ><v>'
                    + value["productName"]
                    +'</v></c>'
   }
   return new Handlebars.SafeString(content);
});

I've also tried changing the syntax, i.e. replacing triple curly braces with a double, using this/productCode instead od this.productCode... but neither works I would really appreciate if anyone can tell me what i'm missing here...

1

There are 1 best solutions below

0
A.T. On

Just wanted to make a note on another issue I encountered, hope it helps somebody who is also new to handlebar/ jsreport

On my server side, I used linkedHashMap to create ordered object for the report. But once I jsonified it, the inner layer (i.e. weekdayMapQty) lost its order. So I loop on the linkedHashMap to created JSONObject and push it into JSONArray to keep the order