pocketquery dynamic template loading Query
Asked by Nathaniel Wright:

Hi,

I have a template that reads the options for a team from the database and also allows for fixed years (I haven't worked out how to perform 2 separate queries!). It uses these two parameters to update two queries on the page.

I'd like to re-use the same template and have it dynamically load the query based on an input that is defined at the time the page is edited - can you recommend a method that doesn't require me to duplicate the query/template?

Query: select TYPENAME as team FROM QA_OWNER.REL_TYPES

Template I want to re-use with query name(s) defined when the macro is added to the page (i.e. ReleaseCalendar and ReleaseStats are dynamically added)

\<form id="my-pocketquery-form" class="aui"\>  
\<div class="field-group"\>  
\<label for="select-team"\>Choose Team:\</label\>  
\<select class="select" id="select-team" name="select-team"\>  
\<option value="%"\>All\</option\>  
#foreach ($row in $result)  
\<option value="$row.team"\>$row.team\</option\>  
#end  

\</select\>  
\</div\>  
\<div class="field-group"\>  
\<label for="select-year"\>Choose Year:\</label\>  
\<select class="select" id="select-year" name="select-year"\>  
\<option value="%"\>All\</option\>  
\<option value="2015"\>2015\</option\>  
\<option value="2016"\>2016\</option\>  
\</select\>  
\</div\>  
\</form\>

$PocketQuery.renderPocketQueryMacro("ReleaseCalendar", {  
'page': $page,  
'parameters': {'team':'%', 'year': '%'},  
'dynamicload': true  
})

<br />

\<script\>  
jQuery('#my-pocketquery-form select').change(function() {  
var container = jQuery('.pocketquery-dynamic-load\[data-query="ReleaseCalendar"\]');  
var dataIndex = container.data('index');  
var data = PocketQuery.getDynamicLoadData(dataIndex);  
data.queryParameters = { team: jQuery('#select-team').val(), year: jQuery('#select-year').val() };  
PocketQuery.load({  
container: container,  
data: data  
});  
var container = jQuery('.pocketquery-dynamic-load\[data-query="ReleaseStats"\]');  
var dataIndex = container.data('index');  
var data = PocketQuery.getDynamicLoadData(dataIndex);  
data.queryParameters = { team: jQuery('#select-team').val(), year: jQuery('#select-year').val() };  
PocketQuery.load({  
container: container,  
data: data  
});

});  
\</script\>


Answers:
author: Felix Grund (Scandio)
body: Hi Nathaniel,

I suggest to create as many "dummy" SELECT parameters in your outer query as you need:

```
SELECT columnX, columnX, :additionalParam1, :additionalParam2FROM tableX...
```

These parameters will be configurable in the macro. In the template of your outer query, you will have all of your dummy params available in the $queryParameters object: *$queryParameters.additionalParam1*

With this you can then render your subquery like this:

```
$PocketQuery.renderPocketQueryMacro($queryParameters.additionalParam1, {  'page': $page,  'parameters': {'team':'%', 'year': '%'},  'dynamicload': true})
```

Please let me know if this works!

Regards, Felix (Scandio)


author: Nathaniel Wright
body: Yes indeed. I'd like to reuse a couple of queries that act as pickers for multiple inner queries to avoid having to duplicate those outer queries. The page wouldn't always have the same inner queries, I.e they are defined at page creation time and not page load.


author: Felix Grund (Scandio)
body: Hi Nathaniel,

Do you mean that in your case you want to set the query dynamically for the renderPocketQueryMacro calls? So the solution would look somehow like this:

```
$PocketQuery.renderPocketQueryMacro($parameters.myTemplate, {  'page': $page,  'parameters': {'team':'%', 'year': '%'},  'dynamicload': true})
```

So the name of the "sub query" would actually be defined as parameter on the "parent query"? This would be a new idea to me, but very interesting. Please let me know if I understand you right.

Felix


Tags: pocketquery