简体   繁体   中英

vuejs this.$refs for more than one element

I have 4 selects:

         <div class="form-row">
            <div class="form-group col-3">
                <label for="stateSelect">Status</label>
                <select v-model="filterState" @change="search()" data-live-search="true" class="form-control" ref="stateSelect" id="stateSelect">
                    <option value="">Alle Status</option>
                    <option v-for="assignmentState in assignmentStates" v-bind:value="assignmentState.state">
                        {{ assignmentState.state }}
                    </option>
                </select>
            </div>
            <div class="form-group col-3">
                <label for="loadingSelect">Beladungsort</label>
                <select v-model="filterLoading" @change="search()" data-live-search="true" class="form-control" ref="loadingSelect" id="loadingSelect">
                    <option value="">Alle Standorte</option>
                    <option v-for="location in locations" v-bind:value="location.id">
                        {{ location.name }}
                    </option>
                </select>
            </div>
            <div class="form-group col-3">
                <label for="unloadingSelect">Entladungsort</label>
                <select v-model="filterUnloading" @change="search()" data-live-search="true" class="form-control" ref="unloadingSelect" id="unloadingSelect">
                    <option value="">Alle Standorte</option>
                    <option v-for="location in locations" v-bind:value="location.id">
                        {{ location.name }}
                    </option>
                </select>
            </div>
            <div class="form-group col-3">
                <label for="carrierSelect">Spediteur</label>
                <select v-model="filterCarrier" @change="search()" data-live-search="true" class="form-control" ref="carrierSelect" id="carrierSelect">
                    <option value="">Alle Speditionen</option>
                    <option v-for="assignmentCarrier in assignmentCarriers" v-bind:value="assignmentCarrier.name">
                        {{ assignmentCarrier.name }}
                    </option>
                </select>
            </div>
        </div>

And I had to refresh this dom elements with the selectpicker function to get bootstrap-select work in vuejs like this:

export default {
    name: "AssignmentList",
    data() { ....
},
updated() {
        $(this.$refs.stateSelect).selectpicker('refresh');
        $(this.$refs.loadingSelect).selectpicker('refresh')
        $(this.$refs.unloadingSelect).selectpicker('refresh')
        $(this.$refs.carrierSelect).selectpicker('refresh')
    }

is it possible to sum up these 4 .selectpicker('refresh') statements?

Or is it possible to group / flag these selects that I can refresh these elements all at once without type each individually.

Just create a constant in a constants.js file or something and import them into your component. Something like this:

const refTypes = Object.freeze({
  stateSelect: "stateSelect",
  loadingSelect: "loadingSelect",
  unloadingSelect: "unloadingSelect",
  carrierSelect: "carrierSelect"
});

Then in your component have a method you call that does something like this:

Object.values(refTypes).forEach((type) => {
  $(this.$refs[type].selectpicker('refresh');
});

And I would probably do the same thing for 'refresh' too

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM