0 Replies Latest reply on Nov 6, 2019 6:02 AM by Caleb Thompson

    Column name from getSelectedMarksAsync()

    Caleb Thompson

      With getSelectedMarksAsync() I can return the value based on index. Is there a way to return the value based on field name?

      It can be done with getUnderlyingDataAsync(), but that returns all values if a mark is not selected.  If a mark is not selected I need an empty array. Which isn't possible with getUnderlyingDataAsync()?

       

      Currently, I find the column name with getUnderlyingDataAsync(), then I ensure at least one mark is selected with getSelectedMarksAsync(). I feel this is probably not ideal, if possible I would like to simplify the code.

       

      Using just getUnderlyingDataAsync() is fine until a user does not select a mark, and all data is returned. This is not the expected functionality, the user must choose which marks to writeback. Any feedback is appreciated.

       

      $(document).ready(function() {
        tableau.extensions.initializeAsync().then(function() {
            // Start the button function 
            $("#initializeButton").click(function() {
                // the following example uses the Superstore workbook and gets the underlying data // for a specific worksheet. 
                // The example writes the values for a single column (states names) to the console. 
                worksheet = tableau.extensions.dashboardContent.dashboard.worksheets.find(w => w.name === "Action")
                worksheet.getUnderlyingDataAsync().then(dataTable => {
                    let field = dataTable.columns.find(column => column.fieldName === "Dckey");
                    let list = [];
                    for (let row of dataTable.data) {
                        list.push(row[field.index].value);
                    }
                    let formattedvalues = list.join(",");
                    //The array above includes all actions if a selection is not made, the following const is used to force a selection so not all actions are dismissed.
                    //getSelectedMarksAsync() returns Promise<MarksCollection> which does not include column titles that is why getUnderlyingDataAsync() is used.
                    worksheet.getSelectedMarksAsync().then(function(marks) {
                        const validselection = marks.data[0].data;
                        const selectionlength = validselection.length;
                        if (!selectionlength) {
                            alert("Select Action Dismissal")
                        } else {
                            // Write data to database
                            $.post('php/writeToDb.php', {
                                dckey: formattedvalues
                            });
                            //refresh the writeback datasource and refresh workbook in Tableau
                            refreshMySql();
                            alert("Number: " + selectionlength + "\nThe dashboard will now refresh");
                        }
                    });
                });
            });
        }, function(err) {
            // something went wrong in initialization
            $("#resultBox").html("Error while Initializing: " + err.toString());
        });
      });