Last active
August 29, 2015 14:19
-
-
Save DanWebb/8695272c7016c6bb7cb1 to your computer and use it in GitHub Desktop.
Shopify admin like custom select box with bootstrap and jquery
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.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; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$.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); | |
}); | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment