Ascending and Descending in Ionic


#1

Hi. I have a problem.
i want to ascending and descending my list by name.
for example , here is my controller :

` .controller(‘cariCtrl’,function($scope,$http,$state,Cari){
$scope.cari = function(input){
Cari.setDaerah(input.daerah);
Cari.setNama(input.nama);
Cari.setUrut(input.urut);
$state.go(‘back.list’);
}
})

.controller('listCtrl',function($scope,$http,$state,$ionicHistory,Cari){
    $scope.user = [];
    $scope.lapangan = [];
    
    $scope.goBack = function(){
        $state.go('menu.cari', {}, {
            reload: true
        });
    }
    $http({
        method: 'POST',
        data:{
            daerah: Cari.getDaerah(),
            nama: Cari.getNama(),
            urut: Cari.getUrut()
        },
        url: "http://localhost/TA2/admin/app/getSearchLapangan.php"
    }).success(function(data){
        $scope.lapangan = data;
    }).error(function(data, status,headers,config) {
        alert(status);
        alert(headers);
    }); 
})` 

in app.js, I add this :

.factory('Cari', function(){ var data = { daerah: '', nama: '', urut: '' }; return { getDaerah: function(){ return data.daerah; }, setDaerah: function(pdaerah){ data.daerah = pdaerah; }, getNama: function(){ return data.nama; }, setNama: function(pnama){ data.nama = pnama; }, getUrut: function(){ return data.nama; }, setUrut: function(purut){ data.urut = purut; } }; })

here is my view:
<label class="item item-select"> <select style="right: auto;" ng-model="input.urut"> <option value="">Silahkan Pilih Urutan</option> <option value="0">A-Z</option> <option value="1">Z-A</option> </select> </label> <button class="button button-block button-positive" ng-click="cari(input)">Cari</button>

the sql query is like this :
`$daerah = $objData->daerah;
$nama = $objData->nama;
$urut = $objData->urut;
$SQL = “”;
if ($urut == 0) {
$SQL = “SELECT L., (SELECT path FROM gambar G WHERE G.id_lapangan = L.id_lapangan LIMIT 1) as path from lapangan L WHERE daerah LIKE ‘" . $daerah . "%’ AND nama LIKE ‘" . $nama . "%’ ORDER BY nama ASC;";
} else {
$SQL = "SELECT L.
, (SELECT path FROM gambar G WHERE G.id_lapangan = L.id_lapangan LIMIT 1) as path from lapangan L WHERE daerah LIKE '” . $daerah . “%’ AND nama LIKE '” . $nama . “%’ ORDER BY nama DESC;”;
}

$result = mysql_query($SQL, $link);
$array = array();
$counter = 0;
while ($row = mysql_fetch_array($result)) {
$array[$counter] = $row;
$counter++;
}
echo json_encode($array);
`
so, when i choose the select list in “A-Z”, it means ascending.

and when i click the button, it should go to another state that show my list.

if A-Z is choosen, the "goal futsal " should be number 1.
how can i do that? what’s wrong with my code. Please help.
thank you :slight_smile:


#2

you can filter the result by name by using angularjs filter method:| orderBy:'name'" ,
example:
ng-repeat="data in results | orderBy:'name'"