Code Monkey home page Code Monkey logo

Comments (5)

cwilby avatar cwilby commented on June 5, 2024 3

Found a solution by appending an option with selected attribute. In the example, I would have added a name prop, a ref, and the following in mounted to ThingSelector.

if (this.name) {
  this.$refs.select.select2
    .append(
      $(`<option selected value="${this.value}">${this.name}</option>`)
    )
    .trigger('change');
}

from vue-select2.

godbasin avatar godbasin commented on June 5, 2024

According to Default (pre-selected) values and Preselecting options in an remotely-sourced (AJAX) Select2, it will be quite complex to make it compatible with ajax setting like this. It will be better to use your solution instead of making the component compatible with select2. Thanks!

from vue-select2.

baddwin avatar baddwin commented on June 5, 2024

Found a solution by appending an option with selected attribute. In the example, I would have added a name prop, a ref, and the following in mounted to ThingSelector.

if (this.name) {
  this.$refs.select.select2
    .append(
      $(`<option selected value="${this.value}">${this.name}</option>`)
    )
    .trigger('change');
}

how did you add ref with vue 3 @cwilby ?

from vue-select2.

cwilby avatar cwilby commented on June 5, 2024

Not 100% as I haven't actually dived into Vue 3 fully yet, might look like this after reading through docs:

import Select2 from "v-select2-component";
import { ref, onMounted } from 'vue';

export default {
  components: { Select2 },

  props: ["value"],

  data: () => ({
    settings: {
        ajax: {
            url: `/api/v1/things`,
            dataType: "json",
            data: params => {
                return {
                    term: params.term,
                    page: params.page || 1,
                    pageSize: params.pageSize || 50
                };
            },
            processResults: (data) => {
                return {
                    results: data.items.map(({ id, name: text }) => ({ id, text })),
                    pagination: {
                    more: data.current_page < data.last_page
                    }
                };
            }
        }
    }
  }),

  setup() {
    const selectRef = ref(null);

    onMounted(() => {
        if (this.name) {
            selectRef
                .append($(`<option selected value="${this.value}">${this.name}</option>`))
                .trigger('change');
        }
    });

    return { ref };
  }
};

from vue-select2.

baddwin avatar baddwin commented on June 5, 2024

I have tried like so, but still failed.

This is the Select2 template

<Select2 id="itemName" :options="trxitems" :settings="settings" @select="getProduct($event)" v-model="item.id" ref="selectRef" />

and this is the JS

...
       setup() {
           onMounted(() => {
                console.log(selectRef);
                selectRef
                    .append($(`<option selected value="1">foo</option>`))
                    .trigger('change');
            });

            return { selectRef }
       }
...

error in js console:

Uncaught (in promise) TypeError: selectRef.append is not a function

log of selectRef var:

RefImpl {_rawValue: null, _shallow: false, __v_isRef: true, _value: null}
__v_isRef: true
_rawValue: null
_shallow: false
_value: null
value: (...)
__proto__: Object

from vue-select2.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.