Accessing lookup data from main context when in a component



  • Hi, another newbie.

    I have a complex JSON structure for my data which includes lookup data - below is a simplified version

    {
    	"lkpCodes": {
    		"SeverityLkp": {"0": "severity-0",	"1": "severity-1",	"2": "severity-2",	"3": "severity-3",	"4": "severity-4",	"5": "severity-5"	}
    	},
    	"Need": {
    		"NeedId": "N00001363",
    		"Assessments": {
    			"A1": [{"period": "2023","severity": "1"},{"period": "2024","severity": "1"},{"period": "2025","severity": "2"},{"period": "2026","severity": "3"}],
    			"A2": [{"period": "2023","severity": "0"},{"period": "2024","severity": "1"},{"period": "2025","severity": "2"},{"period": "2026","severity": "3"}],
    			"A3":  [{"period": "2023","severity": "2"},{"period": "2024","severity": "2"},{"period": "2025","severity": "3"},{"period": "2026","severity": "4"}]
    		}}
    }
    

    Depending on client requirements I need to show one or more of the assessments (ie might only want A2) so I call a component passing only the data for the required assessment

    {{#with (lookup this.Need.Assessments 'A1') }}
    
            {{component "showAssessment" }}
    
    {{/with}} 
    

    I need to use the severity value to lookup the class name from the SeverityLkp in lkpCodes but cant figure out how to access this from within the component. I thought i could get it from @root but this only holds the current data within the component (ie in this case the data for A1).

    Thanks in advance
    Yvonne



  • Similar is the main example and docs
    https://playground.jsreport.net/w/admin/mcb0pE1a
    https://jsreport.net/learn/components

    In your case, it would be like this

    {{#with (lookup this.Need.Assessments 'A1') }}
      {{component "showAssessment"  lkpCodes=@root.lkpCodes }}
    {{/with}} 
    

    In other words, you can pass anything from the root and also the whole root in the extra attribute.



  • Thank you so much - that is working perfectly now.


Log in to reply
 

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