alexurquhart / vue-bootstrap-typeahead

An autocomplete/typeahead component for Vue 2 and Bootstrap 4
https://alexurquhart.github.io/vue-bootstrap-typeahead/
MIT License
206 stars 157 forks source link

Initial selected value #22

Open bogdancarmanus opened 5 years ago

bogdancarmanus commented 5 years ago

Might be stupid, but I really can't figure out how to set the initial selected value. Can I get some help?

jonbartlam commented 5 years ago

Would love to know if this is actually something we've both misunderstood. Great component but this feels like a basic feature that should work? I've just had exactly the same issue - despite setting v-model to the text I wanted displayed, it simply wouldn't show the value. I've found a work around by looking at the sourcecode but I'll add a warning:

Those of a weak disposition, look away now. Those easily offended should also look away.

My component setup is like this:

Component/HTML <vue-bootstrap-typeahead ref="typeahead" :data="addresses" v-model="addressSearch" :serializer="s => locationText(s)" @hit="locationSelected" />

Where you want to set the initial selection this.$refs.typeahead.$data.inputValue = this.locationText(val)

LocationText() is just a method I'm using to format the text as I want it - nothing special

Now that's out of the way - here's my use case incase I misunderstood you:

But obviously, when binding the initial location object, nothing was coming up. I found the only way to do it was reaching inside the typeahead component and setting the value it uses internally. I'm not sure what effect this will have on other aspects, but it works for me. I'm using NUXT FWIW

bogdancarmanus commented 5 years ago

That's exactly the case. Thanks for coming up with the detailed description.

stefandunn commented 5 years ago

I managed to come up with a work-around by setting the ref attribute on the component and then using this.$refs.refName.inputValue = 'Some value'.

<vue-bootstrap-typeahead 
    v-model="town"
    :data="towns"
    :inputClass="'large-input'"
    placeholder="Enter your town or city"
    ref="townTypeahead"
/>

Then in the mounted() lifecycle, use:

this.$refs.townTypeahead.inputValue = 'London'

jonbartlam commented 5 years ago

@stefandunn Much neater fix. I hadn't thought about just omitting the $data (I'm still picking vue up). Cheers.

I also note that there is a branch of this project with this issue updated but it hasn't been merged.

raymondmuller commented 5 years ago

https://github.com/alexurquhart/vue-bootstrap-typeahead/pull/21

AraneaDev commented 5 years ago

#32

siamkreative commented 5 years ago

@alexurquhart Is this repository no longer maintained?

natha9404 commented 5 years ago

I managed to come up with a work-around by setting the ref attribute on the component and then using this.$refs.refName.inputValue = 'Some value'.

<vue-bootstrap-typeahead 
    v-model="town"
    :data="towns"
    :inputClass="'large-input'"
    placeholder="Enter your town or city"
    ref="townTypeahead"
/>

Then in the mounted() lifecycle, use:

this.$refs.townTypeahead.inputValue = 'London'

This doesn't working for me :/, some idea?

joelhickok commented 5 years ago

I managed to come up with a work-around by setting the ref attribute on the component and then using this.$refs.refName.inputValue = 'Some value'.

<vue-bootstrap-typeahead 
    v-model="town"
    :data="towns"
    :inputClass="'large-input'"
    placeholder="Enter your town or city"
    ref="townTypeahead"
/>

Then in the mounted() lifecycle, use:

this.$refs.townTypeahead.inputValue = 'London'

This is great, but we shouldn't have to be doing this. v-model should just work on this component... so there is a bug in there somewhere, although I just started using this and didn't look at the code.

mattzollinhofer commented 4 years ago

I believe that I've merged a fix for this into my fork (https://github.com/mattzollinhofer/vue-typeahead-bootstrap) of this project that I'm actively maintaining. Give it a try and let me know if this is still an issue!

You can find it on npm here: https://www.npmjs.com/package/vue-typeahead-bootstrap.

I hope this helps!

mattzollinhofer commented 4 years ago

I just double checked this issue and it is resolved on my branch. You can now set the initial value and update it as necessary. Report any more issues here https://github.com/mattzollinhofer/vue-typeahead-bootstrap/issues

StrongLucky commented 4 years ago

I believe that I've merged a fix for this into my fork (https://github.com/mattzollinhofer/vue-typeahead-bootstrap) of this project that I'm actively maintaining. Give it a try and let me know if this is still an issue!

You can find it on npm here: https://www.npmjs.com/package/vue-typeahead-bootstrap.

I hope this helps!

Thank you. This is working!

pappascd81 commented 4 years ago

I believe that I've merged a fix for this into my fork (https://github.com/mattzollinhofer/vue-typeahead-bootstrap) of this project that I'm actively maintaining. Give it a try and let me know if this is still an issue!

You can find it on npm here: https://www.npmjs.com/package/vue-typeahead-bootstrap.

I hope this helps!

That broke the serializer. It now shows [Object object].

mattzollinhofer commented 4 years ago

That broke the serializer. It now shows [Object object].

@pappascd81: Interesting, it's working well on my end and others are reporting success. If you can provide a few more details as a new issue on the new repo I can try and see what's going on: https://github.com/mattzollinhofer/vue-typeahead-bootstrap/issues

juanca2626 commented 4 years ago

Creo que he fusionado una solución para esto en mi fork ( https://github.com/mattzollinhofer/vue-typeahead-bootstrap ) de este proyecto que mantengo activamente. Pruébalo y avísame si esto sigue siendo un problema.

Puede encontrarlo en npm aquí: https://www.npmjs.com/package/vue-typeahead-bootstrap .

¡Espero que esto ayude!

Perfecto funciona !!!