Home > Mobile >  How can I save a option from multi select in Angular?
How can I save a option from multi select in Angular?

Time:11-12

I have a modal with a single select called "Impacto OEE" that has 3 options "Queda de Disponibilidade", "Queda de Performance" and "Nenhum". If the option chosen by the user is "Queda de Disponibilidade" it will be saved as "Disponibilidade", if it is "Queda de Performance" it will be "Performance" and if it is "Nenhum" it will be "Nenhum". But when I click on register it is only saved as "Disponibilidade" or "Nenhum". In my database (PostgreSQL) the column is called "impact_oee" as ENUM with values "Disponibilidade, "Performance", "Nenhum".

I'm new to Angular, but I don't understand what I should do to make it work correctly, or what am I doing wrong. Below are parts of the code. Can someone help me?

Here is my code in HTML:

<div class="input-group">
    <isteven-multi-select directive-id="impactos" tabindex="4" class="multiselect" helper-elements="filter"
                          translation="traducaoMultiSelect"
                          input-model="impactos"
                          output-model="impacto"
                          button-label="name"
                          item-label="name"
                          tick-property="ticked"
                          ng-class="{'open':impactosOpen }"
                          on-open="impactosOpen = true"
                          on-close="impactosOpen = false"
                          selection-mode="single" />
</div>

MultiSelect Config:

$scope.impactos = [{ name: "Queda de Disponibilidade" }, { name: "Queda de Performance" }, { name: "Nenhum" }]

Where do I get the data from my server:

new Promise(function (resolve, reject) {
                    restApi.motivosParadaGerenciamento.query({}, function (array) {
                        $scope.motivos = [];
                        if (array.length > 0) {
                            for (var i = 0; i < array.length; i  ) {
                                $scope.motivos.push({
                                    id: array[i].id,
                                    ...

                                    impactos: array[i].impacto_oee,

                                    ...
                                });
                            }
                        }

My register function:

function cadastrar () {
    restApi.gerenciamentoMotivosParada.save({
        ...
        impacto_oee: $scope.selectedUnique($scope.impactos) === "Queda de Disponibilidade" ? "Disponibilidade" : "Queda de Performance" ? "Performance" : "Nenhum",
    }, ...
}

I have an update function that is used in the editing part that also doesn't work when I select "Nenhum". The "Nenhum" option is simply ignored.

function atualizar () {
    var motivo;
    if ($scope.editing >= 0) {
        motivo = {
            ...         
            impacto_oee: $scope.selectedUnique($scope.impacto) === "Queda de Disponibilidade" ? "Disponibilidade" : "Queda de Performance" ? "Performance" : "Nenhum" ? "Nenhum" : "Nenhum"
           ...
        };
    } 

CodePudding user response:

Issue is in your register function. Correct way to write it is as follows

function cadastrar () {
    restApi.gerenciamentoMotivosParada.save({
       ...
        impacto_oee: ($scope.selectedUnique($scope.impactos) === "Queda de Disponibilidade") ? "Disponibilidade" : ($scope.selectedUnique($scope.impactos) === "Queda de Performance") ? "Performance" : "Nenhum",
    }, ...
}

Explanation:

Basically what you are trying to achieve using Ternary Operator is if-elseif-else

if(condition1){
    //value1
}else if(condition2){
    //value2
}else{
    //value3
}

in your case, condition2 was missing, that is the reason, it was not checking "Queda de Performance" condition

  • Related