Vue run function after v-for finishes rendering

2k views Asked by At

I am dynamically making input fields inside a v-for based on a params variable in Vue.js. The inputs are also Twitter typeahead inputs, but they need to be initialized after they are rendered. I can manually call typeaheadLoop() after I see they've been rendered, but how can I do this automatically?

I need to run the typeaheadLoop() function after v-for params finishes, how do I achieve this?

HTML:

<div v-for="item in params" :key="item">
    <label class="col-form-label text-inverse-success">@{{ fromCamelToSentenceCase(item) }}</label>
    <div class="typeahead">
        <input v-bind:id="item + 'ParamInput'" type="text" class="form-control" :placeholder="'Enter ' + fromCamelToSentenceCase(item)"/>
    </div>
</div>

JavaScript:

function typeaheadLoop() {
    for(var i = 0; i < vueApp.params.length; i++) {
        var param = vueApp.params[i];
        if(!param.includes('date')) {
            createTypeahead(param, vueApp.dataSource);
        }
    }
}

function createTypeahead(paramName, dataset) {
    var hound = new Bloodhound({
        datumTokenizer: Bloodhound.tokenizers.whitespace,
        queryTokenizer: Bloodhound.tokenizers.whitespace,
        remote: {
            cache: false,
            url: '/param/' + dataset + '?paramName=' + paramName + '&keyword=%QUERY',
            wildcard: '%QUERY'
        }
    });

    $("#" + paramName + "ParamInput").typeahead({
            highlight: true,
            minLength: 0,
        },{
            name: 'suggestions',
            source: hound,
            limit: 10,
            display: function(data) {
                return data;
            },
        }
    ).bind('typeahead:select', function(ev, suggestion) {
        console.log(suggestion);
    });
}
1

There are 1 answers

3
Tom Matthews On

I think $nextTick may be able to help:

mounted: function () {


this.$nextTick(function () {
// Code that will run only after the
// entire view has been rendered


})

}