I'm trying to build a simple web app that displays tables of items grouped by category. I want the categories to be collapsible, the tables to be sortable, and I want the data to update every 60 seconds without reloading the whole page (the data comes from my_json_data.php which queries a DB and outputs the results as json). It seems like Angular.js is the preferred way to do this (particularly the last part). I'm totally new to this but I've got the basic functionality working, but I'm running into an issue. When the table data refreshes it resets the collapse and sorting, which I don't want to happen. Here's the basic outline of everything:
index.html:
<body ng-app="myApp">
<div ng-controller="myController">
<div ng-repeat="category in categories" class="panel panel-primary">
<!-- Collapsible category panel -->
<div class="panel-heading">
<h5 class="panel-title" ng-click="show = !show">
{{ category.name }}
</h5>
</div>
<!-- Sortable table of items -->
<div class="table-responsive" ng-hide="show">
<table class="table table-condensed table-striped">
<thead>
<th ng-click="sortType = 'name'; sortReverse = !sortReverse">Name</th>
<th ng-click="sortType = 'size'; sortReverse = !sortReverse">Size</th>
<th ng-click="sortType = 'price'; sortReverse = !sortReverse">Price</th>
</thead>
<tbody>
<tr ng-repeat="item in category.items | orderBy:sortType:sortReverse">
<td>{{ item.name }}</td>
<td>{{ item.size }}</td>
<td>{{ item.price }}</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</body>
Factory:
app.factory('myFactory', ['$http', function($http) {
var myFactory = {};
myFactory.getData = function () {
return $http.get('my_json_data.php');
};
return myFactory;
}]);
Controller:
app.controller('myController', ['$scope', '$interval', 'myFactory', function ($scope, $interval, myFactory) {
$scope.categories;
getData();
function getData() {
myFactory.getData()
.success(function (data) {
$scope.categories = data;
})
.error(function (error) {
$scope.status = 'Unable to load data: ' + error.message;
});
$interval(getData, 60000);
};
$scope.sortType = 'price'; // set the default sort type
$scope.sortReverse = true; // set the default sort order
}]);
So everything works exactly how I'd like it to, except every 60 seconds when the data is refreshed, it reopens any category divs that have been closed, and it re-sorts the tables to the default order. Hopefully there's a relatively simple fix, but I'm open to rebuilding everything from the ground up if I've made some glaring rookie mistakes. Thanks!