I am using the brilliant selectize.js library to generate an attractive select box with option groups. It is all working but I am stuck at the point that I cannot use the custom renderer from the examples page (Email contacts) http://brianreavis.github.io/selectize.js/ because "item" does not know of the "email" attribute. I know how to do this in javascript, but how could I define the two attributes in static html?
In js, this woulde be
$('#id').selectize({
  ...
  options: [
    { name: "Martin", email: "[email protected]" }
  ],
  ....
}
I tried the following:
<select>
  <option value="Martin|[email protected]" data-name="Martin" data-email="[email protected]">
    Martin
  </option>
</select>
But this is not working... Finally the render function taken from the examples:
render: {
    item: function(item, escape) {
        return '<div>' +
            (item.name ? '<span class="name">' + escape(item.name) + '</span>' : '') +
            (item.email ? '<span class="email">' + escape(item.email) + '</span>' : '') +
        '</div>';
    },
    option: function(item, escape) {
        var label = item.name || item.email;
        var caption = item.name ? item.email : null;
        return '<div>' +
            '<span class="label">' + escape(label) + '</span>' +
            (caption ? '<span class="caption">' + escape(caption) + '</span>' : '') +
        '</div>';
    }
}
I would be thankful for any hints!
Regards, Martin
Use this example:
var clearhack = $('.selectized').selectize({
    valueField: 'id',
    labelField: 'name',
    searchField: ['name'],
    sortField: 'score',//this is set to 'name' on my version, but seems sortField is only used together with load-function and score-function
    sortDirection: 'desc',
    maxItems: 1,
    //only using options-value in jsfiddle - real world it's using the load-function
    options: [
        {"id":861,"name":"Jennifer","score":6},
        {"id":111,"name":"Jenny","score":6},
        {"id":394,"name":"Jorge","score":6},
        {"id":1065,"name":"Jorge Carlson","score":6},
        {"id":389,"name":"Ann Jennifer","score":3},
        {"id":859,"name":"Bobby Jenkins","score":3},
        {"id":264,"name":"Peter Jenkins","score":3},
        {"id":990,"name":"Fred","score":1},
        {"id":349,"name":"Kal","score":1},
        {"id":409,"name":"Louis","score":1}
    ],
    create: false,
    render: {
        option: function(item, escape) {
            return '<div>'
            + '<span>ID:'+item.id+'</span> '
            + '<span>Name:'+item.name+'</span> '
            + '<span>DEBUG:'+item.score+'</span>'
            + '</div>';
        }
    },
    score: function(search) {
        return function(item) {
            return parseInt(item.score);
        };
    }
});
                        Not sure if this will help as it's super late - but I used the following method to get captions under my select options:
html options like:
<option data-data='<?php echo json_encode($obj, JSON_FORCE_OBJECT) ?>' value="<?php echo $obj->id ?>"><?php echo $obj->name ?></option>
and then selectize code:
$('select#my-select').selectize({
  valueField: 'id',
  labelField: 'name',
  searchField: ['name'],
  render: {
    option: function(item, escape) {
      var label = item.name;
      var caption = item.description;
      return '<div>' +
      '<span style="display: block; color: black; font-size: 14px;">' + escape(label) + '</span>' +
      (caption ? '<span style="display: block; color: gray; font-size: 12px;">' + escape(caption) + '</span>' : '') +
      '</div>';
    }
  }
});
Haven't read a lot of the docs but this will work for $obj such as:
{ 'id': '1', 'name': 'fred', 'description': 'fred person'}
Just add more attributes and reference them in your render option function.
It seems selectize reads json from a data-data attribute to populate these, but I believe you can change what attribute it reads json from by passing a dataAttr option in the initialize.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With