Jquery UI datepicker仅允许动态特定的星期几

时间:2018-04-01 09:27:28

标签: jquery jquery-ui datepicker jquery-ui-datepicker

我想根据用户在其中一个输入框中输入的日期在jQuery UI datepicker中禁用某些天。

如果我输入 MWF ,它应该只允许我选择日期落在" M " on on," W "周三ot" F " riday

我能够动态构造需要在return语句中设置的返回值sting。

 for (var j=0; j < arr.length; j++)
      mystr = mystr + "day != " + arr[j] + " && ";

      mystr = mystr.substring(0, mystr.length - 3);

这给了我mystr变量中的以下文字:

day != 3 && day != 4 && day != 6 

需要传递给return语句,如:

​$("#datepicker").datepicker({
    beforeShowDay: function(date) {
        var day = date.getDay();
        return [(mystr)];
    }
})​​​​​;​

这不起作用....

如果我在return语句中明确设置它,我可以禁用我想要的日子。但我需要根据在文本框中输入的天数动态限制日期选择器中的日期选择。我可以动态地获取日期,并且还准备好动态返回语句。它不起作用。

完整代码:

            var batchdays = ['M', 'T', 'W', 'H', 'F', 'S'];
            var batchcode= "MWF";
            var batchdaysarray = batchcode.replace(/\d+/g, '').split('');
            var arr = [];
            var arrayLength = batchdaysarray.length;

            for (var i = 0; i < arrayLength; i++) {
                    if ($.inArray(batchdaysarray[i], batchdays) != -1) {
                        arr.push($.inArray(batchdaysarray[i], batchdays)+1);
                    }
            }

            var mystr = '';

            for (var j=0; j < arr.length; j++)
                mystr = mystr + "day != " + arr[j] + " && ";

            mystr = mystr.substring(0, mystr.length - 3);

            alert(mystr);
            console.log(mystr);

            $('#actual_start_dt').datepicker({
                dateFormat: 'dd-mm-yy',
                prevText: '<i class="fa fa-chevron-left"></i>',
                nextText: '<i class="fa fa-chevron-right"></i>',
                beforeShowDay: function(date) {
                    var day = date.getDay();
                    return [(mystr)];
                }
            });

请帮忙

1 个答案:

答案 0 :(得分:0)

我可能认为你已经自己解决了这个问题。您提供的代码存在问题,

MqttMessage message = new MqttMessage(command);
message.setPayload(command);
message.setQos(1);
mqttClient.publish(topic, message);
System.out.println("Message published");

根据jquery-ui文档beforeShowDay是,

  

将日期作为参数并且必须返回数组的函数   用:

     
      
  1. 表示此日期是否可选的真/假

  2.   
  3. 要添加到日期的单元格的CSS类名称,或者#34;&#34;对于默认值   介绍

  4.   
  5. 此日期的可选弹出工具提示
  6.   

在您的示例中,您正在传递字符串

public void connect() {

    try {
        mqttClient = new MqttClient(serverUri, clientId);
        //mqttClient.setTimeToWait(10000);
    } catch (MqttException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

    mqttClient.setCallback(new MqttCallbackExtended() {
        @Override
        public void messageArrived(String topic, MqttMessage mqttMessage) throws Exception {
            // TODO Auto-generated method stub
            System.out.println("messageArrived: " + topic.toString());
            System.out.println(mqttMessage.toString());
            System.out.println(mqttMessage.getPayload());
        }

        @Override
        public void deliveryComplete(IMqttDeliveryToken arg0) {
            // TODO Auto-generated method stub
            System.out.println("deliveryComplete: " + arg0.getMessageId());
        }

        @Override
        public void connectionLost(Throwable arg0) {
            // TODO Auto-generated method stub
            System.out.println("---Connection lost1");
            // Toast.makeText(App.getContext(), "Connection
            // lost",Toast.LENGTH_SHORT).show();

        }

        @Override
        public void connectComplete(boolean arg0, String arg1) {
            // TODO Auto-generated method stub
            System.out.println("connectComplete");
            try {
                mqttClient.subscribe("device/+/publish");
            } catch (MqttException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            try {
                commands.sendCommand(Commands.GET_STATUS, null, "device/identity:85111741-5789-3010-85c9-be4a7204e5d3");
            } catch (MqttException | InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }
    });

    MqttConnectOptions mqttConnectOptions = new MqttConnectOptions();
    mqttConnectOptions.setAutomaticReconnect(true);
    mqttConnectOptions.setCleanSession(true);
    mqttConnectOptions.setUserName(username);
    mqttConnectOptions.setPassword(password.toCharArray());
    mqttConnectOptions.setKeepAliveInterval(30);
    mqttConnectOptions.setConnectionTimeout(60);
    mqttConnectOptions.setMqttVersion(MqttConnectOptions.MQTT_VERSION_3_1_1);

    try {
        mqttClient.connect(mqttConnectOptions);
    } catch (MqttSecurityException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (MqttException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

return [(mystr)];

中使用时不会转换为语句

相反,您可以使用以下内容,

 for (var j=0; j < arr.length; j++)
    mystr = mystr + "day != " + arr[j] + " && ";

 mystr = mystr.substring(0, mystr.length - 3); 

请参阅此JSFiddle演示enter link description here

我希望这就是你要找的东西。