【问题标题】:Two way Databinding with Angular-UI Select2 with Ajax functionality带有 Ajax 功能的 Angular-UI Select2 的两种方式数据绑定
【发布时间】:2012-11-04 11:27:07
【问题描述】:

AngularJS 的双向数据绑定 + Angular-UI Select2 与 Ajax 功能。

我创建了一个指令来以通用方式实现 Select2 下拉 Ajax 行为:-(它需要几个属性来获取 formatResult、调用 formatSelection 方法和 url)。

我的问题是如何在“编辑模式”中加载值,从下拉列表中选择的值是在格式选择方法中接收的,但是在加载屏幕时,我想从绑定的相同值加载下拉列表。 示例:-

    <input type="hidden" for="part" class="bigdrop" style="width: 250px" formatresult="partFormatResult" formatselection="partFormatSelection" aurl="/api/Part" search-drop-down ui-select2="configPartSelect2" ng-model="product.SalesPart" data-placeholder="Select Part" ng-change="onPartSelect();" />

    Directive Code

    One23SRCApp.directive('searchDropDown', ['$http', function (http) {
        return function (scope, elm, attrs) {
            var raw = elm[0];
            var fetchFuncName = "fetch" + attrs["uiSelect2"];
            console.log("Directive load pat  " + scope[attrs["ngModel"]]);
            scope[fetchFuncName] = function (queryParams) {
                var result = http.get(queryParams.url, queryParams.data).success(queryParams.success);
                result.abort = function () {
                    return null;
                };
                return result;
            };


            scope[attrs["uiSelect2"]] = {
                minimumInputLength: 3,
              initSelection: scope[attrs["initselection"]],
                ajax: {
                    url: attrs["aurl"],
                    data: function (term, page) {
                            return { params: { isStockable: true, query: term, pageNo: page, pageSize: 20 } };
                    },
                    dataType: 'json',
                    quietMillis: 100,
                    transport: scope[fetchFuncName],
                    results: function (data, page) {
                        var more = (page * 20) <= data.length; // whether or not there are more results available
                        return { results: data, more: more };
                    }
                },
                formatResult: scope[attrs["formatresult"]],
                formatSelection: scope[attrs["formatselection"]],
                dropdownCssClass: "bigdrop" // apply css that makes the dropdown taller
            };

            return { bind: attrs["ngModel"] };
        };

    }]);

//inside controller-
after loading of data
   $("#partDD").select2("val", product.SalesPart);
//$scope.partInitSelection definition.
    $scope.partInitSelection = function (element, callback) {
        if (! $scope.PartList) {
            $scope.PartList = [$scope.product.SalesPart];
        } else {
            $scope.PartList.push($scope.product.SalesPart);
        }
        callback($scope.product.SalesPart);
    };

}

【问题讨论】:

标签: angularjs jquery-select2 angular-ui


【解决方案1】:

终于修好了:- 当我将配置对象保存在范围 [attrs["uiSelect2"]] 中时, 每次加载数据时,我都会调用上述配置对象的 .ajax.data 方法。

scope[attrs["uiSelect2"]].ajax.results(product.SalesPart.text, 1);

【讨论】:

    猜你喜欢
    • 2014-02-25
    • 1970-01-01
    • 1970-01-01
    • 2013-06-12
    • 2016-02-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-09
    相关资源
    最近更新 更多