angular-ui / ui-select2

AngularJS wrapper for select2 (deprecated, use angular-ui/ui-select)
https://github.com/angular-ui/ui-select
MIT License
596 stars 443 forks source link

Can't pass parameters to ui-select2 #297

Open 50l3r opened 8 years ago

50l3r commented 8 years ago

I have this html code

<div class="col-lg-8">
    {{Item.CotoNombre}}
    <select id="PaisId" ui-select2="select2Relative(Item)" ng-model="PaisId" class="select2-single form-control">
        <option value="iphone4">iPhone 4</option>
        <option value="iphone5">iPhone 5</option>
        <option value="iphone6">iPhone 6</option>
    </select>
</div>

and this is my Controller with ui-select2 function:


'use strict';

app.controller('AppController', function($scope, $controller, $rootScope, $state, $stateParams, AppFactory, AuthFactory, CRUD_CONFIG) {    
angular.extend(this, $controller('CoreController', {$scope: $scope}));

$scope.AppPermalink = $stateParams.AppPermalink;

$scope.init = function() {
    $scope.getApp();

    switch($state.current.name){
        case "appedit":
            $scope.getItem();
        break;
    }
}

/**
 * [Obtener informacion de aplicacion]
 */
$scope.getApp = function() {
    AppFactory.Applications($stateParams.AppPermalink).success(function(data) {
        $rootScope.App = data[0];
        $scope.AppPermalink = data[0].Permalink;

        $scope.getResults();
    }).error(function() {
        //No se ha encontrado la app 404
    });
}

/**
 * [Listar registros]
 */
$scope.getResults = function() {
    AppFactory.Items($stateParams.AppPermalink).success(function(data) {
        $scope.Results = data.data;
        $scope.ResultsCount = data.total;
    }).error(function() {
        $scope.ResultsCount = 0;
    });
}

/**
 * Ficha de registro
 */
$scope.getItem = function() {
    AppFactory.Item($stateParams.AppPermalink, $stateParams.ItemId).success(function(data) {
        $scope.Item = data.data[0];
    }).error(function() {
        //No se ha encontrado la app 404
    });
}

/**
 * [Eliminar registro]
 * @param  {int} Item [Registro]
 */
$scope.removeItem = function(Item) {
    AppFactory.RemoveItem($scope.Permalink, Item._EntityId).success(function(data) {
        var index = $scope.Results.indexOf(Item);
        $scope.Results.splice(Item, 1);
    }).error(function() {
        //Error al borrar
    });
}

$scope.select2Relative = function(Field) {
    console.log(Field);

    return {
        multiple: true,
        ajax: {
            url: CRUD_CONFIG.apiUrl+"/"+CRUD_CONFIG.appFarm+"/apps/get/" + $stateParams.AppPermalink,
            method: "GET",
            dataType: 'json',
            data: function (term, page) {

                return {
                    filters: "{\""+Field+"\": {\"type\": 4, \"string\": "+term.term+"}}",
                    page: page
                };
            },
            results: function (data, page) {
                return {results: data};
            }
        },
        formatResult: function(object, container, query) {
            return object.name;
        }
    }
}

/**
 * [Obtener valor de campo segun su tipo]
 * @param  {[type]} Field  [Campo de aplicacion]
 * @param  {[type]} Result [Registro]
 */
$scope.renderValueField = function(Field, Result) {
    if(Field.Type.lastIndexOf("[")>0){
        var FieldType = Field.Type.substring(0,Field.Type.lastIndexOf("["));
    }else{
        var FieldType = Field.Type;
    }

    switch(FieldType){
        case "relative":
            var Vars = angular.fromJson(Field.Vars);
            return Result[Field.Name][Vars.relative];
            break;

        case "multiple_relative":
            var Vars = angular.fromJson(Field.Vars);
            var res = "";
            angular.forEach(Result[Field.Name], function(value, key) {
                res = res + value[Vars.relative]+", ";
            });
            return res.trim().substring(0,(res.length - 2));
            break;

        default:
            return Result[Field.Name];
    }
}

$scope.init();
});

Field are undefined vars but should not. "Item.CotoNombre" prints me the value but ui-select2 cant get param.

I need to pass a dinamic url and params to my ajax call and i dont know other method.

What do I do wrong?