代码之家  ›  专栏  ›  技术社区  ›  JackLidge

在引导数据采集器中动态设置可用日期

  •  0
  • JackLidge  · 技术社区  · 5 年前

    beforeShowDay 方法

    我现在正在尝试设置日期选择器,以便在我更改网站上的地图图层时,引导日期选择器将根据新更新的图层日期列表(对于新图层)更新其可用日期。我已尝试更新 as in this answer 还试图破坏并重新初始化日期选择器 as seen here . 这两种方法都不起作用。

    L.Control.textbox = L.Control.extend({
        onAdd: function(map) {
            var info = L.DomUtil.create('div', 'datepicker');
            info.id = 'datePicker';
            var startDates = getLayerTimes();
            prepareDatepicker(startDates);
            info.innerHTML += '<div class="container">';
            info.innerHTML += '<label for="datepicker"><b>Select Date</b></label>';
            info.innerHTML += '<input type="text" class="form-control" id="datepicker"">';
            info.innerHTML += '</div>';
    
            return(info);
        }
    });
    
    L.control.textbox = function(opts) { return new L.Control.textbox(opts);}
    map.removeControl(map.zoomControl); 
    L.control.textbox({ position: 'topleft'}).addTo(map);
    map.addControl(map.zoomControl); 
    
    function prepareDatepicker (startDates) {
        if (mapLayer.options.layers.includes('Modis')) {
            var endDates = startDates.map(x => addDays(x));
        } else {
            var endDates = startDates.map(x => addMonth(x));
        };   
        $(document).ready(function() {
            $('#datepicker').datepicker({
                format: 'yyyy-mm-dd',
                startDate: endDates[0],
                endDate: endDates.slice(-1)[0],
                autoclose: true,
                // enable or disable dates according to whether they are listed
                beforeShowDay: function(date){
                    tdate = date.getFullYear() + '-' + (('0'+(date.getMonth()+1)).slice(-2)) + '-' + (('0'+date.getDate()).slice(-2))
                    if (endDates.includes(tdate)){
                        return true;
                    }
                    else {
                        return false;
                    }
                },
            }).on('changeDate', function (e) {
                endDate = convertBack(e.date);
                updateMapLayer(mapLayer.options.layers, mapLayer.options.style, endDate);            
            });
        });
    

    当发生图层更改事件时,它会运行以下函数以尝试“刷新”日期选择器:

    function refreshLayerTimes(startDate) {
        startDates = getLayerTimes();
        if (mapLayer.options.layers.includes('Modis')) {
            endDates = startDates.map(x => addDays(x));
        } else {
            endDates = startDates.map(x => addMonth(x));
        };
        $('#datepicker').datepicker({ beforeShowDay: endDates});
        return startDates, endDates;
    };
    

    哪里 endDates 是一个数组形式: ["2021-05-31", "2021-05-20", "2021-05-10",....]

    在此方面的任何帮助都将不胜感激。

    0 回复  |  直到 5 年前
        1
  •  1
  •   SaloniMishra    5 年前

    在这里,您可以采取两种方法:

    //删除当前的dateTimePicker并用新日期重新初始化它 范围

         $("#datetime").datetimepicker("remove");
         $('#datepicker').datepicker({
               format: 'yyyy-mm-dd',
               startDate: endDates[0],
               endDate: endDates.slice(-1)[0],
               autoclose: true,
               // enable or disable dates according to whether they are listed
               beforeShowDay: function(date){
                   tdate = date.getFullYear() + '-' + (('0'+(date.getMonth()+1)).slice(-2)) + '-' + (('0'+date.getDate()).slice(-2))
                   if (endDates.includes(tdate)){
                       return true;
                   }
                   else {
                       return false;
                   }
               },
           }).on('changeDate', function (e) {
               endDate = convertBack(e.date);
               updateMapLayer(mapLayer.options.layers, mapLayer.options.style, endDate);            
           });
    

    方法2: 使用以下方法设置新的开始和结束日期

    $('#datetimepicker').datetimepicker('setStartDate', '2012-01-01');
    $('#datetimepicker').datetimepicker('setEndDate', '2012-01-01');
    
    

    https://www.malot.fr/bootstrap-datetimepicker/