【问题标题】:AngularJS - Filter with MysqlAngularJS - 使用 Mysql 过滤
【发布时间】:2013-01-03 15:58:21
【问题描述】:

我尝试根据邮政编码在静态(非谷歌)地图上设置标记。
这是我的 html 代码:

<div ng-controller="DealerMarkerListCtrl">
    <a href="#/dealer/{{marker.id}}" class="marker" style="left:{{marker.left}}px;top:{{marker.top}}px" ng-repeat="marker in dealer|zipFilter:countryLookup"></a>
</div>  

现在我尝试显示按邮政编码和国家/地区过滤的所有标记,这是下拉列表:

<input type="text" name="plz" placeholder="PLZ (z.B 80469)" class="plz" ng-model="zipCodeLookup">
<select name="drop" tabindex="1" class="drop" id="dropcountry" ng-model="countryLookup" ng-options='option.value as option.name for option in typeOptions'>
</select>

我得到了以下过滤器:

app.filter("zipFilter", function() { return function(markers, country, zip) {
    var retMarkers = [];
    for(var i = 0, len = markers.length; i < len; ++i) {
        var singleMarker = markers[i];
        if(singleMarker.land == country) {
            retMarkers.push(singleMarker);
        }
    }
    return retMarkers;
}});

主要问题是:
我得到了一个外接圆搜索,我得到了用户邮政编码周围 20(公里/英里)内的所有邮政编码。这工作正常。当我直接访问框架(不在控制器内)时,我得到一个这样的 json:

{"plz":["44787","44789","44791","44793","44795","44797","44799","44801","44803","44805","44807","44809","44866","44867","44869","44879","44892","44894","45525","45527","45529","45549"]}  

对于邮政编码“45525”。

现在我需要显示此邮政编码数组中的所有标记(经销商)。
需要将过滤器中的 zip 参数与 country 参数一起发送到“区域搜索”功能“files/framework/umkreis/:zip/:country”以获取该区域的 zip json。
之后我需要检查一些标记是否在这个 json 中并过滤它们。

我只是不知道如何将这些东西构建到我的过滤器中。你能帮我解决这个问题吗?非常感谢。

其他有用信息:
路线与服务:

app.factory('dealerService', ['$resource', '$http', '$rootScope', function($resource, $http, $rootScope){
    return {
    //the resource provider interacting with the PHP backend
        api: 
        $resource('files/framework/dealer/:id', {}, {
            update: {method:'PUT'}
        }),
    }
}])

app.config(['$routeProvider', function($routeProvider) {
  $routeProvider.
      when('/dealer/:id', {templateUrl: 'files/tpl/dealer-details.html', controller: 'DealerDetailsCtrl'}).
      otherwise({redirectTo: '/dealer'});
}]); 

“Slim”框架获取区域邮政编码的途径是:

files/framework/umkreis/45525/DE
or
files/framework/umkreis/:zip/:country

【问题讨论】:

  • 我现在创建了一个自定义 php 函数,它以 userZip 和 country 作为参数。它使给定拉链周围的区域大约 20 公里。之后我检查经销商的邮政编码是否在区域邮政编码的数组中。输出(现在)是过滤后的经销商邮政编码。方法如下:pastebin.com/pMf33c9j - 如何使用此功能设置标记?我无法过滤,这是肯定的……对吧?

标签: javascript filter resources angularjs


【解决方案1】:

您似乎正在尝试根据表单中的两个字段更新地图上的标记:“zipCodeLookup”和“countryLookup”。

<input ... ng-model="zipCodeLookup" ... />
<select ... ng-model="countryLookup" ... ></select>

标记显示在以下控制器中:

<div ng-controller="DealerMarkerListCtrl"></div>  

您正在使用此路由从您的 PHP 服务器收集 JSON

files/framework/umkreis/:zip/:country

您需要做的是在“DealerMarkerListCtrl”控制器中指定一个函数,该函数将获取邮政编码和国家/地区,并使用来自服务器的新 JSON 更新经销商。

$scope.updateDealer = function(zip, country) { 
    $http.get('files/framework/umkreis/'+ zip + '/' + country).
    success(function(data) { 
        $scope.dealer = data; 
    }); 
}

完成此操作后,您可以将ng-submit 添加到包含将调用此函数的输入的&lt;form&gt;

<form ng-submit="updateDealer(zipCodeLookup, countryLookup)"> ... </form>

确保&lt;form&gt; 位于正确的控制器内部,以查看新创建的函数。每当用户提交此表单时,它都会调用您的 PHP 服务器,获取 JSON,并更新经销商数组。

【讨论】:

    猜你喜欢
    • 2017-07-22
    • 2023-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多