加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

angularjs – Angular UI select2 – 如何停止自动排序?

发布时间:2020-12-17 18:02:47 所属栏目:安全 来源:网络整理
导读:以下数据通过 Angular UI附于 select2:( Live example here) JS: $scope.items = [ {id: 1,text: 'elephant'},{id: 2,text: 'desk'},{id: 3,text: 'car'},{id: 4,text: 'boat'},{id: 5,text: 'apple'}];$scope.selected = []; HTML: select ui-select2 mu
以下数据通过 Angular UI附于 select2:( Live example here)

JS:

$scope.items = [
  {id: 1,text: 'elephant'},{id: 2,text: 'desk'},{id: 3,text: 'car'},{id: 4,text: 'boat'},{id: 5,text: 'apple'}
];
$scope.selected = [];

HTML:

<select ui-select2 
        multiple 
        ng-model="selected" 
        data-placeholder="Please select..." 
        style="width:200px">
  <option></option>
  <option ng-repeat="item in items" value="{{item.id}}">{{item.text}}</option>
</select>

但是,每次选择项目时,它都会按ID对所选项目进行排序.例如,如果您选择“apple”然后选择“boat”,则所选项目将为“boat”和“apple”(按此顺序!).

我怎样才能保留订单并禁用此自动排序?

解决方法

如果您使用ng-options而不是对每个项目执行ng-repeat看起来它保留了订单,如 here所示

您所要做的就是删除此行:

<option ng-repeat="item in items" value="{{item.id}}">{{item.text}}</option>

然后在您的代码中添加这样的指令:

ng-options="item.text for item in items"

这将使整个项目对象被附加到所选列表而不仅仅是ID,因此您需要将其考虑在内.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读