Domanda

Ho difficoltà a provare a mappare i dati in un osservazione nidificata (tempi). Quando riempirò i miei partecipanti, OsservableArray funziona bene. Scommetto che devo fare qualcosa di speciale quando è nidificato.

Sono nuovo a Tramortire Quindi non sono sicuro di cosa sto facendo di sbagliato. Ho guardato la documentazione inutilmente, soprattutto a:

http://knockoutjs.com/documentation/plugins-mapping.html

Ecco il mio codice: (modificato)

<script type="text/javascript">

var TimeSlot = function (startTime, endTime) {
    var self = this;
    self.startTime = startTime;
    self.endTime = endTime;
};

var Participant = function (id, timeSlots) {
    var self = this;
    self.Name = id;
    self.roomName = ko.observable();
    self.timeSlots = ko.observableArray();

    var timeSlotVMs = _.map(timeSlots, function(ts) {
        return new TimeSlot(ts.startTime, ts.endTime);
    });

    ko.utils.arrayPushAll(self.timeSlots(), timeSlotVMs);
};

function ViewModel() {
    var self = this;
    self.participants = ko.observableArray([]);

    self.addPerson = function () {
         var data = '{"TimeSlot":[{"startTime":"05:23","endTime":"06:32"},
                     {"startTime":"10:23","endTime":"11:32"}]}';
         var partViewModel = new Participant(self.selectedValue().Id(), data.TimeSlot);
         self.participants.push();
    };
};



var viewModel = new ViewModel();
ko.applyBindings(viewModel);

</script>

E HTML se necessario:

<tbody data-bind="foreach: viewModel.participants">
    <tr>
        <td data-bind="text: Name"></td>
        <td data-bind="text: roomName"></td>

        <td data-bind="foreach: viewModel.participants.timeSlots">
                <span data-bind="text: startTime"></span>
        </td>
    </tr> 
</tbody>
È stato utile?

Soluzione

Prova questo:

var TimeSlot = function (startTime, endTime) {
    var self = this;
    self.startTime = startTime;
    self.endTime = endTime;
};

var Participant = function (id, timeSlots) {
    var self = this;
    self.Name = id;
    self.roomName = ko.observable();
    self.timeSlots = ko.observableArray();

    var timeSlotVMs = _.map(timeSlots, function(ts) {
        return new TimeSlot(ts.startTime, ts.endTime); 
    });
    ko.utils.arrayPushAll(self.timeSlots(), timeSlotVMs);        
};

function ViewModel() {
    var self = this;
    self.participants = ko.observableArray();

    self.addPerson = function () {
         var data = {
             TimeSlot: [
               {startTime:"05:23",endTime:"06:32"},
               {startTime:"10:23",endTime:"11:32"}
            ]
         };
         var partViewModel = new Participant(123, data.TimeSlot);
         self.participants.push(partViewModel);
    };
};

ero solito Lodash Funzione di libreria "mappa", per creare una matrice di ViewModels.

Inoltre, hai un errore nel tuo layout. Usa i tempi invece ViewModel.Participants.timeslots:

<tbody data-bind="foreach: participants">
    <tr>
        <td data-bind="text: Name"></td>
        <td data-bind="text: roomName"></td>

        <td data-bind="foreach: timeSlots">
            <span data-bind="text: startTime"></span>
        </td>
    </tr> 
</tbody>
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top