`
elfasd
  • 浏览: 98391 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Jquery Select2-3.3.2 autocomplete 使用

    博客分类:
  • JS
阅读更多



 Select2-3.3.2  官网:http://ivaynberg.github.io/select2/ 

 

先上一个自己的效果图,样式啥的基本没有调,主要测试从后台获取数据:

 

 

 

一些基本的例子上面都有,但是在使用ajax从后台获取数据的时候有些问题,查了一些资料后,下面的链接给出了解决方案:http://it.toolbox.com/blogs/rymoore/select2-jquery-select-boxes-54810

 

官网上使用ajax方式调用返回的格式是 jsonp 的,jsonp什么的据说是跨域调用的,不太理解,下面这个链接详细解释了jsonp的使用及调用,没看懂:http://www.cnblogs.com/dowinning/archive/2012/04/19/json-jsonp-jquery.html

 

最终选择了返回json格式的数据,前台调用的格式为:

 

<input name="TEST_SEARCH" type="hidden" id="userSelect" style="width: 600px" /> 

 

//智能提示
$('#userSelect').select2({
            placeholder: "请输入货位码",
            minimumInputLength: 2,
            multiple:true,
            ajax: {
                url: "meselect.cmd?method=select2FromAjax",
                dataType: 'json',
                data: function (term, page) {
                    return {
                        q: term,
                        page_limit: 5,
                        area_id: "<%=area_id%>",
                        storearea_id: "<%=storearea_id%>",
                        house_id: "<%=house_id%>"
                    };
                },
                results: function (data, page) {
                    return { results: data.results };
                },
                
                formatResult: function(medata){
                }, 
   				formatSelection: resultFormatSelection,  // omitted for brevity, see the source of this page
   				dropdownCssClass: "bigdrop", // apply css that makes the dropdown taller
   				escapeMarkup: function (m) { return m; } 
            }
        });

function resultFormatResult(medata) {
        return medata.text;
    }

    function resultFormatSelection(medata) {
        return medata.text;
    }

  剩下要做的就是从后台拼接json串了,很多种方式,附件中有我的一个简单实现,当然可以使很复杂的逻辑(参见例子e9)

 使用注意事项:

1、引入的js及css文件

<link href="jsp/com/wms/component/select2-gh-pages/bootstrap/css/bootstrap.css" rel="stylesheet"/>
<link href="jsp/com/wms/component/select2-gh-pages/select2-3.3.2/select2.css" rel="stylesheet"/>
<link href="jsp/component/select2-gh-pages/bootstrap/css/bootstrap-responsive.css" rel="stylesheet"/>

<script src="jsp/com/wms/component/select2-gh-pages/js/json2.js"></script>
<script src="jsp/com/wms/component/select2-gh-pages/js/jquery-1.7.1.min.js"></script>
<script src="jsp/com/wms/component/select2-gh-pages/js/jquery-ui-1.8.20.custom.min.js"></script>
<script src="jsp/com/wms/component/select2-gh-pages/js/jquery.mousewheel.js"></script>
<script src="jsp/com/wms/component/select2-gh-pages/prettify/prettify.min.js"></script>
<script src="jsp/com/wms/component/select2-gh-pages/bootstrap/js/bootstrap.js"></script>
<script src="jsp/com/wms/component/select2-gh-pages/js/html5shim.js"></script>
<script src="jsp/com/wms/component/select2-gh-pages/select2-3.3.2/select2.js"></script>
<script src="jsp/com/wms/component/select2-gh-pages/select2-3.3.2/select2_locale_zh-CN.js"></script>
<link href="jsp/com/wms/component/select2-gh-pages/prettify/prettify.css" rel="stylesheet"/>

 2、上面引入的js中,一定要引入jquery-1.7.1.min.js,官网提供的jquery-1.8.1.min.js有些问题,会导致页面很慢

 

3、国际化,引入下面的模板即可

 <script src="jsp/com/wms/component/select2-gh-pages/select2-3.3.2/select2_locale_zh-CN.js"></script>

 

4、 多看官网的文档

 

备注:

1、附件 select2-src.zip 是server端的,参考下结构就可以了

2、附件 select2-gh-pages.zip 是web前端的组件

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  • 大小: 7.4 KB
2
4
分享到:
评论
3 楼 yy8093 2014-11-25  
elfasd 写道
yy8093 写道
如果下拉框,想限制只能输入10个,每个长度不长于20,这个我设置了,但是想让它单个长度不超过20.超过20不能输入

这个还真没有试过,我这都不超过13个。。。。

嗯,在设置值的时候给判断了下长度,不过输入的值,输入一些特殊符号又有些问题,只好也一块判断了,符合的值才让它设置,否则提示
2 楼 elfasd 2014-02-08  
yy8093 写道
如果下拉框,想限制只能输入10个,每个长度不长于20,这个我设置了,但是想让它单个长度不超过20.超过20不能输入

这个还真没有试过,我这都不超过13个。。。。
1 楼 yy8093 2014-01-25  
如果下拉框,想限制只能输入10个,每个长度不长于20,这个我设置了,但是想让它单个长度不超过20.超过20不能输入

相关推荐

Global site tag (gtag.js) - Google Analytics