【问题标题】:Set selected value in Select2 using angularjs使用 angularjs 在 Select2 中设置选定值
【发布时间】:2020-04-14 15:45:45
【问题描述】:

我的 Anjularjs 应用程序中有一个下拉菜单,使用 'ui.select2' 实现

我已经将它初始化如下

  <input type="text" ng-model="objCas.iProjectId" id=" iprojectid" ui-select2="iProjectIdOption" />

而我的 Js 实现是通过分页和过滤器从远程服务器获取数据

  var app = angular.module('CASApp', ['ui.select2', 'checklist-model']);
        app.controller('CASController', function ($scope, $http) {


            $scope.iProjectIdOption = {
                placeholder: "Click to choose the Project...",
                allowClear: true,
                initSelection: function (element, callback) {

                },
                ajax: {
                    url: "/Prj/dummy/Ajaxlist",
                    quietMillis: 0,
                    type: "POST",
                    data: function (term, page) {
                        return {
                            q: term,
                            page: page,
                            listType: "ProjectDetails"
                        }; // query params go here
                    },
                    results: function (data, page) { // parse the results into the format expected by Select2.
                        // since we are using custom formatting functions we do not need to alter remote JSON data
                        var more = (page * 30) < data.total_count; // whether or not there are more results available
                        return {
                            results: $.map(data.items, function (item) {
                                return {
                                    text: item.text,
                                    id: item.id
                                }
                            }),
                            more: more
                        }
                    },
                    cache: true
                }
            }
}
}

一切正常。我可以使用所有功能并发布值。但问题是在编辑时设置已经选择的值

试过了

    $Scope.objCas.iProjectId= {"text":"2010 / 256 - / dummytext","id":240}    
    $Scope.objCas.iProjectId=2;
    $scope.iProjectId.selected = {"text":"2010 / 256 - / dummytext","id":240}

【问题讨论】:

    标签: angularjs jquery-select2-4 ui-select2 angularjs-select2


    【解决方案1】:
        HTML code: no need of id
        <input type="text" ng-model="objCas.iProjectId" ui-select2="iProjectIdOption" />
    
        js code:
        use only this for default biniding
        $Scope.objCas.iProjectId= {"text":"2010 / 256 - / dummytext","id":240}  
    
    
        example code:
        link: http://embed.plnkr.co/K66Pf0/
        replace script.js file:
        // Code goes here
    var myAppModule = angular.module('MyApp', ['ui.select2']);
    
    myAppModule.controller('MyController', function($scope) {
        $scope.select2Options = {
          initSelection: function (element, callback) {
    
                    },
          ajax: {
            url: "./data.json",
            data: function (term, page) {
    
              return {}; // query params go here
            },
            results: function (data, page) { // parse the results into the format expected by Select2.
              // since we are using custom formatting functions we do not need to alter remote JSON data
              return {results: data}
            }
          }
        }
        console.log("$scope.select2Options-",$scope.select2Options.initSelection);
        $scope.testModel={ "id": 4, "text": "Fourth", "color": "red" }
    ;
    });
    
        you can view default selected value in dropdown.
    

    【讨论】:

      【解决方案2】:

      获取select2元素对象并应用以下代码:

      angular.element("#select2_id").select2('data', { "text": "text", "id": [id]});
      

      【讨论】:

      • 工作就像一个魅力......谢谢
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-03
      • 2013-10-23
      • 2012-10-05
      相关资源
      最近更新 更多