Setting value to Select2 working with remote data

Visto 4.030 veces
Saltar al primer mensaje no leído

Sevil YILMAZ

no leída,
14 jul 2012, 17:07:0914/7/12
a sel...@googlegroups.com
Hi,
Here is the scenario:
There is a list of managers and their infos(Bank Name, IBAN Number...) on the page. If the user(admin) wants to add a manager to the list, he or she can type the name and find the manager to add. Here is the code for this:

Manager.nameInput.select2({
  minimumInputLength: 2,
  ajax: {
    url: 'assets/data/managers.json',
    dataType: 'json',
    data: function (term) {
      return {
        q: term
      };
    },
    results: function (data) {
      return {results: data.managers};
    }
  },
  formatResult: nameFormatResult,
  formatSelection: nameFormatSelection
});

and if the user wants to update the managers informations, he or she can click the update button in the list item. To provide this I wrote this code:
Manager.nameInput.select2('val', {id: Manager.$id, text:Manager.$name.text()});
(and I have to disable selection after updating value)

But it doesn't set this value. I think it's because select2 initilized with ajax at the beginning.

Is there any solution for this?

Thanks,
Sevil

Igor Vaynberg

no leída,
14 jul 2012, 17:46:1314/7/12
a sel...@googlegroups.com
the select2('val', object) works irregardless of how select2 has been
initialized. check for a js error in the console. it seems like the
code you provided should work.

-igor
> --
> You received this message because you are subscribed to the Google Groups
> "select2" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/select2/-/mwlygQKDDksJ.
> To post to this group, send email to sel...@googlegroups.com.
> To unsubscribe from this group, send email to
> select2+u...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/select2?hl=en.

Sevil YILMAZ

no leída,
14 jul 2012, 17:58:3114/7/12
a sel...@googlegroups.com
There is no error. It changes the value of the input. But select2 stays with placeholder.

---

Sevil YILMAZ

Front-end Developer

professional blog: codersgrave.com

personal blog: mudadost.org

Igor Vaynberg

no leída,
15 jul 2012, 4:43:5415/7/12
a sel...@googlegroups.com
not really sure. i use it with this exact usecase all the time and it appears to be working fine...

-igor

Sevil YILMAZ

no leída,
15 jul 2012, 5:28:4415/7/12
a sel...@googlegroups.com
for quick solution of this I destroy select2 and then disable the input.


---

Sevil YILMAZ

Front-end Developer

professional blog: codersgrave.com

personal blog: mudadost.org



agg....@gmail.com

no leída,
9 ago 2012, 16:49:599/8/12
a sel...@googlegroups.com
I am having the same issue, I initialize select2 with the ajax data but when the user clicks on "Show Only Mine" link i have an object that i need to give to select2.
I looked at the docs and i see taht there is a "data" attribute you can use
$(selector).select2("data",{my data object }); but it gives me
"Unknown method: data".
As Sevil mentioned doing the val on select doesn't work for me too.

On Sunday, July 15, 2012 2:28:44 AM UTC-7, Sevil YILMAZ wrote:
> for quick solution of this I destroy select2 and then disable the input.
>
>
>
>
>
>
> ---
>
>
> Sevil YILMAZ
>
> Front-end Developer
>
>
> professional blog: codersgrave.com
>
> personal blog: mudadost.org
>
>
>
>
> On Sun, Jul 15, 2012 at 11:43 AM, Igor Vaynberg <igor.v...@gmail.com> wrote:
>
>
> not really sure. i use it with this exact usecase all the time and it appears to be working fine...
>
>
> -igor
>
>
>
>
>
> On Sun, Jul 15, 2012 at 12:58 AM, Sevil YILMAZ <sevil...@gmail.com> wrote:
>
>
> There is no error. It changes the value of the input. But select2 stays with placeholder.
>
>
>
>
>
>
>
>
>
> ---
>
>
>
> Sevil YILMAZ
>
> Front-end Developer
>
>
> professional blog: codersgrave.com
>
> personal blog: mudadost.org
>
>
>
>
>
>
>
>
>

agg....@gmail.com

no leída,
9 ago 2012, 16:58:469/8/12
a sel...@googlegroups.com,agg....@gmail.com
Ok i think i know the problem, seems like you might be using an older version of select2. Try upgrading it and see if you see the same problem.

Thanks
Varun
Responder a todos
Responder al autor
Reenviar
0 mensajes nuevos