Problem filtering counties by the district selected with ng-change in Angularjs?

Asked

Viewed 104 times

1

I have a mobile app where I have two select option (districts and counties). What I intend is that when choosing a district appear only the counties that belong to that district.

How can I do that? I’ve tried filtering through ng-change only that it is not appearing the name of the councils appears the blank list and by the console I can verify that filters.

View

<div ng-controller="FiltraEstabelecimentos">
        <form ng-submit>
            <div class="row">
                <div class="col">
                    <label ng-controller="ListaDistritos" style="border-radius: 10px; margin: 0px 0px 10px 0px;" class="item item-input item-select">
                        <div class="input-label">
                            Distrito
                        </div>
                        <select ng-controller="ListaConcelhos" ng-model="distrito" ng-options="lista_distritos as lista_distritos.titulo for lista_distritos in distritos" ng-change="id_distrito()"></select>
                    </label>
                    <label ng-controller="ListaConcelhos" style="border-radius: 10px;" class="item item-input item-select">
                        <div class="input-label">
                            Concelho
                        </div>
                        <select ng-model="concelho" ng-options="lista_concelhos as lista_concelhos.titulo for lista_concelhos in concelhos"></select>
                    </label>
                </div>
            </div>
            <div style="margin:0px 10px 0px 10px;">
                <button type="submit" ng-click="filtra_estabelecimentos(input)" style="background-color: #CA5B60; border:#CA5B60; border-radius: 10px;" class="button button-block button-positive">
                    <i class="ion-search"></i> Pesquisar
                </button>
            </div>
        </form>  
    </div> 

PHP districts

function converte($term, $tp) {
    if ($tp == "1") $palavra = strtr(ucwords(strtoupper($term)),"àáâãäåæçèéêëìíîïðñòóôõö÷øùüúþÿ","ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖ×ØÙÜÚÞß");
    elseif ($tp == "0") $palavra = strtr(ucwords(strtolower($term)),"ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖ×ØÙÜÚÞß","àáâãäåæçèéêëìíîïðñòóôõö÷øùüúþÿ");
    return $palavra;
}

$distritos = array();

$sql = $conexao->prepare("SELECT * FROM distritos ORDER BY titulo");
$sql->execute();
$distritos = $sql->fetchAll(PDO::FETCH_ASSOC);

foreach ($distritos as $rows ) {
    $distritos[] = array(
        'titulo'        => converte($rows['titulo'], 0),
    );
}
echo json_encode($distritos);

PHP Concelhos

 function converte($term, $tp) {
    if ($tp == "1") $palavra = strtr(ucwords(strtoupper($term)),"àáâãäåæçèéêëìíîïðñòóôõö÷øùüúþÿ","ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖ×ØÙÜÚÞß");
    elseif ($tp == "0") $palavra = strtr(ucwords(strtolower($term)),"ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖ×ØÙÜÚÞß","àáâãäåæçèéêëìíîïðñòóôõö÷øùüúþÿ");
    return $palavra;
}

$concelhos = array();

$sql = $conexao->prepare("SELECT * FROM concelhos WHERE id_mae = :distritos ORDER BY titulo");
$sql->bindParam(':distritos', $_GET['id_distrito'], PDO::PARAM_INT); 
$sql->execute();
$row = $sql->fetchAll(PDO::FETCH_ASSOC);

foreach ($row as $rows ) {
    $concelhos[] = array(
        'titulo'        => converte($rows['titulo'], 0),
    );
}
echo json_encode($concelhos);

County and District Controller

.controller('ListaDistritos', function($scope, $http) {
    $http.get("https://www.sabeonde.pt/api/api_distritos.php").success(function (data) {
        $scope.distritos = data;
    });
})
.controller('ListaConcelhos', function($scope, $http, $stateParams) {
    $scope.id_distrito= function (){
        $http.get("https://www.sabeonde.pt/api/api_concelhos.php?id_distrito="+$scope.distrito.id).success(function (data) {
            $scope.concelhos = push(data);
        });
    };
})
  • 1

    Already tried to consult the councils in the change of the district combobox?

  • Is not missing the where here SELECT * FROM concelhos ORDER BY titulo, that would be it?

  • No, and I don’t know how I can do if I publish an answer I’d appreciate how I could do ?

  • Is not for two comments or just mine? xD

  • and for Emir Marques

  • Emir Marques I made a change with ng-change I’ve already put the code above so don’t stop the councils when I select a district must be missing something

  • See if it helps, http://stackoverflow.com/a/15688483/1342547

  • I tested but it didn’t work still the same

Show 3 more comments

1 answer

1

To list the counties according to the district selected, it is necessary to add a WHERE to specify this.

$sql = $conexao->prepare("SELECT * FROM concelhos 
                          WHERE id_distrito = :distritos ORDER BY titulo");

$sql->bindParam(':distritos', $distritos, PDO::PARAM_INT); ; 
  • Yes and now how do I get the selected district id ?

  • @Césarsousa, before the consultation make a print_r($_GET) will show the names of the fields sent, put here in the comments.

  • Shows nothing

  • @Césarsousa print_r($_POST); shows something? vc is seeing in the browser console?

  • Give it to the console Error: JSON.parse: unexpected character at line 1 column 1 of the JSON data

  • But I do not press any button to filter I want that when selecting the district in the counties appear only the counties of that district

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.