mercredi 7 décembre 2016

Get the value of a property from it's name

I have the following class in TypeScript:

class TrainingToAdd {
    public TrainingId: string = $("#Details_PendingTrainings_Key").val();
    public TrainingDate: string = $('#TrainingInstance_Instance_TrainingDate').val();
    public EfficiencyDescription: string = $('#TrainingInstance_Instance_EfficiencyDescription').val();
    public EmployeeId: string = $("#Employees_Key").val();
    public ResponsibleId: string = $('#TrainingInstance_Responsibles').val();
    public VersionId: string = $('#TrainingInstance_Versions').val();
}

Then I have the following function:

private beforeCreateOrUpdate(): void {
    if (!$('#form-instance').valid())
        return;

    showSpinner();
    let previous = $('#Details_New');

    for (let i: number = 0; i < this.pendingTrainings.trainingsToAdd.length; ++i) {
        let keys = Object.keys(this.pendingTrainings.trainingsToAdd[i]);

        for (let j: number = 0; j < keys.length; ++j) {

            let input = $('<input form="form-instance" class="employees-temp-array" type="hidden"' +
                '" name="Details.TrainingsToAdd[' + i + '].' + keys[j] + '" value="' + this.pendingTrainings.trainingsToAdd[i][keys[j]] + '" />');
            previous.after(input);
            input = previous;
        }
    }
}

The problem appears when I try to access a member of the TrainingToAdd class by it's name:

this.pendingTrainings.trainingsToAdd[i][keys[j]]

Any way to do that in TypeScript or should I just scrape it and go back to plain Javascript/JQuery?





Aucun commentaire:

Enregistrer un commentaire