1

I have a webapp and two models which I would like to use. One model is Trainees and another one is TraineeStatus. I would like to get an array or a list of TraineeStatus inside Trainees and then pass it to AngularJS view an display it inside select method, what is the best approach to do it?

Trainees.java

@Entity
public class Trainees {

    @Id
    @GeneratedValue
    private int traineesID;
    private int groupsID;
    @ManyToOne
    private TraineeStatus traineestatus;
    private int customersID;
    private String name;
    private String surname;
    private String phoneDetails;
    private String email;

    public Trainees(){

    }

    public Trainees(String name, String surname, String phoneDetails, String email, int id, int groupsID, TraineeStatus traineestatus, int customersID) {
        super();
        this.name = name;
        this.surname = surname;
        this.email = email;
        this.phoneDetails = phoneDetails;
        this.groupsID = groupsID;
        this.traineestatus = traineestatus;
        this.customersID = customersID;
    }

    //getters and setters

    @Override
    public boolean equals(Object object) {
        if (object instanceof Trainees){
            Trainees contact = (Trainees) object;
            return contact.traineesID == traineesID;
        }

        return false;
    }

    @Override
    public int hashCode() {
        return traineesID;
    }
}

TraineeStatus.java

@Entity
@Table(name="traineeStatus")
public class TraineeStatus {

    @Id
    @GeneratedValue
    private int traineeStatusId;
    private String value;

    public TraineeStatus(){

    }

    public TraineeStatus(String value, int id) {
        super();
        this.value = value;
    }

    //getters and setters

    @Override
    public boolean equals(Object object) {
        if (object instanceof TraineeStatus){
            TraineeStatus value = (TraineeStatus) object;
            return value.traineeStatusId == traineeStatusId;
        }

        return false;
    }

    @Override
    public int hashCode() {
        return traineeStatusId;
    }
}

My view:

 <div class="input-append">
                        <select
                               required
                               ng-model="contact.traineestatus"
                               name="traineestatus.id"
                               placeholder="<spring:message code='sample.status'/> ">
                        <option ng-repeat="trainee in contact.traineestatus" value="{{trainee.id}}">{{trainee.value}}</option>
                        </select>
                    </div>
                    <div class="input-append">
                        <label>
                                <span class="alert alert-error"
                                      ng-show="displayValidationError && updateContactForm.traineestatus.id.$error.required">
                                    <spring:message code="required"/>
                                </span>
                        </label>
                    </div>

Currently with contact.traineestatus returns one object with id and value, but I would like to get an array.

I am trying to utilize the object, so I would have a select function, where it displays the value and would save the Id and save it in the Trainees table.

Is there a way to utilize this object and use it in my select? Thanks in regards!

Spinxas
  • 71
  • 1
  • 2
  • 15
  • just do `trainee in contact.traineestatuss` and while doing expression do `{{trainee.id}}` and `{{trainee.value}}` See link https://docs.angularjs.org/api/ng/directive/ngRepeat – Sajan Chandran Oct 02 '14 at 09:26
  • @SajanChandran It worked, but somehow now I am getting blank objects. The object only has id and value, with contact.traineestatus.id it returns the objects Id, but while using it inside ng-repeat and calling as you said trainee.id id does not display it. Thanks for quick reply. – Spinxas Oct 02 '14 at 09:38
  • @SajanChandran I have modified the question, due to getting the object not as an array, but a single object with id and value. – Spinxas Oct 02 '14 at 10:30
  • If you only have a single object why do you need a `ng-repeat`? Shouldn't you just have `` ? – bmleite Oct 02 '14 at 11:11
  • @bmleite Hello, I want to retrieve instead of single object, an array of objects from the model TraineeStatus and then implement ng-repeat. Hope it clears up a bit. – Spinxas Oct 02 '14 at 11:51
  • Ok. I'm not sure which web server framework are you using but I imagine that you have some kind of API to return a Trainee or a list of Trainees. If yes, then you must implement the same logic to return the list of TraineeStatus (note that you want all of them and not the one associated to a single contact). If you provide the code you are using to load the contacts on your angularjs app it will probably be easier to explain. – bmleite Oct 02 '14 at 12:28
  • @bmleite https://gist.github.com/anonymous/4380145c69b03e68160a This is my project with required files, which does all the passing through. My aim was to create a shortcut inside Trainee model to get a list of TraineeStatuses and them pass it to the view using same controller. I hope you can help me. :) – Spinxas Oct 02 '14 at 12:41

1 Answers1

2

This is a little difficult to explain, so I will try to keep it simple.

First of all you need to have in mind the the list of TraineeStatus is not going to be retrieved in the same request has the contact/trainees list, you will need a separate HTTP request to get that list.

The same way you have ContactRepository to get all the Trainees records, you will need a TraineeStatusRepository to get all the TraineeStatus records. Then, on one of your Services you would need a method to get the TraineeStatus list, like this:

private List<TraineeStatus> findAllTraineeStatus() {
    return traineeStatusRepository.findAll();
}

After that you will need to define an HTTP API to access that list (i.e. GET http://example.com/traineeStatus). The idea is probably the same you have to retrieve the ContactList.

Then, on your AngularJS app, you would need to use a function to load the list of TraineeStatus. For example:

$scope.getTraineeStatus = function () {
    // assuming the HTTP API is: GET http://example.com/traineeStatus
    $http.get('http://example.com/traineeStatus', config)
        .success(function (data) {
            $scope.traineeStatus = data;
        })
        .error(function (err) {
            // handle error
        });
}

And then loop through the $scope.traineeStatus property on the ng-repeat:

<select
       required
       ng-model="contact.traineestatus"
       name="traineestatus.id"
       placeholder="<spring:message code='sample.status'/> ">
<option ng-repeat="status in traineeStatus" value="{{status.id}}">{{status.value}}</option>
</select>
bmleite
  • 26,850
  • 4
  • 71
  • 46
  • Thank you very much for your answer, you are reaching my goal, but the problem I am facing is that I cannot use traineeStatus loop like you showed inside contactsController, everything else you said is right, just need to fix the issue that I cannot use two controllers at a time. For example: I am using contactsController in angularJS to manipulate the table displayed and one of the columns requires your answered method which would manipulate the view of the status. I hope you understand my goal. :) Thanks! – Spinxas Oct 02 '14 at 22:45
  • You can use 2 controllers at the same time. Check AngularJS [scope's prototypal inheritance](http://stackoverflow.com/questions/14049480/what-are-the-nuances-of-scope-prototypal-prototypical-inheritance-in-angularjs). Also check this **[Plunker](http://plnkr.co/edit/AhD6xRzfIMTix1W9wdR2?p=preview)** and see if it helps. – bmleite Oct 03 '14 at 09:31