Changed month selection behaviour.

This commit is contained in:
Alexis Lahouze 2013-01-08 18:50:47 +01:00
parent 7d54015970
commit 5ccddb0511
3 changed files with 33 additions and 43 deletions

View File

@ -129,21 +129,17 @@ class EntryAPI extends RestAPI {
protected function get_months() { protected function get_months() {
$year=$this->_request['year']; $account=$this->_request['account'];
$connection=$this->get_db_connection(); $connection=$this->get_db_connection();
$statement=$connection->prepare("select distinct extract(month from value_date) as month from entry where value_date between date (:year || '-01-01') and date(:year || '-01-01') + interval '1 year' - interval '1 day' order by month desc"); $statement=$connection->prepare("select distinct extract(year from value_date) as year, extract(month from value_date) as month from entry where account_id = :account order by year, month");
$statement->bindParam("year", $year); $statement->bindParam("account", $account);
$return=$statement->execute(); $return=$statement->execute();
if($return) { if($return) {
$result=[]; $this->response($statement->fetchAll(PDO::FETCH_ASSOC));
foreach($statement->fetchAll(PDO::FETCH_NUM) as $value) {
array_push($result, $value[0]);
}
$this->response($result);
} else { } else {
$this->response($statement->errorInfo()[2], 500); $this->response($statement->errorInfo()[2], 500);
} }

View File

@ -24,7 +24,10 @@
<p><a class="btn btn-primary" data-bind="click: $root.add" href="#" title="Add entry"><i class="icon-plus"></i> Add entry</a></p> <p><a class="btn btn-primary" data-bind="click: $root.add" href="#" title="Add entry"><i class="icon-plus"></i> Add entry</a></p>
<p><select class="span1" data-bind="options: years, value: year"></select><select class="span1" data-bind="options: months, value: month"></select></p> <div class="tabbable">
<ul data-bind="foreach: months()" class="nav nav-tabs">
<li data-bind="css: {'active': $data == $root.month()}"><a href="#" data-bind="click: $parent.changeMonth"><span data-bind="text: $data.year"></span>-<span data-bind="text: $data.month"></span></a></li>
</ul>
<table class="table table-striped table-condensed table-hover"> <table class="table table-striped table-condensed table-hover">
<thead> <thead>
@ -34,7 +37,7 @@
<th>Label</th> <th>Label</th>
<th style="width: 50px">Value</th> <th style="width: 50px">Value</th>
<th style="width: 50px">Sold</th> <th style="width: 50px">Sold</th>
<th style="width: 50px">Pointed sold</th> <th style="width: 50px">P. sold</th>
<th style="width: 60px">Actions</th> <th style="width: 60px">Actions</th>
</tr> </tr>
</thead> </thead>
@ -45,6 +48,7 @@
</div> </div>
</div> </div>
</div> </div>
</div>
<script id="itemsTmpl" type="text/html"> <script id="itemsTmpl" type="text/html">
<tr data-bind="css: { 'error': sold() < 0 }"> <tr data-bind="css: { 'error': sold() < 0 }">

View File

@ -32,8 +32,6 @@ var ListViewModel = function() {
self.entries = ko.observableArray([]); self.entries = ko.observableArray([]);
self.years = ko.observableArray();
self.year = ko.observable();
self.months = ko.observableArray(); self.months = ko.observableArray();
self.month = ko.observable(); self.month = ko.observable();
@ -45,7 +43,7 @@ var ListViewModel = function() {
self.savedItem = ko.observable(); self.savedItem = ko.observable();
self.loadEntries = function() { self.loadEntries = function() {
$.post("api/entry.php", {action: "get_entries", account: self.account().id, year: self.year(), month:self.month()}, function(data) { $.post("api/entry.php", {action: "get_entries", account: self.account().id, year: self.month().year, month:self.month().month}, function(data) {
self.entries.removeAll(); self.entries.removeAll();
self.selectedItem(null); self.selectedItem(null);
@ -165,6 +163,10 @@ var ListViewModel = function() {
} }
}; };
self.changeMonth = function(month) {
self.month(month);
};
self.loadAccounts = function() { self.loadAccounts = function() {
$.post("api/entry.php", {action: "get_accounts"}).success(function (result) { $.post("api/entry.php", {action: "get_accounts"}).success(function (result) {
self.accounts(result); self.accounts(result);
@ -175,29 +177,17 @@ var ListViewModel = function() {
}); });
}; };
self.loadYears = function(account) { self.loadMonths = function(account){
$.post("api/entry.php", {action: "get_years", account: account.id}).success(function (result) { $.post("api/entry.php", {action: "get_months", account: account.id}).success(function (result) {
self.years(result);
if(! self.year()){
self.year(result[0]);
}
});
};
self.loadMonths = function(year){
$.post("api/entry.php", {action: "get_months", year: year}).success(function (result) {
self.months(result); self.months(result);
if(! self.month()) { if(! self.month()) {
self.month(result[0]); self.month(result[result.length - 1]);
} }
}); });
}; };
self.account.subscribe(self.loadYears); self.account.subscribe(self.loadMonths);
self.year.subscribe(self.loadMonths);
self.month.subscribe(self.loadEntries); self.month.subscribe(self.loadEntries);