Vue JS-Bootstrap Datepicker无法正常工作 [英] Vue JS - Bootstrap Datepicker not working

查看:62
本文介绍了Vue JS-Bootstrap Datepicker无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过动态添加Vue Js中的bootstrap datepicker,但它尚未初始化.

I am trying to use bootstrap datepicker in Vue Js by adding it dynamically, but it is not initializing.

HTML

<div v-for="(aligner,index) in aligners" v-bind:key="aligner">
<input type="hidden" v-bind:name="'AlignerDetails[' + index + '].BatchNo'" v-bind:value="'AlignerDetails_' + (index + 1) + '__BatchNo'" />
<div class='mt-element-ribbon bg-grey-steel'>
    <div class='ribbon ribbon-round ribbon-color-primary uppercase'>Batch #{{index + 1}}</div>
    <div class='pull-right'>
        <a href='javascript:;' v-on:click='remove(index);' class='btn-delete'>
            <i class='fa fa-trash-o font-red'></i>
        </a>
    </div>
    <div class='row ribbon-content'>
        <div class='col-md-12 padding-left-0 padding-right-0' style="margin-bottom:5px;">
            <input type="text" v-bind:name="'AlignerDetails[' + index + '].AlignersSent'" v-on:blur="calculate();" class="form-control alignersSent" placeholder="Aligners Sent" />
        </div>
        <div class='col-md-12 padding-left-0 padding-right-0'>
            <div class="input-group date bs-datetime">
                <input type="date" v-bind:name="'AlignerDetails[' + index + '].AlignersSentDate'" class="form-control form-control-inline alignersSentDate" placeholder="Aligners Sent Date" autocomplete="off" />
                <span class="input-group-addon">
                    <button class="btn btn-default date-set" type="button">
                        <i class="fa fa-calendar"></i>
                    </button>
                </span>
            </div>
        </div>
    </div>
</div>
</div>

JS

var app = new Vue({
el: "#alignerDetails",
data: {
    aligners: []
},
mounted: function () {
    $('.date').datepicker({
        autoclose: true,
        format: 'dd M yyyy'
    });
},
methods: {
    add: function () {
        this.aligners.push({});
    },
    remove: function (index) {
        this.aligners.splice(index, 1);
    }
}
});

我不明白为什么它不起作用.

I am not getting why this is not working..

推荐答案

我嵌入需要初始化的Jquery元素时发现的技巧是使用v-if将它们放置在隐藏的元素中.根据Vuejs的文档,v-if在满足v-if条件之前不会渲染该元素.因此,除非您以编程方式将其设置为,否则这些项目不会在DOM中.在这种情况下,您可以通过添加jQuery init语句来完成一个伟大的活动.

The trick I've found when embedding Jquery elements that require initialization is to place them inside a hidden element using v-if. According to Vuejs's documentation, v-if does not render that element until it meets the v-if condition. So the items are not in the DOM until you programatically set them to. At which case give you a great event to add jquery init statements.

将日期选择器放入基于v-if的div

Place your datepicker inside a v-if based div

<div v-if="show"></div>

在内部安装中,我会做类似的事情

Inside mounted I do something like this

async mounted() {
    this.show = true

    $('select', this.$el).selectpicker();
    $('.datepicker', this.$el).datepicker({format: 'mm/dd/yyyy', orientation: 'bottom auto'});

    //this is how you allow datepicker to send its value to v-model on the input field
    $('.datepicker').change(function() {
        $(this)[0].dispatchEvent(new Event('input'));
    });
}

这篇关于Vue JS-Bootstrap Datepicker无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆