4

Trying to incorporate Angularjs multiselect into my project, however, whenever I try to pull the data from my mysql database, only the last record is being displayed.


html

<div isteven-multi-select
     input-model="allPersonnelGrouped"
     output-model="groupedOutput"
     button-label="icon name"
     item-label="icon name maker"
     tick-property="ticked"
     max-height="250px" 
     group-property="msGroup">
</div>

angularjs

$http.get('/messages/shifts').success(function(data) {

    $scope.groupedShifts = data;


    angular.forEach( $scope.groupedShifts, function( groupShift ) {

        $scope.allPersonnelGrouped = [
           { name: '<strong>All Shifts</strong>', msGroup: true },
           { name: '<strong>' + groupShift.title + '</strong>', msGroup: true },
           { icon: '', name: groupShift.personnel, maker: '(' + groupShift.email + ')', ticked: false },
           { msGroup: false },
           { msGroup: false }
        ];

    });

});

array of objects

[
   {
      "id": 1,
      "title": "Shift 1",
      "email": "[email protected]",
      "personnel": "Johnny Depp"
   },
   {
      "id": 2,
      "title": "Shift 2",
      "email": "[email protected]"
      "personnel": "Napoleon Bonaparte"
   }

]

In the example the last object in the array of objects above, only the last object is shown and I need all of them to be displayed.

Essentially the output I am getting is

enter image description here

3
  • Only the last object is show because inside the forEach loop you are creating a new array each time it executes. I was typing this as an answer (and w/a solution), but in doing so I realized I wasn't sure what your resulting array should look like. Should it be an array of objects (as you've shown above), or perhaps an array of arrays, where the inner array contains objects? Commented Apr 20, 2015 at 16:00
  • @Sunil D. It should be array of objects. Commented Apr 20, 2015 at 16:13
  • what would you suggest I do in order to access each property and display all the needed data? @Sunil D. Commented Apr 20, 2015 at 16:46

1 Answer 1

1

You are doing a forEach loop where you set the value of an array each time, wiping out the prior value. Instead, you want to:

  1. Initialize the array $scope.allPersonnelGrouped with the initial menu items ("All Shifts", etc.)
  2. Loop over the $scope.groupedShifts array and add items to $scope.allPersonnelGrouped to fill in the members of shift group
  3. After the loop, close the menu groups

I've only spent a few minutes looking at the multiselect directive, so this may not be exactly what you want.

// initialize the array
$scope.allPersonnelGrouped = [
  { name: '<strong>All Shifts</strong>', msGroup: true } 
];

angular.forEach( $scope.groupedShifts, function( groupShift ) {
  // populate the dynamic portion of the array
  $scope.allPersonnelGrouped.push(
    { name: '<strong>' + groupShift.title + '</strong>', msGroup: true }
  );

  $scope.allPersonnelGrouped.push(
    { icon: '', name: groupShift.personnel, maker: '(' + groupShift.email + ')', ticked: false }
  );
  $scope.allPersonnelGrouped.push({ msGroup: false });
});

// close the menu groups
$scope.allPersonnelGrouped.push(
  { msGroup: false }
);
Sign up to request clarification or add additional context in comments.

2 Comments

I was trying something similar, and your answer is almost exactly what I was looking for. its just that groupShift.title would be within the foreach loop (I'm assuming) or it would be undefined. All Shift does nothing when trying to select it though, and if i put groupShift.title within the foreach loop, it does what All Shift is supposed to do & that is selecting all shifts within the list. @Sunil D.
Figured it out. Thanks for a great response @Sunil D.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.