vue-bootstrap-typeahead icon indicating copy to clipboard operation
vue-bootstrap-typeahead copied to clipboard

Initial selected value

Open bogdancarmanus opened this issue 5 years ago • 15 comments

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

bogdancarmanus avatar Oct 05 '18 14:10 bogdancarmanus

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:

  • User clicks a record from a table and a record is retreived from the database. Part of the record contains an id to a location object and the location object itself (basically, a region and postcode/zip code).
  • Form is loaded with record and the location object's text representation is shown (concatenated text) inside the typeahead.
  • User clicks in field to edit and when typing it will suggest records.
  • On select, the id of the record selected is put into the location id field.

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

jonbartlam avatar Oct 07 '18 21:10 jonbartlam

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

bogdancarmanus avatar Oct 08 '18 06:10 bogdancarmanus

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'

stefandunn avatar Oct 10 '18 07:10 stefandunn

@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.

jonbartlam avatar Oct 10 '18 09:10 jonbartlam

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

raymondmuller avatar Oct 11 '18 07:10 raymondmuller

#32

AraneaDev avatar Dec 22 '18 00:12 AraneaDev

@alexurquhart Is this repository no longer maintained?

siamkreative avatar Apr 11 '19 08:04 siamkreative

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?

natha9404 avatar May 24 '19 17:05 natha9404

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.

joelhickok avatar Jun 17 '19 13:06 joelhickok

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 avatar Feb 26 '20 03:02 mattzollinhofer

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

mattzollinhofer avatar Feb 28 '20 03:02 mattzollinhofer

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!

StrongLucky avatar Mar 04 '20 12:03 StrongLucky

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].

pappascd81 avatar Mar 07 '20 04:03 pappascd81

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

mattzollinhofer avatar Mar 11 '20 03:03 mattzollinhofer

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 !!!

juanca2626 avatar Jul 24 '20 05:07 juanca2626