Using multiple data sources in a single template?



  • Thanks for this reply, I think i understand a little better. I can't test it yet though, as the JS I used from above seems to be tripping me up. If I change my data source in the studio and remove any handlebars in my template and try to run it, I get this error:

    Error while executing templating engine. Unable to find module jsreport-proxy
    Searched paths: 
    jsreport-proxy
    jsreport-proxy
    C:\Users\ar206747\jsreport-proxy
    C:\Users\ar206747\jsreport-proxy
    C:\Users\ar206747\node_modules\jsreport-cli\lib\jsreport-proxy
    . 
    
    > 1 | const jsreport = require('jsreport-proxy');
    

    Is it possible I don't have jsreport-proxy installed, and if so, how would I get it? As far as I know I did a complete normal npm install of jsreport.

    Edit: I fixed the above problem by copying jsreportProxy.js to \jsreport-cli\lib (is that a bug needs reporting, or my bad installation/config?). I'll try working some code in now and see how I get on.



  • Should it work if I don't assign a data source in the studio? I'm happy to upload my code/etc if you let me know the easiest and best way to do it, but as soon as I deselect a data source in the studio, it falls over with the Error: template input in request contain values that does not match the defined schema. schema validation errors: template.data should be objecterror, even with nothing included in the template.



  • Out of interest, should I be able to accomplish this with Resources extension? I've installed it, it lets me choose which data sources I want, but using the notation {{$resource.course.CourseName}} I still get the error. I get the error in the reply above whenever I don't choose a data source from the drop-down in the studio.



  • I just wanted to update to say I think the problem must be in my template somewhere, and I don't know where. I copied the HTML out to a new template, didn't choose a data source and set the resources up the same way and it works. It seems that it didn't like having a data source chosen and then removed?



  • It seems that it didn't like having a data source chosen and then removed?

    Thank you. I can replicate it. Ugly bug, working on fix now.

    Error while executing templating engine. Unable to find module jsreport-proxy

    I was not able to replicate this so far. This happens when rendering from jsreport studio? Do you have the latest version installed? The proxy is installed from v2 by default. No other step needed.

    Out of interest, should I be able to accomplish this with Resources extension?

    Yes this works. If you attach two "data" items as resources. The course should be name of that data item. And courseName a property inside.



  • Hi, thanks, yes I got that working now.

    If I can be a pain and ask, is it possible to iterate over two nest each loops to match data in the two sources? As per my original idea.

    So I have a data source called course, which has a list of associated modules, held in array, with an identifier 'modulecode'. I have a second data source called modules which have the same 'modulecode' identifier, but then extended information like name, duration etc.

    What l'd like to do is to step over each module code in courses, then loop over the array of modules and output the name for example on a match. Is it possible?

    I understand that for each loop of the each, I can refer to the current record as 'this.', but what happens when it's nested? I need the equivalent of this pseudocode:

    {{#each $resource.course.module}}
      <p>Module {{5}}: </p>
      {{#each $resource.modules.modules}}
        {{#if this.modulecode **meaning the modules data source** == parent.modulecode  **is there a concept of parent for nests?**}}
          {{this.modulename}}
        {{/if}}
      {{/each}}
    {{/each}}
    

    Does that make sense? Is it achievable without merging the datasets? Is it using @root as above?



  • I understand the main problem is reaching the parent scope inside handlebars template...
    Usually you find the best answers when googling: handlebars access parent scope. This leads you to something like:

    {{#each items}}
        <div style="font-size:{{../itemSize}}px">{{this}}</div>
        {{#if this.items.someKey}}
           <div style="font-size:{{../../itemSize}}px">{{this}}</div>  
        {{/if}}
    {{/each}}
    

    So in other words you can reach parent scope using ../. Hope this helps.



  • Thanks, it really does. The problem I'm having now is that when I try to each loop with a different resource as the source the nested loop, nothing happens.

     <tr>
                    <td>{{#each $resource.course.Modules}}
                         {{this.[Module Code]}}
                            {{#each $resource.modules.modules}}
                             <p>1</p>
                            {{/each}}
                        {{/each}}
                    </td>
                </tr>
    

    The <p>1</p> is in there for my own testing. it never gets printed/output, like the inner loop just isn't happening.



  • Ignore that, I fixed it by changing {{each $resource.modules.modules}} to {{each ../$resource.modules.modules}}. I didn't realise that I'd have to be that explicit when using $resource.



  • Yes, the $resource is top level prop on input data. So you cannot use it inside #each. This is the part where you should use the ../


Log in to reply
 

Looks like your connection to jsreport forum was lost, please wait while we try to reconnect.