It is not throwing an error, it is just not setting the default value
of the drop down list at all.
In the following code I loop through my DOM object that contains all
my product types and only select the ones I want to load into the drop
down box:
$.each(window.productTypes, function (index, value) {
if (value.Product ==
crm.rateCard.administration.viewModel.Product()) {
prods.push(value);
}
});
prods.sort(crm.rateCard.administration.sortProductTypeDetail);
Then I init my observableArray with it:
crm.rateCard.administration.viewModel.productTypeOptions =
ko.observableArray(prods);
var item = null;
Now I am getting the object that I want to use to set the default
value in the drop down box:
ko.utils.arrayForEach(crm.rateCard.administration.viewModel.productTypeOptions(),
function (e) {
if (e.Id == result.ProductType) {
item = e;
}
return item;
});
And now I am init the default value in the drop down box, and this
part simply does not do what I expect it to do, it does nothing, I am
expecting the correct default value to be pre-selected in my drop down
box:
crm.rateCard.administration.viewModel.selectedProductType
= ko.observable(item);
ko.applyBindings(crm.rateCard.administration.viewModel);
I cannot believe that I am the only one that needs to init a drop down
box with a default value after it has been altered/reloaded. How hard
can it be? I am missing something obvious here?! Maybe in stead of
trying th do a mapping.updateJSON, I shoud new up my viewmodel
entirely, although even with that I still need to pre-select my
productTypeOptions' default value.
Just to help here a a snap shot of what one of the productTypeOptions
objects look like:
Commissionable true
CoverageFrom 1
CoverageTo 2
Description "StoreFront Package"
Detail "StoreFront Package (1 - 2)"
GenerateHowManyTrafficLineItems 0
Id 5
Listing false
Name "StoreFront Package"
Product 1
So when the json object comes back from the server I use
mapping.updateJSON to refresh my viewmodel, but recall I am also
completely re-loading the productTypeOptions observableArray with new
values. Here is what that json object from the server looks like:
Sort by key
Id "309"
Name "2010.001"
Year "2010"
Version "001"
Description "2010.001"
Product 3
ProductType 20
Online 4
Amount 4350
GrossAmountOverride false
EffectiveFrom "1/1/2010"
EffectiveTo "1/1/2011"
CoverageFromTo "(1 - 2)"
TotalContracts 0
TotalNonContractContracts 0
TotalContractContracts 0
Used "False"
I then look for the correct productType in window.productTypes to find
the object and using the json object's ProductType ("20" in this
example) to get the object and using that object to pre-select the
drop down list that productTypeOptions is bound to:
<select id="ProductType" data-bind="options:
productTypeOptions,optionsCaption: '...', optionsText: 'Detail',
optionsValue: 'Id',value: selectedProductType" >
</select>
I thought if I can pass in the object to
crm.rateCard.administration.viewModel.selectedProductType
= ko.observable(item);
,item being that object, that it should then select that option from
the drop down list, but it does not. Any clues of why this does not
work? Much appreciate any feedback. If anyone has an example of where
they are pre-selecting a value from a drop down box with a list of
objects, not simply types like strings, please paste it in here, no
need to taylor my example if it's easier to just share how to set a
default value for a drop down box AFTER the viewmodel has been
reloaded.