简体   繁体   English

如何使用Aurelia的bootstrap-select或任何其他js控件?

[英]How to use bootstrap-select or any other js control with Aurelia?

Not sure what I am missing here, probably something silly, but I am unable to find anything regarding, let's say how to use bootstrap-select control in Aurelia views. 不知道我在这里缺少什么,可能是愚蠢的,但我无法找到任何关于,让我们说如何在Aurelia视图中使用bootstrap-select控件。 Can someone point me to the right article please? 有人能指出我正确的文章吗? PS: I am not looking to create another custom control out of bootstrap-select but use as it as. PS:我不打算用bootstrap-select创建另一个自定义控件但是使用它。 Request for your help. 请求您的帮助。 https://silviomoreto.github.io/bootstrap-select/ https://silviomoreto.github.io/bootstrap-select/

You can create a custom attribute that adds the bootstrap-select behavior to the <select> element. 您可以创建一个自定义属性,将bootstrap-select行为添加到<select>元素。 Here's an example: 这是一个例子:

http://plnkr.co/edit/So23Hm?p=preview http://plnkr.co/edit/So23Hm?p=preview

bootstrap-select.js 引导-select.js

import {inject} from 'aurelia-framework';

const defaultOptions = {
  style: 'btn-info',
  size: 4
};

@inject(Element)
export class BootstrapSelectCustomAttribute {
  constructor(element) {
    this.element = element;
  }

  attached() {
    let options = Object.assign({}, defaultOptions, this.value || {});
    $(this.element).selectpicker(options);
  }

  detached() {
    $(this.element).selectpicker('destroy');
  }
}

app.html : app.html

<template>
  <require from="./bootstrap-select"></require>

  <select value.bind="selectedPlanet" bootstrap-select>
    <option model.bind="null">Select a planet</option>
    <option repeat.for="planet of planets" model.bind="planet">${planet.name}</option>
  </select>
</template>

app.js : app.js

export class App {
  selectedPlanet = null;
  planets = [
    { name: 'Mercury', diameter: 3032 },
    { name: 'Venus', diameter: 7521 },
    { name: 'Earth', diameter: 7926 },
    { name: 'Mars', diameter: 4222 },
    { name: 'Jupiter', diameter: 88846 },
    { name: 'Saturn', diameter: 74898 },
    { name: 'Uranus', diameter: 31763 },
    { name: 'Neptune', diameter: 30778 }];
}

Pass options to the selectpicker call like this: 将选项传递给selectpicker调用,如下所示:

<select bootstrap-select.bind="{ size: 4 }">

Or like this: 或者像这样:

<select bootstrap-select.bind="myOptions"> <!-- assumes there's a myOptions property on your view-model -->

You need to fit it into the Aurelia lifecycle, but other than that, you're free to do as you like. 你需要将它融入Aurelia生命周期,但除此之外,你可以自由地按照自己的意愿去做。

If you look here you will find a couple of examples of using jQuery plugins in Aurelia. 如果你看这里你会发现一些在Aurelia中使用jQuery插件的例子。 A relatively complex example being the Autocomplete widget. 一个相对复杂的例子是自动完成小部件。 This is a custom control wrapping the jQuery plugin, which I think you don't want to do, but it should still give you an idea of how to implement it. 这是一个自定义控件包装jQuery插件,我认为你不想这样做,但它仍然应该让你知道如何实现它。

Extending Jeremy's answer and partially solving the refresh question, you could add something like this. 扩展Jeremy的答案并部分解决刷新问题,你可以添加这样的东西。

I couldn't find any legal way to get an event when the source of the repeater changed in a custom attribute. 在自定义属性中更改转发器的源时,我找不到任何合法的方法来获取事件。 If anyone knows a better/elegant/decent way, please share. 如果有人知道更好/优雅/体面的方式,请分享。

Based on this answer Two way binding not working on bootstrap-select with aurelia , you could add a "task" that queries the length of the select and if the length changes, call the refresh. 根据这个答案双向绑定不能用于aurelia的bootstrap-select ,你可以添加一个查询select长度的“任务”,如果长度发生变化,则调用刷新。

With a little variation from the original, I decided to abort the task when the elements length changes the first time. 与原始版本略有不同,我决定在元素长度第一次更改时中止任务。 In my case, they will always change only once, when updated after getting them from the database. 在我的情况下,从数据库中获取更新后,它们将始终只更改一次。

    bind() {
        var _this = this;
        var sel = this.element;
        var prevLen = sel.options.length || 0;
        var addOpt = setInterval(function () {
            var len = sel.options.length || 0;
            if (len != prevLen || len > 0) {
                clearTimeout(addOpt); //abort the task
                $(_this.element).selectpicker("refresh");
            }
        }, 250);
    };

As a way of disclaimer, I'm against writing code like this. 作为免责声明的一种方式,我反对编写这样的代码。 It wastes resources when there should be a better way to solve this problem. 当应该有更好的方法来解决这个问题时,它会浪费资源。

Finally, I started saying that it partially solved the refresh problem. 最后,我开始说它部分解决了刷新问题。 Since it aborts the execution after the first change (assuming that no more changes are going to happen), if the items change more than once, this task would need to run forever. 由于它在第一次更改后中止执行(假设不会发生更多更改),如果项目更改多次,则此任务需要永久运行。

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

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