2016-08-18 87 views
1

我正在用angular和php構建一個項目。我有一個「選擇」選項,我選擇具體的日期,並在表格中顯示我所有的細節,並指望幾件事情,一切都很好。但是當我選擇一個日期時,我希望它顯示特定月份的所有結果,而不是特定日期(例如 - >現在我選擇這樣的日期 - '2016-4-15'它會給我所有特定的信息這個日期,而不是像我需要的月份)。有人可以幫忙嗎?在我的數據庫中,'日期'值是日期。如何過濾角度的日期?

腓查詢:

$query=" SELECT `description` ,`total_price` , `name`,`supplier_id`,`date` FROM `suppliers`,`expenses` 
    where `supplier_id` = `refer_supplier_id` "; 

HTML:

<select ng-model="supplierExpenses.selectedOption" ng-change="setTotals(supplierExpenses)" 
     ng-options = "item.date for item in supplierExpenses | 
     unique:'date'" > 

     <option value="">בחר תאריך</option> 
     </select> 



<div class="table-responsive"> 
<table class="customer-list table table-striped" > 
      <thead> 
       <tr > 

        <th class="Column-Header">מספר ספק</th> 
        <th class="Column-Header">שם ספק</th> 
        <th class="Column-Header">כמות מוצרים שנקנו</th> 
        <th class="Column-Header">מחיר הוצאה</th> 

       </tr> 
      </thead> 
      <tbody> 
       <tr ng-repeat="item in supplierExpenses" ng-if = "item.date == supplierExpenses.selectedOption.date" 
        > 

        <td>{{item.supplier_id}}</td> 
        <td>{{item.name}}</td> 
        <td>{{item.description}}</td> 
        <td>{{item.total_price}}</td> 

       </tr> 
      </tbody> 
      <tfoot> 

       <tr class="bg-warning"> 
        <td><font size="6">סה"כ הוצאות</font></td> 
        <td><font size="6">{{totalExpenses}}</font></td> 
        <td></td> 
       </tr> 
      </tfoot> 

     </table> 


</div> 

控制器:

"use strict"; 
angular.module('dataSystem').controller('supplierExpensesCtrl', function ($scope, $route, $location, $http) { 
    $http({method:'GET', url:'api/reports-tab/supplier-expenses-by-mounth.php/'}) 
     .then(function(response) { 
     var arr = JSON.parse(JSON.parse(response.data)); 
     $scope.supplierExpenses = arr; 
     }) 

     // This will log you the error code and trace, if there is an error. 
     .catch(function(err) { 
     console.log('err', err) 
     }); 

     $scope.totalExpenses = 0; 
     $scope.setTotals = function(totalItem){ 
     $scope.totalExpenses = 0; 
     for(var item =0; item< totalItem.length; item++){ 
      // console.log(totalItem[item]); 
      if (totalItem[item] && (totalItem[item].date == $scope.supplierExpenses.selectedOption.date)){ 

      $scope.totalExpenses += parseInt(totalItem[item].total_price); 
      } 
     } 
    } 
}); 
+0

你想要一個'GROUP BY month'或'WHERE row.date = @ month'嗎?向我們展示樣本數據的當前和預期結果。請閱讀[** How-to-Ask **](http://stackoverflow.com/help/how-to-ask) \t \t這裏是[** START **](http ://spaghettidba.com/2015/04/24/how-to-post-at-sql-question-on-a-public-forum/),瞭解如何提高您的問題質量並獲得更好的答案。 –

+0

@JuanCarlosOropeza感謝您的提示。我想選擇一個月,並從該特定月份獲取所有數據 – tanyaa

+0

可能的重複[如何在AngularJs中按月篩選](http://stackoverflow.com/questions/29241696/how-to-filter-by -month-in-angularjs-with-filter) –

回答

1

喜高興你同生共成功^^我對工作的解決方案編輯我的答案日期問題

$http({method:'GET', url:'api/reports-tab/supplier-expenses-by-mounth.php/'}) 
     .then(function(response) { 
      var arr = JSON.parse(JSON.parse(response.data)), month, date; 
      for(var i = 0; i< arr.length; i++){ 
       date = new Date(arr[i].date); //we convert the string into javascript date 
       month = date.getMonth()+1; 
       if(month.length === 1){ 
        month = '0'+month; //we add a 0 if needed 
       } 
       var year = date.getFullYear(); 
       arr[i].date = month+'/'+year; //we use only the month and year 
      } 
      $scope.supplierExpenses = arr; 

     }) 
+0

哎!謝謝我現在就試試 – tanyaa

+0

它不起作用:(桌子上顯示沒有我選擇一個日期 – tanyaa

+0

你可以添加formatedDate()函數看看有什麼可能是錯誤的嗎 – DMCISSOKHO