引导自动完成不加载地图

引导自动完成不加载地图

本文介绍了引导自动完成不加载地图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个地图,是一个JSON的结果。我需要分配它自动完成。但是好像我必须做一个肮脏的工作。有没有清洁的方式做到这一点?

在code这是很肮脏的一部分:

  $(#autocomplete1).autocomplete({        来源:函数(请求,响应){
            VAR重= $ .ui.autocomplete.escapeRegex(request.term);
            VAR匹配=新的RegExp(N *+重+N *,我);
            VAR arrayKey = $ .MAP(v1.data,功能(的ItemKey,itemValue){
                返回的ItemKey
            });
            VAR arrayValue = $ .MAP(v1.data,功能(的ItemKey,itemValue){
                返回itemValue
            });
            VAR键= $ .grep(arrayKey,功能(项指数){
                返回matcher.test(项目);
            });
            VAR值= $ .grep(arrayValue,功能(项指数){
                返回matcher.test(项目);
            });            变种S ={;
            对于(VAR I = 0; I< key.length;我++){
                如果(ⅰ&下; value.length){
                    S + =\\+值[I] +\\:\\+键[I] +\\,;
                }
            }
            S = s.substring(0,s.length-1);
            S + =};            VAR的JSONObject = jQuery.parseJSON(S);            响应($。图(JSONObject的,功能(的ItemKey,itemValue){
                返回{
                    标签:的ItemKey,
                    值:itemValue
                };
            }));
        }    });


解决方案

简单地做到这一点:

  VAR I = 0;
    VAR代表codesMap = v1.data;
    VAR代表codesSource =新的Array();
    $。每个市(REP codesMap,功能(键,值){
        代表codesSource [我++] = {标签:值,值:关键};
    });    $(#autocomplete1).autocomplete({
        来源:代表codesSource
    });

在这里更新了code:

I have a map which is a JSON result. I need to assign it to autocomplete. But it seems like I have to do a dirty job. Is there any cleaner way to do that?

http://jsfiddle.net/7dLRh/

part of the code which is very dirty:

$( "#autocomplete1" ).autocomplete({

        source: function(request, response) {
            var re = $.ui.autocomplete.escapeRegex(request.term);
            var matcher = new RegExp( "n*" + re + "n*", "i");
            var arrayKey = $.map(v1.data, function (itemKey, itemValue) {
                return itemKey
            });
            var arrayValue = $.map(v1.data, function (itemKey, itemValue) {
                return itemValue
            });
            var key = $.grep( arrayKey, function(item,index){
                return matcher.test(item);
            });
            var value = $.grep( arrayValue, function(item,index){
                return matcher.test(item);
            });

            var s = "{ ";
            for (var i =0; i< key.length; i++) {
                if (i < value.length) {
                    s+= "\"" + value[i] + "\":\"" + key[i] + "\",";
                }
            }
            s = s.substring(0, s.length-1);
            s += "}";

            var jsonObject = jQuery.parseJSON(s);

            response($.map(jsonObject, function (itemKey, itemValue) {
                return {
                    label: itemKey,
                    value: itemValue
                };
            }));
        }



    });
解决方案

simply do this:

    var i = 0;
    var repCodesMap = v1.data;
    var repCodesSource = new Array();
    $.each(repCodesMap, function(key, value) {
        repCodesSource[i++] = { label : value, value : key };
    });

    $( "#autocomplete1" ).autocomplete({
        source : repCodesSource
    });

updated your code here:http://jsfiddle.net/7dLRh/2/

这篇关于引导自动完成不加载地图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 07:03