您的位置:首页 > 其它

JqGrid 添加信息时某字段自动选择器

2015-04-23 15:35 260 查看
  因为项目中要用到添加信息时,要关联到某字段自动匹配,如果有数据就要显示让用户选择,所以就需要这个功能,找了很多文档,终于搞定。

在页首或css中先设置样式:

<style type="text/css">
.ui-autocomplete { font-size: 11px; position: absolute; cursor: default;z-index:5000 !important;}
</style>


在javascript中设置字段的colModel如下:

{
name:'jobStationName',
index:'job_Station_Name',
width:150,
editable: true,
edittype: "text",
editoptions: {
size:"20",
maxlength:"30",
dataInit: function (element) {
window.setTimeout(function () {
$(element).autocomplete({
id: 'AutoComplete',
source: function(request, response){
this.xhr = $.ajax({
url: '<%=request.getContextPath()%>/web/stationAutoComplate',//请求地址
data: request,
dataType: "jsonp",
success: function( data ) {
response( data );
},
error: function(model, response, options) {
response([]);
}
});
},
autoFocus: true
});
}, 100);
}
}},


java服务器端代码如下:

public String stationAutoComplate(String callback,String term){ //callback为jquery请示时生成的随机串,term为输入框内输入的字符
//处理过程就省略了,反正就是根据term去数据库查数据,然后再转换成如下的JSON格式。
return callback+"([{label:'label1',value:'value1'},{label:'label2',value:'value2'}])";
}


这是最终结果

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息