简体   繁体   English

在Aurelia中集成select2插件

[英]Integrating select2 plugin in aurelia

I'm integrating select2 plugin in aurelia and I'm facing problem while binding a data which I receive from ajax call and takes few seconds after my custom element is rendered. 我正在将select2插件集成到aurelia中,并且在绑定从ajax调用接收到的数据并呈现自定义元素后需要几秒钟的数据时遇到了问题。

 import 'select2' import 'select2/css/select2.css!' import { bindable, bindingMode, customElement, inject } from 'aurelia-framework' import $ from 'jquery' @customElement('select2') @inject(Element) export class CustomSelect { @bindable name = null @bindable({ defaultBindingMode: bindingMode.twoWay }) selected = {} @bindable options = [] @bindable labelField = "label" @bindable valueField = "value" constructor(element) { this.element = element } bind() { this.isComplexModel = this.options && this.options.length > 1 && (typeof this.options[0] !== "string" && typeof this.options[0] !== "number") this.translateModel() this.selectedValue = this.options[0].value this.selectedValue = !this.selected ? this.selectedValue : this.selected[this.valueField] } attached() { $(this.element).find('select') .val(this.selectedValue) .select2() .on('change', (event) => { if (this.isComplexModel) { this.selected = event.currentTarget.selectedOptions[0].model } else { this.selected = event.currentTarget.selectedOptions[0].model.value } let changeEvent if (window.CustomEvent) { changeEvent = new CustomEvent('change', { detail: { value: event.target.value }, bubbles: true }) } else { changeEvent = document.createEvent('CustomEvent') changeEvent.initCustomEvent('change', true, true, { detail: { value: event.target.value } }) } $(this.element).val(event.target.value) this.element.dispatchEvent(changeEvent) }) } translateModel() { if (this.isComplexModel) { this.options = this.options.map((option) => $.extend(option, {"label": option[this.labelField], "value": option[this.valueField]})) } else { this.options = this.options.map((option) => $.extend({}, {"label": option, "value": option})) } } } 
 <template> <select name.bind="name" id.bind="name"> <option repeat.for="option of options" value.bind="option.value" model.bind="option">${option.label}</option> </select> </template> 

In this code, attached only gets called once (at that point, the options provided is undefined) and I couldn't found any way to update the select2 with the latest data which I gets from the API. 在这段代码中,attach仅被调用一次(此时,提供的选项是未定义的),我找不到用我从API获得的最新数据更新select2的任何方法。

I need your help to make this custom element workable as and when the options changes its state. 我需要您的帮助,以使此自定义元素在选项更改其状态时也可以使用。 Thanks in advance! 提前致谢!

I realize that it's a long time since this question was asked, but i have just done an implementation of Select2 v4.0.3 in Aurelia myself. 我意识到自问这个问题以来已经有很长时间了,但是我自己已经在Aurelia中完成了Select2 v4.0.3的实现。 I thought i could share it here with you. 我以为可以在这里与您分享。 It's a bit different approach than you, and it might not be the perfect solution, but you can take a look. 与您的做法有些不同,它可能不是完美的解决方案,但您可以看一下。 Maybe you will get inspired :) 也许你会得到启发:)

https://github.com/Kla3mus/select24aurelia https://github.com/Kla3mus/select24aurelia

Single and multi select modes should work. 单选和多选模式应该起作用。 Select2 is a bit picky when it comes to the values you set, so selected should be an integer array. 对于设置的值,Select2有点挑剔,因此selected应该是一个整数数组。

It's in typescript, not JavaScript, but it should be fairly easy to modify it to your needs. 它使用打字稿,而不是JavaScript,但是根据需要对其进行修改应该相当容易。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM