Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extended options search query #50

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,14 @@ multi(select_element, {
});
```

### Extended search

You can extended your options search abilities by adding custom search parameters in `data-search=""` to your options.

```html
<option data-search="delicious taste for a pie">Apple</option>
```

### jQuery

multi.js is fully native Javascript but also has jQuery support. If you have jQuery included multi can be applied to a select element as follows:
Expand Down
4 changes: 2 additions & 2 deletions dist/multi-es6.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions dist/multi.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

57 changes: 57 additions & 0 deletions examples/extended-search.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>multi.js Extended Search Example</title>

<!-- Include multi.js -->
<link rel="stylesheet" type="text/css" href="../dist/multi.min.css" />
<script src="../dist/multi.min.js"></script>

<style>
/* Basic styling */
body {
font-family: sans-serif;
}

.container {
box-sizing: border-box;
margin: 0 auto;
max-width: 500px;
padding: 0 20px;
width: 100%;
}
</style>
</head>
<body>
<div class="container">
<h1>multi.js</h1>

<form>
<select
multiple="multiple"
name="favorite_fruits"
id="fruit_select"
>
<option data-search="Fuji Gala Pink Lady">Apple</option>
<option>Banana</option>
<option>Blueberry</option>
<option>Cherry</option>
<option>Coconut</option>
<option>Grapefruit</option>
<option>Kiwi</option>
<option>Lemon</option>
<option>Lime</option>
<option>Mango</option>
<option data-search="Clementine Mandarine Tangerine">Orange</option>
<option>Papaya</option>
</select>
</form>
</div>

<script>
var select = document.getElementById("fruit_select");
multi(select)
</script>
</body>
</html>
8 changes: 7 additions & 1 deletion src/multi.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ var multi = (function() {

var value = option.value;
var label = option.textContent || option.innerText;
var search = option.getAttribute('data-search')

var row = document.createElement("a");
row.tabIndex = 0;
Expand All @@ -119,6 +120,10 @@ var multi = (function() {
row.setAttribute("data-value", value);
row.setAttribute("multi-index", i);

if (search) {
row.setAttribute("data-search", search);
}

if (option.disabled) {
row.className += " disabled";
}
Expand Down Expand Up @@ -158,7 +163,8 @@ var multi = (function() {
// Apply search filtering
if (
!query ||
(query && label.toLowerCase().indexOf(query.toLowerCase()) > -1)
(query && label.toLowerCase().indexOf(query.toLowerCase()) > -1) ||
(query && search && search.toLowerCase().indexOf(query.toLowerCase()) > -1)
) {
// Append to group if one exists, else just append to wrapper
if (item_group != null) {
Expand Down