interactive filter - Dynamic Content

eyala
Tera Contributor

Hello all , I created a Dynamic Content Interactive filter.
I added a report to follow the interactive filter but I have a bug.
when the page load the active filter well not work. only if I remove the report from following the interactive filter and re-add it then it will work.

the Dynamic Content Interactive filter.

<?xml version="1.0" encoding="utf-8" ?>
<j:jelly trim="false" xmlns:j="jelly:core" xmlns:g="glide" xmlns:j2="null" xmlns:g2="null">
<g:evaluate var="jvar_status" object="true" jelly="true">
	
var statusArray=[];
<!-- Get choices of select box [status] and pass it to the widget UI -->
var gr= new GlideRecord('question_choice');
  gr.addEncodedQuery('question=64f94a6c870f465040f210273cbb3516'); <!-- replace the sys_id with the sys_id of your variable. -->
  gr.addOrderBy('name');
  gr.query();

  while(gr.next()){
    statusArray.push([gr.getValue('value'),gr.getValue('text')]);
  }
	
 statusArray;
</g:evaluate>
 
    <select id='filter_statuses' class='select2-search' onchange='filterStatus()'>
        <j:forEach var="jvar_state" items="${jvar_status}"> 
            <option value="${jvar_state[0]}">${jvar_state[1]}</option>        
        </j:forEach>
    </select>   
 
    <script>
     var dbh = new DashboardMessageHandler("filter_status");
		
     function filterStatus(){
        var status = $j('#filter_statuses').val();
        if (status)
            dbh.publishFilter('u_department_of_finance', 'sc_cat_item_producer=d398c62c870f465040f210273cbb35f1^variables.64f94a6c870f465040f210273cbb3516=' + status); 
        else
            dbh.removeFilter();
     }
     filterStatus();
</script>
 
</j:jelly>

 

2 REPLIES 2

Akshay Gupta2
Kilo Sage

Hi @eyala 

 

I checked you code, I have two points that you can try and check if this works.

 

1. Ensure the Report Initializes Correctly:
- The interactive filter should be applied during the page load by calling the `filterStatus` function once the page has loaded and the 'select' element has been populated.

2. Delay the Filter Application:
- Sometimes, the interactive filter might not be ready by the time the script runs. Adding a slight delay before applying the filter can help.

 

Here's a revised version of your code

 

<?xml version="1.0" encoding="utf-8" ?>
<j:jelly trim="false" xmlns:j="jelly:core" xmlns:g="glide" xmlns:j2="null" xmlns:g2="null">
<g:evaluate var="jvar_status" object="true" jelly="true">
	
var statusArray=[];
<!-- Get choices of select box [status] and pass it to the widget UI -->
var gr= new GlideRecord('question_choice');
  gr.addEncodedQuery('question=64f94a6c870f465040f210273cbb3516'); <!-- replace the sys_id with the sys_id of your variable. -->
  gr.addOrderBy('name');
  gr.query();

  while(gr.next()){
    statusArray.push([gr.getValue('value'),gr.getValue('text')]);
  }
	
 statusArray;
</g:evaluate>
 
    <select id='filter_statuses' class='select2-search' onchange='filterStatus()'>
        <j:forEach var="jvar_state" items="${jvar_status}"> 
            <option value="${jvar_state[0]}">${jvar_state[1]}</option>        
        </j:forEach>
    </select>   
 
    <script>
     var dbh = new DashboardMessageHandler("filter_status");

     function filterStatus() {
        var status = $j('#filter_statuses').val();
        if (status)
            dbh.publishFilter('u_department_of_finance', 'sc_cat_item_producer=d398c62c870f465040f210273cbb35f1^variables.64f94a6c870f465040f210273cbb3516=' + status); 
        else
            dbh.removeFilter();
     }

     // Delay the filter application to ensure the filter is ready
     $j(document).ready(function() {
        setTimeout(filterStatus, 1000);
     });
</script>
 
</j:jelly>

 

 

1. JavaScript Code Improvement:
- Added a `setTimeout` function to delay the initial call to `filterStatus` by 1 second to ensure the interactive filter is ready and initialized correctly.

2. Ensuring Filter Application on Page Load:
- Used `$j(document).ready(function() {...})` to ensure the filter is applied once the document is fully loaded.

 

This approach ensures that the filter is applied correctly on page load and the report follows the interactive filter without needing manual re-addition. Adjust the delay in `setTimeout` as needed based on your environment's loading times.

 

Please mark as solution or helpful if this solver your issue.

Thanks

Akshay

Tried it but unfortunately it didn't work