I have a table which I want to re-sort by picking a tab or menu item.
Context: The table is populated with all "orders" in the SQL DB, and by clicking a tab for a specific area it should update the table with orders only in that area.
Can I make an IF statement somehow, or a while-loop in angular?
Currently it gets its data like this in php (where I already try to filter);
<?php
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
echo json_encode($data);
// creates a connection to the mysql server
require('connection.php');
$filterGeo = $filterSta = $filterNH = $filterNot = '%';
if (isset($_GET['filterGeo'])) {$filterGeo = $_GET['filterGeo'];};
if (isset($_GET['filterSta'])) {$filterSta = $_GET['filterSta'];};
if (isset($_GET['filterNH'])) {$filterNH = $_GET['filterNH'];};
if (isset($_GET['filterNot'])) {$filterNot = $_GET['filterNot'];};
$result = $conn->query("SELECT * FROM orders
Where address LIKE '%$filterGeo%' AND status LIKE '%$filterSta%'");
$outp = "";
while($rs = $result->fetch_array(MYSQLI_ASSOC)) {
if ($outp != "") {$outp .= ",";}
$outp .= '{"orderID":"' . $rs["orderID"] . '",';
$outp .= '"customerID":"' . $rs["customerID"] . '",';
$outp .= '"date":"' . $rs["date"] . '",';
$outp .= '"address":"'. $rs["address"] . '",';
$outp .= '"area":"'. $rs["area"] . '"}';
}
$outp ='{"orders":['.$outp.']}';
$outp = utf8_encode($outp);
$conn->close();
echo($outp);
//?>
And the JSON is handled by angular like this;
var app = angular.module('Orders', []); app.controller('getOrders', function($scope, $http) {
$http.get("/filterOrders.php")
.then(function (response) {$scope.orders = response.data.orders;}); });
The relevant HTML snippet:
<div ng-app="Orders" ng-controller="getOrders">
<ul class="nav nav-tabs">
<li role="presentation" class="active"><a href="#">Alle</a></li>
<li role="presentation"><a href="#">Odder</a></li>
<li role="presentation"><a href="#">Hørning</a></li>
<li role="presentation"><a href="#">Ry</a></li>
<li role="presentation"><a href="#">Skanderborg</a></li>
</ul>
<table class="table table-condensed table-border table-striped">
<thead align="center">
<th>Order #</th>
<th>Customer #</th>
<th>Address</th>
<th>Area</th>
<th>Date</th>
<th>OrderType</th>
<th>TrashcanType</th>
<th>Status</th>
<th>RenoNord Invoice</th>
</thead>
<tbody>
<tr ng-repeat="x in orders">
<td>{{ x.orderID }}</td>
<td>{{ x.customerID }}</td>
<td>{{ x.address }}</td>
<td>{{ x.area }}</td>
<td>{{ x.date }}</td>
<td>{{ x.orderType }}</td>
<td>{{ x.trashcanType }}</td>
<td>{{ x.status }}</td>
<td>{{ x.renonord }}</td>
<td class="text-right">
<button href="#" data-id="{{ x.orderID }}" class="btn btn-xs btn-default">Ret</button>
<button href="#" data-id="{{ x.orderID }}" class="btn btn-xs btn-danger">Slet</button>
</td>
you could use angular filters:
<div ng-repeat="x in orders | filter:{area:'MyArea'}">
...
It's quite easy to filter a table in angular. On your tabs, have it set a variable when it's clicked:
<li role="presentation"><a data-ng-click="areaFilter = 'Odder'">Odder</a></li>
Then use the filter in your ng-repeat:
<tr ng-repeat="x in orders | filter: {area: areaFilter}">
Just make sure that you set areaFilter = ''
in your controller, and your All tab should use that same assignment.
Depending on where you want to filter the result set (client or server side), you got a couple of options:
Filtering client side:
Add ng-click
directive to your options. With that you can set a scope variable which you would use to filter by.
<li role="presentation"><a ng-click="areaFilter = 'Odder'">Odder</a></li>
Filtering the results with filter
in ng-repeat:
<tr ng-repeat="x in orders | filter: {area: areaFilter}">
Filtering server side:
In this case you will have to make a new request to the server every time you apply a new filter. The semantics are the same, but in this case you'll call a function on ng-click
instead of assigning a string to a variable.
<li role="presentation"><a ng-click="filter('Odder')">Odder</a></li>
Controller:
$scope.filter = function(val) {
// create the url you want to call depending on "val"
var url = '/filterOrders.php';
switch (val) {
case 'Odder':
url += '?filterXY=Odder';
break;
default: break;
}
$http.get(url)
.then(function (response) {
$scope.orders = response.data.orders;
});
});
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.