简体   繁体   English

Select2和Meteor.js怎么做?

[英]Select2 and Meteor.js how to?

I've only been using javascript / jquery and meteor for a couple of weeks now, so forgive me for asking what will be a simple question. 我现在只使用javascript / jquery和流星几周了,所以请原谅我问一个简单的问题。

I'm trying to use Select2 in my Meteor.js app, and can't figure out how to get it working. 我正在尝试在Meteor.js应用程序中使用Select2,但无法弄清楚如何使其工作。 I have installed natestrauser:select2 in my project and my jsfiddle shown just won't work. 我已经在项目中安装了natestrauser:select2,但是显示的jsfiddle无法正常工作。 https://jsfiddle.net/jt0jr9uk https://jsfiddle.net/jt0jr9uk

Template.createjob.onCreated( function() {
    $("clientlist").select2({
        placeholder: "Select a client",
        allowClear: false,
    });
});

And html: 和html:

<span class="input-group-addon"><i class="glyphicon glyphicon-user"></i></span>
<select id="clientList" class="form-control">
    <option value=""></option>
    <option value="aberfitch">Abercrombie & Fitch</option>
    <option value="bent">Bentley</option>
    <option value="barb">Barbour</option>
    <option value="dcsh">DC Shoes</option>
    <option value="echo">ECHO United</option>
</select>

I'm using bootstrap also. 我也在使用引导程序。 Which I've added to my project. 我已将其添加到我的项目中。 And basically I just want the single select box as shown here https://select2.github.io/examples.html#single 基本上我只想要一个选择框,如下所示:https://select2.github.io/examples.html#single

Should I be using any imports, or anything? 我应该使用任何进口货吗?

So first of all, on js level you tried to select an id, named clientlist 因此,首先,在js级别上,您尝试选择一个名为clientlist的ID。

Template.createjob.onCreated( function() {
    $("clientlist").select2({....

However, on html level, your id is defined in camelCase 但是,在html级别,您的ID是在camelCase中定义的

<select id="clientList" class="form-control">

Second of all, when you select an id, you should use # sign before the id, what I mean is your jquery should look like this to achive the desired result: 其次,当您选择一个ID时,应在ID之前使用#号,我的意思是您的jquery应该看起来像这样才能达到所需的结果:

   Template.createjob.onCreated( function() {
      $("#clientList").select2({
          placeholder: "Select a client",
          allowClear: false,
      });
    });

You need to import jquery on top of the document if you use meteor >1.3 as 如果您使用流星> 1.3作为,则需要在文档顶部导入jquery

import { $ } from 'meteor/jquery';

As a final suggestion, I would use Template.createjob.onRendered() instead for Template.createjob.onCreated() 作为最后的建议,我将使用Template.createjob.onRendered()代替Template.createjob.onCreated()

The template rendered callback is fired after the DOM is rendered on screen. 在屏幕上呈现DOM之后,将触发呈现模板的回调。 The created callback is fired when the Template is instantiated but not yet rendered 实例化但尚未呈现模板时,将触发创建的回调

Check the following link for the full explanation https://stackoverflow.com/a/28783762/7235661 检查以下链接以获取完整说明https://stackoverflow.com/a/28783762/7235661

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

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