0

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...

A.T.
  • 11
  • 2
  • The first approach works for me. Check it in playground. Perhaps your input data are differently serialized? Try to check what you actually send to jsreport. https://playground.jsreport.net/w/anon/dIgAcvk2 – Jan Blaha Apr 24 '23 at 16:43
  • Hi @JanBlaha, thanks! silly me, I forgot to jsonify the JAVA object. It works fine now – A.T. Apr 25 '23 at 01:45

1 Answers1

0

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

A.T.
  • 11
  • 2