0

For example if my loop is as below

function test(obsArray,dynamicColumnName){
     var item = obsArray();
    ko.utils.arrayForEach(item, function(data) {
                computedColName = ko.computed(function () {
                    return data.dynamicColumnName();

    });
}

How do i pass dynamic columns in loop of array?

I tried the above approach but i get error as Object doesnt support property or method dynamicColumnName

James
  • 1,827
  • 5
  • 39
  • 69
  • Please post the full code. what is item? is it an array? does the array item have an observable called dynamicColumnName? – Sherin Mathew Jun 18 '14 at 05:07
  • @SherinMathew item is an array. the observable doesnt have property called dynamicColumnName but i am passing valid property via that parameter in function test – James Jun 18 '14 at 05:09
  • If the array item doesn't have a property, how can you call it like this - data.dynamicColumnName()? What are you trying to achieve? – Sherin Mathew Jun 18 '14 at 05:12
  • possible duplicate of [JavaScript object: access variable property by name as string](http://stackoverflow.com/questions/4255472/javascript-object-access-variable-property-by-name-as-string)... javascript 101. – Jeff Mercado Jun 18 '14 at 05:22
  • @JeffMercado so can i write like data.[dynamicColumnName]() ? can you tell exactly how to write it? – James Jun 18 '14 at 05:41
  • Assuming `dynamicColumnName` is a string or other appropriate type, then yes most likely. **Try it.** – Jeff Mercado Jun 18 '14 at 05:42
  • 1
    you are getting this error because `dynamicColumnName()` is not observable – Muhammad Raheel Jun 18 '14 at 06:42
  • @jeffMercado i tried like data.[dynamiccolumnname]() but it gives me syntax error identifier expected – James Jun 18 '14 at 07:00
  • @RaheelShan yes dynamicColumn is not observable but i want to access it as property of array – James Jun 18 '14 at 07:02

1 Answers1

0

I assunme you want to add computed to each object of array. Each computed should be dependant of name property.

var arr = [
    {name:"A",active:true},
    {name:"B",active:true},
    {name:"C",active:true},
]

function test(){
    var self = this

    self.TestData = ko.observableArray([])

    self.LoadData = function(){
        self.TestData(arr)
        self.MakeComputed()
    }

    self.MakeComputed = function(){
        ko.utils.arrayForEach(self.TestData(),function(data) {
            data.computedColName = ko.computed(function () {
                return data.dynamicColumnName();
            });
        });     
    }
    self.LoadData()
}

ko.applyBindings(new test())
Muhammad Raheel
  • 19,823
  • 7
  • 67
  • 103