DanWebb
4/20/2015 - 5:33 AM

Shopify admin like custom select box with bootstrap and jquery

Shopify admin like custom select box with bootstrap and jquery

$.fn.customSelect = function(callback) {
    function toggle() {
        $(this).parents('.custom-select').find('.options').toggle(400);
    }

    function search() {
        var $this = $(this);
        var $options = $this.parents('.custom-select').find('.options li:not(.select-search)');
        var searchTerm = $this.val().toLowerCase();
        var option = '';

        $options.each(function() {
            option = $(this).text().toLowerCase();
            option.indexOf(searchTerm) > -1 ? $(this).show() : $(this).hide();
        });
    }

    function selectOption(e) {
        e.preventDefault();

        var $this = $(this);
        var $select = $this.parents('.custom-select');
        var $selected = $select.find('span:first');
        var val = $this.attr('data-val');

        $selected.attr('data-val', $this.attr('data-val'));
        $selected.find('span').text($this.text());
        $selected.click();
        
        if(typeof callback==='function') return callback(val, $select);
    }

    this.each(function() {
        var $this = $(this);

        $this.find('span:first').click(toggle);
        $this.find('input[name="select-search"]').keyup(search);
        $this.find('.options a').click(selectOption);
    });
};

$(function() {
    $('.custom-select').customSelect(function(val, $select) {
        console.log(val);
    });
});


<div class="custom-select">
    <span data-val=""><span>Select an option</span> <i class="glyphicon glyphicon-triangle-bottom pull-right"></i></span>
    <ul class="options list-unstyled">
        <li class="select-search"><input type="text" name="select-search" /></li>
        <li><a href="#" data-val="value">An Option</a></li>
    </ul>
</div>
.custom-select {
    position: relative;
    width: 100%;
}
.custom-select > span {
    border-radius: 3px;
    border: 1px solid #d3dbe2;
    padding: 5px 15px;
    color: #479ccf;
    cursor: pointer;
    display: block;
}
.custom-select > span:hover { background: #F4F4F4; }
.custom-select > span i {
    font-size: 10px;
    margin-top: 3px;
}
.custom-select .options {
    display: none;
    position: absolute;
    top: 34px;
    background: #fff;
    border: 1px solid #dee3e8;
    border-radius: 3px;
    box-shadow: 0 2px 4px rgba(0,0,0,0.1);
    z-index: 1;
    width: 100%;
}
.custom-select .options li {
    padding: 5px 10px;
    width: 100%;
}
.custom-select .options li:hover {  background: #F4F4F4; }
.custom-select .options li.select-search {
    border-bottom: 1px dotted #479ccf;
    padding: 0;
}
.custom-select .options li.select-search input {
    border: none;
    padding: 5px;
    width: 100%;
}
.custom-select .options li a {
    width: 100%;
    color: #479ccf;
    display: block;
}