Git Product home page Git Product logo

bootstrap-select-meteor's Introduction

Bootstrap-Select

Meteor packaging for Bootstrap-Select, an enhancement for <select> inputs, with custom styling and icons, multiple selections, local autocomplete/filtering/live-search and size/width control. Doesn't support tags mode input - use Select2 for that.

##Usage

Defining the templates:

<template name="students">
  <select id="students">
    {{#each students}}
      {{> student}}
    {{/each}}
  </select>
</template>
          
<template name="student">
  <option>{{name}}</option>
</template>

The two separate templates are required to catch updates on the underlying collection. See rendered events below.

Creating collection and assigning it to the template students:

var students = new Meteor.Collection(null);

students.insert({name: "Albert Einstein"});
students.insert({name: "Tim Berners-Lee"});
students.insert({name: "Bill S. Preston, Esquire"});

Template.students.students = function() {
  return students.find({});
}

When the select is rendered, fire the selectpicker on it:

Template.students.rendered = function(){
  $('#students').selectpicker();
};

In Blaze the rendered event would only be thrown once when the template is rendered first. To refresh the selectpicker whenever the underlying collection has changed, we have to add another hook to the student template:

var renderTimeout = false;
Template.student.rendered = function(){
  if (renderTimeout !== false) {
    Meteor.clearTimeout(renderTimeout);
  }
  renderTimeout = Meteor.setTimeout(function() {
    $('#students').selectpicker("refresh");
    renderTimeout = false;
  }, 10);
};

This event will be fired for every option which is rendered. This means if the collection has 100 documents, the selectpicker would be refreshed 100 times. On big collections this leads to a few seconds freeze in the browser. Therefore the small renderTimeout to only fire once on a bulk of updates.

Catching the user selection:

Template.students.events({
  'change #students': function(e) {
    var student = $("#students").val();
    console.log(student);
  }
});

If I forgot to upgrade to the latest version, please start a new issue or notify me and I'll happily upgrade it for you. Or if you want to be nice and save me a few seconds; fork it, make a PR and I'll merge it.

Author: Amr Ali (amralicc AT gmail.com)

bootstrap-select-meteor's People

Contributors

amrali avatar dandv avatar udondan avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.