如何使用 selectize.js 获取选定的 ID 和文本值。流星?

How to get get selected id and text value using selectize.js. meteor?

我通过这个例子将 selectize 与 meteor 结合使用: meteor is not working with selectize.js

Template.hello.onRendered(function(){
$('#select-links').selectize({
  maxItems: null,
  valueField: 'id',
  searchField: 'title',
  options: [
    {id: 1, title: 'DIY', url: 'https://diy.org'},
    {id: 2, title: 'Google', url: 'http://google.com'},
    {id: 3, title: 'Yahoo', url: 'http://yahoo.com'},
  ],
  render: {
    option: function(data, escape) {
      return '<div class="option">' +
          '<span class="title">' + escape(data.title) + '</span>' +
          '<span class="url">' + escape(data.url) + '</span>' +
        '</div>';
    },
    item: function(data, escape) {
      return '<div class="item"><a href="' + escape(data.url) + '">' + escape(data.title) + '</a></div>';
    }
  },
  create: function(input) {
    return {
      id: 0,
      title: input,
      url: '#'
    };
  }
});

});
<!-- html -->
<template name="hello">
<select id="select-links" placeholder="Pick some links..."></select>
</template>

所以在 onChange 事件中我想得到

value and id

要保存到数据库的那个值。

结果可能是这样的:{id: 1, text: google}

那我该怎么做呢?

来自流星新手

您可以利用开箱即用的 selectize 支持的 onChange 回调。这是一个工作示例。

import { Template } from 'meteor/templating';
import { $ } from 'meteor/jquery';
import { _ } from 'meteor/underscore';

import selectize from 'selectize';

import './main.html';
import 'selectize/dist/css/selectize.css';

const selectLinks = [
  { id: 1, title: 'DIY', url: 'https://diy.org' },
  { id: 2, title: 'Google', url: 'http://google.com' },
  { id: 3, title: 'Yahoo', url: 'http://yahoo.com' },
];

const getLinkTitle = (id) => {
  let title;
  if (id) {
    const selectedLink = _.find(selectLinks, (link) => {
      return link.id === parseInt(id);
    });
    if (selectedLink) {
      title = selectedLink.title;
    }
  }
  return title;
};

Template.body.onRendered(function onRendered() {
  $('#select-links').selectize({
    maxItems: null,
    valueField: 'id',
    searchField: 'title',
    options: selectLinks,
    render: {
      option: function(data, escape) {
        return '<div class="option">' +
            '<span class="title">' + escape(data.title) + '</span>' +
            '<span class="url">' + escape(data.url) + '</span>' +
          '</div>';
      },
      item: function(data, escape) {
        return '<div class="item"><a href="' + escape(data.url) + '">' + escape(data.title) + '</a></div>';
      }
    },
    create: function(input) {
      return {
        id: 0,
        title: input,
        url: '#'
      };
    },
    onChange: function (values) {
      if (values) {
        values.forEach((value) => {
          // Can save to your collection/database here; for now 
          // just logging in the format you requested.
          console.log({
            id: value,
            text: getLinkTitle(value)
          });
        });
      }
    }
  });
});

注意这一点:

const getLinkTitle = (id) => {   let title;   if (id) {
    const selectedLink = _.find(selectLinks, (link) => {
      `return link.id === parseInt(id);`
    });
    if (selectedLink) {
      title = selectedLink.title;
    }   }   return title; };
 `return link.id === parseInt(id);`

注意那行代码。这取决于你的身份证。我正在使用 mongoDB,因此在我的实际应用程序中它不再是数字。