2013-02-24 43 views
1

我想將jQueryUI Selectable Widget與AngularJS一起使用。我設法使用來自AngularUI的ui-jq jQuery Passthrough指令來包含它,它很好地運行以獲得所需的UI效果。現在我想使用AngularJS更新所選項目的數據,但無法想出辦法。如何更新jQueryUI的選定對象可以用AngularJS選擇?

我發現AngularUI Sortable directive,這可能是一個很好的起點來實現一個Selectable指令,但正如我剛剛開始使用AngularJS時,我無法適應它的需要。

例子:

http://jsfiddle.net/ffeldha/2KzRt/

如何更新所選項目的名稱?

回答

3

您可以創建可選的一個指令,而無需角UI和與addItem()方法的範圍添加項目

HTML:

<ol ui-selectable> 

JS

var myApp = angular.module('soil', []) 

myApp.directive('uiSelectable', function() { 
    return function (scope, el, attrs) { 
     el.selectable(); 
    }; 
}); 

function ItemCtrl($scope) { 
    /* triggered by "ng-submit" on a form*/ 
    $scope.addItem = function() { 
     /* newItem comes from "ng-model" on an input*/ 
     if (!$scope.newItem.length) { 
      return; 
     } 
     $scope.items.push({ 
      name: $scope.newItem 
     }); 

     $scope.newItem = ''; 
    }; 
    $scope.newItem = ''; 
    $scope.items = [{ 
     name: 'one' 
    }, { 
     name: 'two' 
    }, { 
     name: 'three' 
    }]; 
} 

DEMO:http://jsfiddle.net/2KzRt/5/

更新這裏的選擇時,如何創造出一個動態模型集更新列表項:

HTML:

<div id="update_items" ng-show="updateItems.length"> 
     <div ng-repeat="item in updateItems"> 
      <input value="{{item.name}}" ng-model="items[item.index].name"/> 
     </div> 
     <button ng-click="updateItems=[]">Cancel</button> 
    </div> 

JS:

var myApp = angular.module('soil', []) 

myApp.directive('uiSelectable', function() { 
    return function (scope, el, attrs) { 
     el.selectable({ 
      stop:function(evt,ui){ 

       var selected=el.find('.ui-selected').map(function(){ 
        var idx=$(this).index(); 
        return {name: scope.items[idx].name, index:idx} 
       }).get(); 

       scope.updateItems=selected; 
       scope.$apply() 
      } 
     }); 
    }; 
}); 


function ItemCtrl($scope) { 

    $scope.addItem = function() { 
     if (!$scope.newItem.length) { 
      return; 
     } 
     $scope.items.push({ 
      name: $scope.newItem 
     }); 

     $scope.newItem = ''; 
    }; 
    $scope.newItem = ''; 
    $scope.updateItems=[]; 
    $scope.items = [{ 
     name: 'one' 
    }, { 
     name: 'two' 
    }, { 
     name: 'three' 
    }]; 
} 

DEMO:http://jsfiddle.net/2KzRt/7/

+0

感謝更新,似乎解決了我的問題。我會將它應用到我的完整應用程序中,以便在接受您的答案之前查看是否有任何我不明白的地方。 – 2013-02-24 16:12:04

+1

正在學習角自己...這是很好的練習,以瞭解如何創建動態模型..希望它適合你 – charlietfl 2013-02-24 16:13:09

+0

它工作得很好。我根據自己的需要調整了它,並添加了一個對話框來詢問所選項目的新名稱:http://jsfiddle.net/SaBaJ/2/ – 2013-02-24 18:28:32

相關問題