Selectize.js custom rendering with static html
Asked Answered
N

3

14

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

Nonperishable answered 21/4, 2014 at 21:45 Comment(2)
Have you handled this issue? I actually want to display custom items but don't want to use ajax to get the data. Just want to do it with static HTML somehowEmancipate
Hi there. Did you solve it ? I'm kind of the same ship.Stunner
T
10

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);
        };
    }
});
Truda answered 13/9, 2014 at 11:43 Comment(2)
This does not answer the questionUnbutton
Question is how to do this using static HTMLChatterer
I
3

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.

Imbibe answered 13/3, 2015 at 15:16 Comment(1)
But how to find the option tag attributes inside render ?Stunner
S
0

I had the same problem couple minutes ago. I've add little code to selectize.js

In function init_select and next addOption after default code, I've add:

    /// iterate on data attr on <option>
    $.each($option.data(), function(i, v) {
       option[i] = v;
    });

Paste this code below original lines:

var option             = readData($option) || {};
option[field_label]    = option[field_label] || $option.text();
option[field_value]    = option[field_value] || value;
option[field_optgroup] = option[field_optgroup] || group;

After this my render method in selectize works fine with static data attributes :)

Sunburst answered 21/4, 2016 at 13:47 Comment(2)
this seems like a smart approach to solve a commonly discussed Selectize issue... think you can elaborate on this solution a bit?Inanity
@JeffSolomon what do you mean? :) do you mean the extension of selectize? This is old answer maybe problem is solved out of the box right nowSunburst

© 2022 - 2024 — McMap. All rights reserved.