Search code examples
javascriptjquerytableau-apidaterangepicker

How to pass array values to function onChange from input field?


I am calling a function named "VizDateRange" which receives two parameters when dates are selected and these selected values should pass as two values but the selected dates are in a weird format which comes out as 02/01/2017-06/19/2017 as one value. So,in order to pass it as two values, how should I proceed?

//HTML

<div class="input-group date" data-provide="datepicker" name="" title="Select date range">
            <input type="text" id="daterange" class="form-control" onChange=VizFilter(this.val().split(" - "))>  //this is not working obviously. 
        </div>

//calling daterangepicker which is bootstrap's date range plugin

   $(document).ready(function () {
            var dp = $('#daterange').daterangepicker({

                "startDate": "02/06/2017", setting the limit for min
                "endDate": "06/19/2017"setting the limit for max
            }


            )

        });

//the output is

02/01/2017-06/19/2017

// but what I need is "02/01/2017","06/19/2017" in order to pass it to VizDateRange function.

//so I can use split (" - ") to turn it to array but how to pass this onChange or onClick from input field?

//the function should get called onChange or onCLick and it asks for two values, it receives two parameters

 function vizDateRange(minDate,maxDate)

            {
                sheet = viz0.getWorkbook().getActiveSheet();
                worksheetsArray = sheet.getWorksheets();
                for (var i = 0; i < worksheetsArray.length; i++) {
                    worksheetsArray[i].applyRangeFilterAsync("WEEK(DCM Date)", {
                        min: new Date(minDate),
                        max: new Date(maxDate)

                    });
                }
};

//here is jsfiddle

https://jsfiddle.net/u4t1rebs/


Solution

  • If you look at the library's examples the first argument is an options object, but as a second optional argument you can pass a callback function. Here's how I would use that callback to solve this issue:

            $(document).ready(function() {
                var dp = $('#daterange').daterangepicker({
                        "startDate": "02/06/2017",
                        "endDate": "06/19/2017"
                    },
                    function(start, end, label) {
                    	start = moment(start).format('MM/DD/YYYY');
                        end = moment(end).format('MM/DD/YYYY');
                        vizDateRange(start, end)
                    }
                )
            });
    
    
            function vizDateRange(minDate, maxDate) {
                // do stuff here 
                console.log(minDate, maxDate);
            }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <!-- Include Required Prerequisites -->
    <script type="text/javascript" src="//cdn.jsdelivr.net/momentjs/latest/moment.min.js"></script> 
    <script type="text/javascript" src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> 
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
    
    <!-- Include Date Range Picker -->
    <script type="text/javascript" src="//cdn.jsdelivr.net/bootstrap.daterangepicker/2/daterangepicker.js"></script>
    <link rel="stylesheet" type="text/css" href="//cdn.jsdelivr.net/bootstrap.daterangepicker/2/daterangepicker.css" />
    
    
    <div class="input-group date" data-provide="datepicker" name="" title="Select date range">
      <input type="text" id="daterange" class="form-control" > //this is not working obviously.
    </div>