Comments (5)
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.
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.
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 inmounted
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.
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.
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)
- On Open not Focus on search filed HOT 2
- Why bootstrap theme doesn't have any effects on select2 component? HOT 1
- Missing usage of optgroup HOT 1
- vee-validate package v2 not working properly with vue-select 2
- Select two elements when one is chosen
- vue3-select2-component modal issue HOT 1
- Vue-Select2 not showing on result
- How to serverside query
- Vue.js 3 - problem jquery - does not provide an export named 'default' HOT 7
- modelValue as Number
- Custom Name Option Not Working
- Vue3-select2-modal problem. HOT 1
- Dropdown doesn't close when I use more than one select2 on a page event it remains in dom till press any click
- Multiple Select HOT 1
- @change not working
- selectionCssClass setting doesn't work
- Update proposal
- vue3-select2-component: select2 is not a function & Unhandled error during execution of mounted hook
- Paginate data options in select 2
- how can i add icons/image against every option?
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from vue-select2.