question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Extra parameter templateSelection

See original GitHub issue

Hello i have this problem latest version of select 2

I inizialize select 2 with extra parameter:

let data = {
          id: this.caratteristica.CaratteristicaInfo.Id,
          text: this.caratteristica.CaratteristicaInfo.Nome,
          acronimo: this.caratteristica.CaratteristicaInfo.Acronimo,
          qualita: this.caratteristica.CaratteristicaInfo.Qualita
        };
var caratteristicaSelect = (<any>$(this.render.nativeElement));
var option = new Option(this.caratteristica.CaratteristicaInfo.Nome, this.caratteristica.CaratteristicaInfo.Id, true, true);
        
caratteristicaSelect.append(option).trigger('change');
caratteristicaSelect.trigger({
         type: 'select2:select',
         params: {
             data: data
         }
 });

but i don’t find this parameter on templateSelection so i can’t unable to render well data inside how do i ajax request.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:6

github_iconTop GitHub Comments

1reaction
razvanioancommented, Dec 11, 2019

+1

I’ve lost some time on this too (mostly creating a jsfiddle to demonstrate this)

I don’t have time to look over the source code to try to find a proper solution (and maybe propose a PR) but I’ve just crafted a custom / dirty hack on this, maybe somebody else will find this handy, it might be too late for @Toso82 after almost a year though

  1. When manually selecting a dynamically created option, just append custom data as data attribute on it (the option). You can choose yourself what data you need in the template and how to pass it: multiple data attributes or only one as JSON string with all data like I did
let data = {
  id : 1,
  text : 'Dynamically created option',
  custom : 'parameter'
}

let option = new Option(data.text, data.id, true, true)

option.dataset.params = JSON.stringify(data)

$(select).append(option).trigger('change')

// I don't think that triggering custom `select2:select` event is needed anymore
/*$(select).trigger({
  type: 'select2:select',
  params : {
    data : data
  }
})*/
  1. Inside template method, decode custom params if needed only
templateSelection : function (result, container) {
  // be sure `element` parameter is present (something selected) and data attribute configured (only when manually selected)
  if (result.element && 'params' in result.element.dataset) {
    try {
      // add parameters to `result` object
      Object.assign(result, JSON.parse(result.element.dataset.params))
    } catch (err) {
      console.error('Parameters not in JSON format')
    }
  }

  // you can safely use now `result.custom` parameter, from AJAX response or dynamically assigned
}
0reactions
jowestecommented, Sep 28, 2019

@Toso82 This is pretty much a dead issue tracker. I recommend you store the data in a global variable and look it up in the trigger callback.

I think it is not a good solution

Read more comments on GitHub >

github_iconTop Results From Across the Web

Select2 parameter for templateResult and templateSelection ...
Ok I have an answer. In the context of initProductSelect2() i create an options object that is populated in ProcessResults and then i ......
Read more >
templateSelection is not receiving the same JSON data ...
Then, templateSelection receives as 'data' parameter an standard JSON:{ ... and assuming there's “extra” data in the inserted item that you ...
Read more >
Passing extra data to select2 - Laracasts
I've got a multiple select box working well for 'colour' with select2 but I'm wanting to add the hex code colours next to...
Read more >
Conditional template selection & generation - S-Docs
The doclist can include as many S-Doc templates as you'd like. If you just append the doclist parameter, templates will be automatically ...
Read more >
Select2 3.5.3
For more info on the parameters, refer to the JQuery API Documentation. ... an object that contains extra parameters that will be passed...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found