2016-04-29 70 views
0

我正在使用bootstrap table和ngStorage。我試圖做的是將表搜索保存到本地存儲並在刷新表後重新找回。 這是我簡單的代碼:

JS

//Assigning $localstorage to $session.storage 
     $scope.storage = $localStorage; 
     $scope.saveSearch = function() { 
      $scope.child = prompt('Set your filter.'); 

      $('#table').on('search.bs.table', function (e, text){ 
       $scope.storage = text 
      }); 

     }; 
     $scope.loadSearch = function() { 

      $scope.data = $scope.storage; 

     }; 

HTML

<div id="toolbar"> 
<div style="display: -webkit-box"> 
<button id="new" class="btn btn-default">New</button> 
<button id="edit" class="btn btn-default">Edit</button> 
<button id="remove" class="btn btn-default">Delete</button> 
<button class="btn btn-default " ng-click="saveSearch()">Save Filter</button> 
<button class="btn btn-default " ng-click = "loadSearch()">Load Filter</button> 
<button id="button" class="btn-primary btn">Load Table</button> 
<pre>{{data}}</pre></div> 
</div> 
table id="table" 
     class="table-striped" 
         data-toggle="table" 
         data-toolbar="#toolbar" 
         data-search="true" 
         data-show-columns="true" 
         data-show-export="true" 
         data-filter-control="true" 
         data-events="operateEvents" 
         data-formatter="operateFormatter" 
         data-response-handler="responseHandler" 
         > 
         </table> 

,並通過這個代碼,我越來越變回來,當我點擊按鈕加載篩選。您可以在Load Table按鈕的右側看到。

enter image description here

但是當我試圖把這個變量來搜索。

$scope.loadSearch = function() { 
      $('#table').on('search.bs.table', function (e, text){ 
       text.push($scope.storage); 
       console.log($scope.storage) 
      }); 
      $scope.data = $scope.storage; 
     }; 

我的功能根本不起作用。我很感激能否有人解釋我的錯誤在哪裏?

My plunker, where you can see the problem

With this plunker,就可以把價值從localStorage的搜索BS表,但不觸發。

回答

1

若要保存文本到本地存儲

$scope.storage.text = text; 

比來檢索文本

$scope.data = $scope.storage.text 
+0

正在檢索文本,問題是我無法推回這個文本來搜索 – Anton

+0

你可以發佈你的搜索代碼 –

+0

你嘗試過text.push($ scope.storage.text ); –

1

$scope.storage = $locastorage將整個$localstorage對象分配給變量。我不認爲這是你想要做的。

要閱讀$localstorage特定變量做

$scope.myvar = $localstorage.localvar 

此外text.push($scope.storage);不分配什麼$scope.storagemyarray.push(myvar)增加myvarmyarray

要東西放在$scope.storage

  • $scope.storage = text
  • $scope.storage.mykey = text
+0

它返回:'類型錯誤:$ scope.storage.push不是function' – Anton

+0

比較合適。 我做了一些閱讀:$ scope.storage = $ localstorage推動整個本地存儲在這個變量。請嘗試'$ scope.storage = text' – hicks