0

In my vue laravel application ...i have dynamicllay added some select option by onclick function. For the intial select option select2 is working fine but when i am trying to added more field dynamically the select 2 is not working . i have used (npm install v-select2-component --save ) but unfortunatlley does not work

<script>
import Select2 from 'v-select2-component';
export default {
  components: {Select2},
  data(){
  return{
   event_Category_Remove: [],
      index: 0,
      eventsElement_id: [],
  }
  },
      
     methods:{
     addEvent() {
      this.eventsElement_id = [];
      this.event_Category_Remove.push({
        id: this.index,
        name: "ecr" + this.index,
      });
      this.index++;
    },

    /** ========= Remove Event  ========= */
    removeEvent(index) {
      this.event_Category_Remove.splice(index, 1);
    },
  }
</script>
<template>
                              <div
                              class="
                                form-group
                                row
                                without-form-background
                                mb-2
                              "
                            >
                              <label class="col-md-2 label-control" for="weight"
                                >Event (can be multiple)</label
                              >
                              <a class="text-primary" @click="addEvent">
                                <i>Add</i> <i class="fa fa-plus"></i>
                              </a>
                            </div>
                            <!-- ======== Adding Multiple Event Button  End  ======== -->

                            <!-- ///////////////////// -->

                            <!-- ======== Event Start  ======== -->
                            <div
                              class="event_Category_Remove"
                              v-for="(ecr, index) in event_Category_Remove"
                              :key="ecr.id"
                              :id="'eventChild_' + index"
                            >
                              <div
                                class="
                                  form-group
                                  row
                                  without-form-background
                                  mb2
                                "
                              >
                                <label class="col-md-2 label-control" for="name"
                                  >Event</label
                                >
                                <div class="col-md-10">
                                  <select
                                    :name="'event_' + index"
                                    id=""
                                    class="form-control"
                                  >
                                    <option
                                      name="event"
                                      v-for="event in event"
                                      :key="event.id"
                                      :value="event.id"
                                      :selected="event.id == event_selected"
                                    >
                                      {{ event.name }}
                                    </option>
                                  </select>
                                </div>
                              </div>

                              <!-- ======== Event Ends  ======== -->

                              <!-- ////////////////////// -->

                              <!-- ======== Category Start  ======== -->

                              <div
                                class="
                                  form-group
                                  row
                                  without-form-background
                                  mb2
                                "
                              >
                                <label class="col-md-2 label-control" for="name"
                                  >Category</label
                                >
                                <div class="col-md-10">
                                  <Select2
                                    :name="'category[]_' + index"
                                    id=""
                                    class="form-control multiselect"
                                    multiple="multiple"
                                    tabindex="-1"
                                    selected
                                    value="cate.id"
                                  >
                                    <option
                                      v-for="cate in categoryarr"
                                      :key="cate.id"
                                      :value="cate.id"
                                      :selected="
                                        categoryarr_selected.includes(
                                          String(cate.id)
                                        )
                                      "
                                    >
                                      {{ cate.title }}
                                    </option>
                                  </Select2>
                                </div>
                              </div>

                              <!-- ======== Category End  ======== -->

                              <!-- /////////////// -->

                              <!-- ======== Remove Button1  Start  ======== -->

                              <div
                                class="form-group without-form-background mb2"
                              >
                                <button
                                  class="btn btn-danger text-white"
                                  @click="removeEvent(ecr.id)"
                                >
                                  Remove
                                </button>
                                <hr class="bg-danger" />
                              </div>
                            </div>
                </template>

3
  • You can add options dynamically as explained here - select2.org/programmatic-control/add-select-clear-items Commented Nov 15, 2021 at 8:20
  • can you please write a sample code . Actually i am a beginner and vue is totally new for me Commented Nov 15, 2021 at 8:38
  • Well, then my advice to you (as a beginner) is not to include any jQuery-based non-Vue 3rd party components (like Select2) into your Vue project - at least, until you advance into a non-beginner. This advice comes from experience. If you need such functionality - my recommendation is to use something like vue-multiselect.js.org Commented Nov 15, 2021 at 15:09

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.