Load marks selection based on different sheet selection not working

Version 2

    My First Post, I have initialized the extensions in my JS file and then looping the sheets available in the Dashboard to get its name.

    I want to switch the sheet selections on the dashboard and get input for a specific field and then write it back to DB. We have write back feature for another application which is working fine. we want to implement the same and at this time the selection will be from 3different sheets.

    Then, I have added if condition to check the desired sheet and then load the marks selected. We have below piece of code for clearing the existing event.

      // Remove any existing event listeners

        if (unregisterEventHandlerFunction) {

          unregisterEventHandlerFunction();

        }

     

    and at the end of marks selection, we use below code snippet to reload the marks if they are from the same sheet.

      unregisterEventHandlerFunction = worksheet.addEventListener(tableau.TableauEventType.MarkSelectionChanged, function (selectionEvent) {

          // When the selection changes, reload the data

          loadSelectedMarks(worksheetName);

        });

     

    It is working fine for 1st sheet selection and due to "loadSelectedMarks(worksheetName);" in the unregisterEventHandlerFunction, it keeps the same sheet name so it is not recognizing the 2nd sheet name if I navigate my selection to a different sheet.

     

    Please help to guide me to overcome this case.

     

    Thanks.

     

    BR

    Vijay

     

    Hi Vijay,

     

    Why are you clearing the event handler? You only need to do this when you want to stop listening to mark selection events. The reason you are not getting the selection events from the other worksheets is that you have only added the event listener to one worksheet. The event listener is worksheet scoped, not dashboard scoped, so each worksheet needs the event listener added. If you loop through each worksheet and add the event listener you will be able to capture data from any worksheet selection. Like this:

     

    tableau.extensions.initializeAsync().then(function () {
        const dashboard = tableau.extensions.dashboardContent.dashboard;
        for (worksheet of dashboard.worksheets) {
            worksheet.addEventListener(tableau.TableauEventType.MarkSelectionChanged, function (selectionEvent) {
                console.log(selectionEvent)
                console.log(selectionEvent.worksheet.name)
                selectionEvent.getMarksAsync().then(console.log)
            });
        }
    });
    

     

    If you do in fact need the unregister functions for later you can store them in an array, like this:

     

    let unregisterEventHandlerFunctions = [];
     ... 
    unregisterEventHandlerFunctions[worksheet.name] = worksheet.addEventListener....
    

     

    Take care,

    Keshia

     

    Dear Keshia,

     

    The idea which you lighted with had helped me out to resolve the issue.

    Now the extension which i developed has capability to fetch corresponding marks on different sheets from dashboard.

     

    Thanks a lot:)

     

    Best regards

    Vijay

     

    This document was generated from the following discussion: Load marks selection based on different sheet selection not working