1 Reply Latest reply on Dec 16, 2016 4:50 AM by lenaic.ridinger

    Adding a var to javascript

    jens.bruckmann

      Hi all,

       

      after I was able to get a my first report with WDC I fugured out I have a nother issue which I hope you can help me getting this sorted.

       

      In the link where I get my data from it is necessary to set an end date. As you can imagine I do not want to manipulate every hour my script to setup a new end date. So I thought I can work with a new var. I think I found the correct one but maybe the place where I need to put the var is not really clear for me

       

      This is my script

       

       

       

      (function () {
          var myConnector = tableau.makeConnector();
      
      myConnector.getSchema = function (schemaCallback) {
          var cols = [
           { id: "datetime_raw",   alias:"Raw Zeit",        dataType: tableau.dataTypeEnum.float },
              { id : "datetime", alias : "Messzeitpunkt", dataType : tableau.dataTypeEnum.string },
              { id : "value_raw", alias : "Wert der Abfrage", dataType : tableau.dataTypeEnum.float },
        { id : "value", alias : "Value", dataType : tableau.dataTypeEnum.string }
         
          ];
      
          var tableInfo = {
              id : "Capacity_Management",
              alias : "Needed Sensors for a propper Capacity Management Report",
              columns : cols
          };
      
          schemaCallback([tableInfo]);
      };
      
      myConnector.getData = function(table, doneCallback) {
      
          $.getJSON("https:/****/api/historicdata.json?id=43036&avg=0&sdate=2016-12-03-00-00-00&edate="+datestring+"&username=****&passhash=****", function(resp) {
        
       var date = new Date();
        var datestring = ("0" + (date.getFullYear() + 1).toString()).substr(-2) + "-" + ("0" + date.getMonth().toString()).substr(-2)  + "-" + (date.getDay().toString()).substr(2)+ "-" + (date.getHours().toString()).substr(2)+ "-" + (date.getMinutes().toString()).substr(2)+ "-" + (date.getSeconds().toString()).substr(2);
      
          var feat = resp.histdata,
                  tableData = [];
      
                  // Iterate over the JSON object
        //String id = UUID.randomUUID().toString();  
                  for (var i = 0, len = feat.length; i < len; i++) {
                      tableData.push({
      
                      "datetime_raw": feat[i].datetime_raw,
                          "datetime": feat[i].datetime,
                          "value_raw": feat[i].value_raw,
        "value": feat[i].value
                      
                      });
                  }
      
      
              table.appendRows(tableData);
              doneCallback();
      
          });
      };
      
       tableau.registerConnector(myConnector);
        $(document).ready(function () {
          $("#submitButton").click(function () {
              tableau.connectionName = "Capacity Management";
              tableau.submit();
          });
      });
      
       })();
      

       

      In line 24 I added the datestring which I have defined in line 26/27

       

      as you can imagine it is not working. I am always getting following error:

       

      SimulatorDispatcher.js:88 Uncaught Looks like the WDC is calling a tableau function before tableau.init() has been called.

       

      Any Idea how to put the var into this script?

        • 1. Re: Adding a var to javascript

          Hi Jens Bruckmann,

           

          You don't need jQuery for that. Use regular javascript:

          var arr = new Array(); // or var arr = []; arr.push('value1'); arr.push('value2');

          Note: In javascript, you can also use Objects as Arrays, but still have access to the Array prototypes. This makes the object behave like an array:

          var obj = new Object(); Array.prototype.push.call(obj, 'value');

          will create an object that looks like:

          { 0: 'value', length: 1 }

          You can access the values just like a normal array f.ex obj[0].