I am struggling figuring out how to correctly add more options to a select list dynamically.
This code works, but it wipes out everything existing in the array because it is setting the value of the array instead of pushing:
// add seller to dropdown list
var opts = [];
opts.push({Text: studentName, Value: result.sellerID});
console.log("opts: " + JSON.stringify(opts));
if (sellerOptionModel.sellers.indexOf(opts) < 0) {
sellerOptionModel.sellers(opts);
}
And this code does not work, it just adds a blank item to the dropdown select list:
// add seller to dropdown list
var opts = [];
opts.push({Text: studentName, Value: result.sellerID});
console.log("opts: " + JSON.stringify(opts));
if (sellerOptionModel.sellers.indexOf(opts) < 0) {
sellerOptionModel.sellers.push(opts);
}
ViewModel:
var SellerOptionModel = function() {
var self = this;
self.sellers = ko.observableArray([]);
self.sellerID = ko.observable();
}
Full JS function with the above KO code in in the success part:
$("#nextForm").on('click', function(e) {
e.preventDefault();
var room = $("#roomSelect").val();
var invalidItems = invalidItemsArr.join(",");
$("#invalidItems").val(invalidItems);
if(verify == "True") {
var seller = $('#sellerSelect').val();
}
if(room == "") {
e.preventDefault();
slideDownMsg("Please select a room first.");
slideUpMsg(4000);
} else {
var firstName = capitalizeFirstLetter($.trim($("#firstName").val()));
var lastName = capitalizeFirstLetter($.trim($("#lastName").val()));
if(verify != "True" && (firstName == "" || lastName == "")) {
e.preventDefault();
slideDownMsg("Please fill in the student's first and last name.");
slideUpMsg(3500);
} else if(seller == "") {
e.preventDefault();
slideDownMsg("Please select a seller from the dropdown.");
slideUpMsg(3500);
} else { // all good we can save the student
$.ajax({
url: '@Url.Action("SaveStudent", "Tally")',
type: 'POST',
data: $("#tallyForm").serialize(),
success: function (result) {
if(result.success == true) {
var studentName = firstName + " " + lastName;
slideDownMsg("Tally entry saved for " + studentName + ".");
slideUpMsg(4000);
var room = $("#roomSelect").val();
$("#tallyForm")[0].reset();
$("#roomSelect").val(room);
tallyViewModel.items([]);
tallyViewModel.addLine();
if(room != "") {
getImage(room);
}
tallyViewModel.runningQty(0);
tallyViewModel.runningTotal(0);
// add seller to dropdown list
var opts = [];
opts.push({Text: studentName, Value: result.sellerID});
console.log("opts: " + JSON.stringify(opts));
if (sellerOptionModel.sellers.indexOf(opts) < 0) {
sellerOptionModel.sellers.push({Text: studentName, Value: result.sellerID})
}
sellerOptionModel.sellers.sort();
} else {
slideDownMsg("Failed saving tally entry for " + firstName + " " + lastName + ".");
slideUpMsg(4000);
}
},
error: function (request, status, error) {
e.preventDefault();
displayError("Error", request.responseText);
}
});
}
}
});
optsarray to yoursellersarray (array within an array). You likely wantsellerOptionModel.sellers.push({Text: studentName, Value: result.sellerID})instead. But yourindexOfwill always be -1 as that compares objects by reference. - JohnnyHK