Angularjs, How to create Checkbox to insert array in Mongodb

Asked

Viewed 520 times

0

I created a collection on mondoDB called Sleeves, where you have "name, author, genre and info"

Gender is array.

I have a form that will receive the values name, author and info, the Checkbox must be filled as Array with values to be inserted with the forumulario

Question. How to make a Checkbox that has 2 options "Adventure and Action" and insert into mongoDB as array?

Like the collection below.

Mongodb Mangas

db.mangas.insert({
    nome:'Toriko',
    autor:'não sei',
    genero:['aventura','ação'],
    info:'...'
    })

Index.html

<tr>
  <td><input class="form-control" ng-model="manga.nome"></td>
  <td><input class="form-control" ng-model="manga.autor"></td>
  <!-- aqui precisa ser a CHECKBOX que irá ter os valores do array e inseridos no GENERO -->
  <td><input class="form-control" ng-model="manga.info"></td>



  <td><button class="btn btn-primary" ng-click="addManga()">Add manga</button></td>
</tr>

     <tr ng-repeat="manga in mangas">
       <td>{{manga.nome}}</td>
       <td>{{manga.autor}}</td>
       <td><!-- aqui vai listar o valor recebido do checkbox --></td>
       <td>{{manga.info}}</td>
     </tr>

AddManga function

$scope.addManga = function() {
    console.log($scope.manga);
    $http.post('/mangas', $scope.manga).success(function(response) {
      console.log(response);
       refresh();
    })};
  • Just to make sure I understand: your question is about how to create a list of checkboxes linked to an array in your scope, this?

  • That’s right, why do that? no gender, would be an array, checkboxex would have 2 options example:"Action", "Adventure", in the form, selects the 2 options of checkboxes and sends as array to mongoDB, so would create a generic table and look for all values of Action and Adventure.

  • @Juanbiscaia I don’t know how to create a checkbox with the values of the array and how to create a function to send the value of the checkboxes.

1 answer

1


I made a basic spa showing how you can use a function in the ng-click of each input checkbox to add or remove items from your generos array.

This app has a difference, instead of adding strings in the list I add objects, one for each gender, so you can make them more useful.

I used the style guide created by this guy to set up this spa.

Link to the Plnkr

  • 1

    Incredible, it was perfect , that’s what I’ve been looking for, thank you very much, Success.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.