Jump to content
We've recently updated our Privacy Statement, available here ×
  • Visualize.js: How to hide specific input controls


    stasp
    • Version: v7.1, v7.1.0, v6.4, v6.4.3, v6.4.2, v6.4.0, v6.3, v6.3.1, v6.3.0 Product: JasperReports® Server

    Use case

    It is in some cases required to hide some of the input controls from users when displaying report via Visualize.js API, while all the controls should be visible via JasperReports Server repository interface.

    Notes on implementation

    The API provides a built in 'inputControls' method to render all the controls associated with a report. However there is no built in method to render the controls separately. Such functionality has to be implemented on the client side.

    There is more than one way to achieve this. However the simplest one is probably to add a 'display:none' CSS property to the <div> element which contains the Input control you want to hide. Now, it is probably desirable to be able to hide different controls for different scenarios, so using a script to do this is preferrable to assigning the 'display:none' in your CSS configuration.

    Another important note is that since the API call is naturally delayed in its execution, you have to implement the CSS overriding in a way that it happens AFTER the report and Input controls have already rendered. Often such tasks are addressed using a callback function. In the example below we will use a built in 'reportCompleted' event instead.

    Solution

    Lets use the following Visualize.js sample as a basis:

    https://jsfiddle.net/gh/get/mootools/1.5.2/TIBCOSoftware/JS-visualize/tree/master/inputControl-report/bind-with-report/

    Now lets say we want to hide the Product Family input control:

    product_family.png.5d3d083418632cac370fe3d02a2ee9bf.png

    After a quick inspection of rendered elements we can see that the control is placed inside a <div> with class "jr-mInputControlSingleSelect":

    product_family_div.png.87ac4a3806ac77e8621edbf4ec3b41ca.png

    Now lets create a simple CSS class which will hide an element:

    .hiddenControl {
      display: none;
    }

    And then, we will loop through the divs on the page and assign our class to the ones we want to hide. In this case we only want to hide one <div> with class "jr-mInputControlSingleSelect" and label "Product Family". As mentioned above, we will use the built in 'reportCompleted' event to achieve this:

    var report = v.report({
            resource: resourceUri, 
            container: "#report",
            events: {
                reportCompleted: function(status) {
                  var divs = document.getElementsByTagName('div');
                  for (var i=0; i<divs.length; i++) {
                    if (divs[i].innerHTML.indexOf("Product Family") > -1 &&
                        divs[i].className.indexOf("jr-mInputControlSingleSelect") > -1) {
                      divs[i].addClass('hiddenControl');
                    }
                  }
                }
            }
        });

    And here is the result:

    product_family_hidden.png.ac88cba6f65afb0d628e1d7eb95943d0.png

    Other options

    You may also consider using other built in event handlers instead of reportRendered, such as beforeRender and pageFinal. In fact after further tests it looks like the pageFinal event works the smoothest of all three, at least in my use case. The complete list of report rendering events available for Visualize.js API can be found here:

    https://community.jaspersoft.com/wiki/visualizejs-complete-list-built-events-report-rendering

    product_family.png.decceeeaa90923080e6183c1dc71ac87.png

    product_family_div.png.2ce7a477701487c9fed6a431944c1f8b.png

    product_family_hidden.png.c861254991c36a16a675edd1992f443d.png


    User Feedback

    Recommended Comments

    There are no comments to display.



    Create an account or sign in to comment

    You need to be a member in order to leave a comment

    Create an account

    Sign up for a new account in our community. It's easy!

    Register a new account

    Sign in

    Already have an account? Sign in here.

    Sign In Now

×
×
  • Create New...