I created a custom FieldType that gathers data from 2 different Models. I was able to successfully display it as a Drop Down. My problem now is that there are tooo many records on my array and I would like to have a search instead similar to what craft already has implemented on the Tags Field.

My current template is:

    {%- set class = [
    (class is defined ? class : null)
    ]|filter|join(' ') %}

    {% set selectedValue = (value is defined ? value : null)  %}

    <div class="{{ class }}">
        <select {%- if name is defined %} name="{{ name }}"{% endif %}>
    {% if value is not null %}
    {% set selectedValue = value.id %}
    {% endif %}
    {% else %}
    <option value="0" > </option>
    {% endif %}

    {% for test in tests %}
    {% set optionValue = test.id %}
    <option value="{{test.id}}" {% if optionValue == selectedValue %} selected{% endif %}>Test - {{ test.name }}: {{ test.price }}</option>
            {% endfor %}

Any way I can make it similar to the craft/app/templates/_components/fieldtypes/Tags/input.html

  • Would it be an option to use an external library like select2.org/dropdown ? Otherwise you'll need to create a custom script for it – Robin Schambach Jun 29 '18 at 13:58
  • That would be amazing. not sure how to implement. any advice? – juanmrad Jun 29 '18 at 13:59

You could use Select2 to search in a select field

$(document).ready(function() {

All you need to do is to include the js file via asset bundle or register it in your view directly and execute the JS code

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.