Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The results converts to [object Object] under templateSelection #4438

Closed
HamzaLJ opened this issue Jun 23, 2016 · 5 comments
Closed

The results converts to [object Object] under templateSelection #4438

HamzaLJ opened this issue Jun 23, 2016 · 5 comments

Comments

@HamzaLJ
Copy link

HamzaLJ commented Jun 23, 2016

I using the latest version of select2 library; however, I am having an issue which I believe it's a bug.

I am using Select2 in order to fetch some data from a rest api, until now everything is fine I can see the data that I want when searching; however when I click on that result using the below lines:

templateSelection: function(results){
                return results.Name;
                               //the below works fine
                               //return 'This is a plain text'
            }

What I see is [object Object] knowing that 'results' is an array and 'results.Name' is a string, but when it goes under 'templateSelection' everything converts to [object Object]
For you notes, the same array works fine under 'templateResult', the issue is only in 'templateSelection'

Is anybody facing this issue too? Is this a bug? How can I solve it?

@kevin-brown
Copy link
Member

Can you expand upon the issue you are facing by adding a few more pieces of information to your bug report?

  • What steps can be used to reproduce the issue?
  • What is the bug and what is the expected outcome?
  • What browser(s) and Operating System have you tested with?
  • Does the bug happen consistently across all tested browsers?
  • Can you reproduce this issue on the examples page?
  • What version of jQuery are you using? And what version of Select2?
  • Are you using Select2 with other plugins?

The contributing guide contains a useful section with a few questions which can help us track down the bug and speed up the process of finding a fix for it. The creation of an isolated test case would also be useful.

@in-in
Copy link

in-in commented Jun 23, 2016

@miguelsmuller
Copy link

I have the same problem. this is my return JSON:

{
    "results": [{
            "id": "1",
            "text": {
                "iua": "968448600731",
                "nomeCompleto": "Luciana Quintana",
                "cpf": "62291584421",
                "dtNascimento": "18/11/1976"
            }
    }]
}

my templateSelection option is this one:

function customSelectText (optionSelected) {
  if (!optionSelected.id) {
    return item.text.nomeCompleto;
  }

  var $contentHTML = '<b>' + optionSelected.text.nomeCompleto + '</b> (' + optionSelected.text.dtNascimento + ')';

  return $contentHTML;
};

it's happening that my optionSelected being converted to [object Object] . I am unable to access internal items.

The error does not happen in the templateResult option. the listing is displayed perfectly.

@miguelsmuller
Copy link

someone?

@miguelsmuller
Copy link

no one?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants