Skip to content

Commit bfd9f29

Browse files
committed
typeahead自动提示功能
1 parent ccbf8ad commit bfd9f29

File tree

4 files changed

+88
-0
lines changed

4 files changed

+88
-0
lines changed

README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,34 @@ vuejs 增删改查的demo,自动建表 <br>
3939
4040
gons json转对象
4141
42+
typeahead:输入框文本自动提示功能, 返回值为json , jsonp都可以
43+
依赖的js为:http://www.guriddo.net/demo/js/bootstrap3-typeahead.js
44+
核心代码如下:
45+
```javascript
46+
$(".typeahead").typeahead({
47+
source: function(query, proxy) {
48+
$.ajax({
49+
url: '/autoJson?callback=?',
50+
dataType: "jsonp",
51+
data: {term: query},
52+
53+
/***完整的写法, 指定json中的某个字段(属性)***/
54+
//方法一:
55+
success : function (data){
56+
var arr = [];
57+
for (i in data)
58+
{
59+
arr.push(data[i]['name']);
60+
}
61+
proxy(arr);
62+
},
63+
//方法二:
64+
success : proxy //简写
65+
});
66+
}
67+
});
68+
```
69+
4270
Excel导出 demo
4371
4472

src/main/java/com/example/demo/controller/AutoCompleteController.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import java.util.List;
77
import java.util.Map;
88

9+
import org.apache.commons.lang.StringUtils;
910
import org.slf4j.Logger;
1011
import org.slf4j.LoggerFactory;
1112
import org.springframework.beans.factory.annotation.Autowired;
@@ -95,6 +96,25 @@ public JSONPObject autoScriptUpdate(@RequestParam("term") String query,
9596
return new JSONPObject(callback, list);
9697
}
9798

99+
@ResponseBody
100+
@RequestMapping(value = "/autoTypeahead", method = RequestMethod.GET)
101+
public List<Order> autoTypeahead(@RequestParam("term") String query){
102+
103+
List<Order> orders = orderRepository.findAll();
104+
105+
if(StringUtils.isNotBlank(query)){
106+
List<Order> list = new ArrayList<>();
107+
for(Order order : orders){
108+
if(order.getProduceName().contains(query)){
109+
list.add(order);
110+
}
111+
}
112+
return list;
113+
}
114+
return orders;
115+
116+
}
117+
98118

99119
@ResponseBody
100120
@RequestMapping("/amountSelect")
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.example.demo.test;
2+
3+
public class TestMap {
4+
public static void main(String[] args) {
5+
int hash = TestMap.hash("java");
6+
System.out.println(hash);
7+
}
8+
static final int hash(Object key) {
9+
int h;
10+
return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);
11+
}
12+
13+
}

src/main/resources/static/user/jqgridDemo.html

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,38 @@
3939
<div style="margin-left:20px">
4040
<table id="jqGrid"></table>
4141
<div id="jqGridPager"></div>
42+
43+
<div id="remote">
44+
<input class="typeahead" type="text" placeholder="输入文字,自动提示.....">
45+
</div>
4246

4347
</div>
4448
<script type="text/javascript">
4549

4650
$(document).ready(function () {
51+
52+
$(".typeahead").typeahead({
53+
source: function(query, proxy) {
54+
$.ajax({
55+
url: '/autoTypeahead',
56+
dataType: "json",
57+
data: {term: query},
58+
59+
/***完整的写法, 指定json中的某个字段(属性)***/
60+
//方法一:
61+
success : function (data){
62+
var arr = [];
63+
for (i in data){
64+
console.dir(data[i])
65+
arr.push(data[i]['produceName']+" "+data[i]['orderId']);
66+
}
67+
proxy(arr);
68+
}
69+
//方法二:
70+
//success : proxy //简写
71+
});
72+
}
73+
});
4774

4875

4976
$("#jqGrid").jqGrid({

0 commit comments

Comments
 (0)