簡體   English   中英

使用 angularjs 在 Select2 中設置選定的值

[英]Set selected value in Select2 using angularjs

我在使用“ui.select2”實現的 Anjularjs 應用程序中有一個下拉菜單

我已將其初始化如下

  <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}

獲取 select2 元素對象並應用以下代碼:

angular.element("#select2_id").select2('data', { "text": "text", "id": [id]});
    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.

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM