2014-04-04 90 views
0

獲取服務器數據之後創建我有DOM沒有得到在angularjs

我無法從服務器獲取數據後,重新創建表(我用DWR來從數據在前端顯示數據的問題服務器)

在點擊表我打電話sort()sort()的頭部有以getObj()函數的調用,這使得DWR調用來獲取數據,

當我點擊搜索按鈕,我得到的選擇的值選項和輸入字段值和通行證它通過getObj ..... DWR調用.....這裏是我的問題,當我點擊表頭時,獲取的數據得到顯示,但是當我點擊搜索按鈕它沒有。

這是我的控制器和功能 -

var searchApp = angular.module('searchApp', []), 
    selectData = []; 

var inputValue='Yahoo', 
    filterValue='0' 
searchApp.controller('searchCtrl', function ($scope) { 
    $scope.results = []; 
    $scope.filterOption=0; 
    $scope.filters = [ 
     {id:1,value:'Campaign'}, 
     {id:2,value:'Account'}, 
     {id:3,value:'Publisher Url'}, 
     {id:4,value:'Email Address'} 
    ]; 

    $scope.obj = []; 
    $scope.loaded = false; 
    $scope.isSorting = false; 
    $scope.inputData='yahoo'; 

    // Default sort order 
    $scope.sortValue = 'categoryId'; 
    $scope.isSortAsc = false; 
    $scope.headList = [ 
     { 
      name: '', 
      value: '', 
      css: '' 
     }, 
     { 
      name: 'Name', 
      value: 'value', 
      css: 'col-sort-asc' 
     }, 
     { 
      name: 'Type', 
      value: 'categoryId', 
      css: 'col-sort' 
     } 

    ]   


    // build array 
    /*angular.forEach(resultsObj, function (obj, key) { 
     /*$scope.filters.push({ 
      value: key, 
      name: obj.label 
     }); 
     for (var i = 0; i < obj.item.length; i++) { 
      $scope.results.push({ 
       label: obj.label, 
       name: obj.item[i].name 
      }); 
     } 


    });*/ 

    $scope.getObj = function() { 
     // Add the dialog loader 
     dialog.load.init(); 
     dialog.load.add('search'); 
     // Make the DWR call 
     console.log($scope.isSortAsc,$scope.sortValue,inputValue,filterValue); 
     BimDWRLogic.getSearchByCateogry($scope.isSortAsc,$scope.sortValue,inputValue,filterValue, function(obj) { 
      $scope.$apply(function() { 
       $scope.setObj(obj); 
       // Complete the dialog loader 
       dialog.load.complete('search'); 
      }); 
     }); 
    }; 

    // ================================================== 
    // Set Object 
    // ================================================== 
    $scope.setObj = function(obj) { 
     $scope.obj = obj; 
     console.log('Length is '+$scope.obj.list.length); 
     for(i=0;i<$scope.obj.list.length;i++) 
     { 
      console.log($scope.obj.list[i].categoryId); 
      console.log($scope.obj.list[i].value); 
     } 
     //dialog.load.init(); 
     //dialog.load.add('performance-summary-chart'); 
     // Load the chart 
     // Sometimes the div is not there 
     // TODO: might be a better way of handling this 
     // Don't load the chart if we are sorting 
     /*if($scope.isSorting !== true){ 
      window.setTimeout(function(){ 
       $scope.loadChart(); 
      }, 500); 
     }*/ 
     $scope.isSorting = false; 
     $scope.loaded = true; 

    }; 

    $scope.search=function(){ 
     inputValue=$scope.inputData; 
     console.log('Input value is '+inputValue); 

     $scope.getObj(); 


    } 

    $scope.filterCriteria=function(opt){ 
     $scope.filterOption=opt; 
     filterValue=opt; 
     console.log('Selected Criteria is '+filterValue); 
    }  

    $scope.sort = function(value){ 

     // Return if the column object is not sortable 
     if(value === ''){ 
      return false; 
     } 

     // Sets the Data Value 
     if(value === $scope.sortValue){ 
      if($scope.isSortAsc === false){ 
       $scope.isSortAsc = true; 
      }else{ 
       $scope.isSortAsc = false; 
      } 
     }else{ 
      $scope.sortValue = value; 
      $scope.isSortAsc = false; 
     } 

     // Sets the Style 
     // Note: we don't loop through the first row 
     for(var i=1; i<$scope.headList.length; i++){ 
      if(value === $scope.headList[i].value){ 
       if($scope.isSortAsc === true){ 
        $scope.headList[i].css = 'col-sort-asc'; 
       }else{ 
        $scope.headList[i].css = 'col-sort-desc'; 
       } 
      }else{ 
       $scope.headList[i].css = 'col-sort'; 
      } 
     } 
     console.log('Inside Sort '+$scope.isSortAsc+' '+$scope.sortValue+' '+inputValue+' '+filterValue); 
     // Get the object 
     $scope.isSorting = true; 
     $scope.getObj(); 

    }; 


}); 

下面是HTML頁面模板,我包括一個jsp頁面

HTML頁面 -

<table class="table table-index search-results"> 
      <thead> 
       <tr class="info"> 
        <th colspan="3"> 
         <div>Filter By: 
          <select ng-model="searchData" ng-options="f.id as f.value for f in filters" class="search-filter" ng-change="filterCriteria(searchData)"> 
           <option>--none--</option> 
          </select> 
         </div> 
        </th> 
       </tr> 
       <tr> 
        <th ng-repeat="th in headList" class="{{th.css}}" ng-click="sort(th.value)">{{th.name}}</th> 
       </tr> 
      </thead> 
      <tfoot> 
       <tr> 
        <td colspan="3">&nbsp;</td> 
       </tr> 
      </tfoot> 
      <tbody> 
       <tr ng-repeat="resultObj in obj.list"> 
        <td>{{$index + 1}}.</td> 
        <td>{{resultObj.categoryId}}</td> 
        <td>{{resultObj.value}}</td> 
       </tr> 
      </tbody> 
     </table> 

and jsp page is 

<div class="container-fluid" ng-app="searchApp"> 
     <h1>Search Results</h1> 
     <section class="group" ng-controller="searchCtrl"> 
      <input type="text" class="search-input" ng-model="inputData"/><div id="button1" class="btn btn-lg btn-search" ng-click="search()">Search</div> 
      <div>74 results</div> 
     </section> 
     <section> 
      <!-- Search Results --> 
      <div class="row"> 
       <div class="col-12" id="searchCtrl" ng-controller="searchCtrl" ng-include="'views/search.htm'"></div> 
      </div> 
     </section> 
    </div> 

回答

0

的問題這裏面頁與搜索結果html模板中缺少控制器有關。 我認爲它會工作:

<table class="table table-index search-results" ng-controller="searchCtrl"> 
     <thead> 
      <tr class="info"> 
       <th colspan="3"> 
        <div>Filter By: 
         <select ng-model="searchData" ng-options="f.id as f.value for f in filters" class="search-filter" ng-change="filterCriteria(searchData)"> 
          <option>--none--</option> 
         </select> 
        </div> 
       </th> 
      </tr> 
      <tr> 
       <th ng-repeat="th in headList" class="{{th.css}}" ng-click="sort(th.value)">{{th.name}}</th> 
      </tr> 
     </thead> 
     <tfoot> 
      <tr> 
       <td colspan="3">&nbsp;</td> 
      </tr> 
     </tfoot> 
     <tbody> 
      <tr ng-repeat="resultObj in obj.list"> 
       <td>{{$index + 1}}.</td> 
       <td>{{resultObj.categoryId}}</td> 
       <td>{{resultObj.value}}</td> 
      </tr> 
     </tbody> 
    </table> 

但更好的辦法將是你的包裹與部分一個標籤,並使用一個控制器聲明。