Skip to content

Commit

Permalink
Add search wow
Browse files Browse the repository at this point in the history
  • Loading branch information
muan committed Apr 19, 2014
1 parent fb6d453 commit 4227cb2
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
20 changes: 18 additions & 2 deletions js/pesticide.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ function createTableStructure(data) {
}

function appendData(data) {
$("#pesticide-table").data("data", data)
tbody = table.find("tbody")
tbody.html("")

Expand All @@ -40,7 +41,8 @@ function appendData(data) {
}

function sortData(key, direction) {
data = data.sort(function(a, b) {
dataToSort = $("#pesticide-table").data('data')
dataToSort = dataToSort.sort(function(a, b) {
a = a[key].length == 0 ? "0" : a[key]
b = b[key].length == 0 ? "0" : b[key]
if(parseInt(a) || parseInt(b)) {
Expand All @@ -51,7 +53,7 @@ function sortData(key, direction) {
return r ? -1 : 1
}
})
appendData(data)
appendData(dataToSort)
}

$(document).on("click", ".js-sort", function() {
Expand All @@ -61,6 +63,20 @@ $(document).on("click", ".js-sort", function() {
false
})

$(document).on("search", ".js-search", function() {
query = $(this).val()
if(query.length) {
queryData = data.filter(function(obj) {
string = Object.keys(obj).map(function(k) { return obj[k] }).join()
return string.match(new RegExp(query, "i"))
})
} else {
queryData = data
}
appendData(queryData)
false
})

function processData(data) {
data = data.map(function(obj, i) {
val = obj["施藥方法"]
Expand Down
4 changes: 2 additions & 2 deletions pesticide.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
</head>
<body>
<h1 class="title">查詢藥劑使用方式</h1>
<form id="search-table">
<input type="search" />
<form>
<input type="search" class="js-search" />
</form>
<div id="pesticide">
<table class="table" id="pesticide-table"></table>
Expand Down

0 comments on commit 4227cb2

Please sign in to comment.