2

オブジェクトの配列を含むviewmodelにobservablearray(mappedCompaignByInterest)があり、各オブジェクトは辞書のようで、文字列であるキーとオブジェクトの配列である値(Compaign)が含まれています。このオブジェクトを knockoutjs のテーブルにバインドする方法を教えてください。

ここに私のビューモデルがあります:

    function DashboardViewModel() {
    var self = this;        
    self.BuzzCompaignByInterest = ko.observableArray([]);

}

これはサーバーからデータをロードするためのものです

  //    Load initial state from server,
    $.getJSON("/Dashboard", function (Data) {            
        var mappedCompaignByInterest = Data.BuzzCompaignByInterest;            
        self.BuzzCompaignByInterest(mappedCompaignByInterest);                        
       });

Data.BuzzCompaignByInterest がサーバーから取得したいのはディクショナリであり、キーは文字列で、値は object(Compaign) の配列であることに注意してください。ここでは Compaign クラスのプロパティです。

 public class BuzzCompaignModel
{
    public long BuzzCompaignId { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
 }

BuzzComapignByInterest(viewmodelのobservablearray)からデータを表示するにはどうすればよいですか?

4

1 に答える 1

1

あなたの辞書項目は次のクラスのように見えると思います:

function DictionaryItem(key, value) {
                this.key = key;
                this.value = value;
            }

value は、次のような BuzzCompaignModel です。

function BuzzCompaignModel(id, name, description) {
            this.id = id;
            this.name = name;
            this.description = description;
        }

初期化された BuzzCompaignModel で DictionaryItem のコレクションを割り当てた後、次の方法でこの配列をバインドできます。

    <table>
        <tbody data-bind="foreach:BuzzCompaignByInterest">

            <tr>
                <td data-bind='text:key'></td>
                <td data-bind='text:value.id'></td>
                <td data-bind='text:value.name'></td>
                <td data-bind='text:value.description'></td>
            </tr>
        </tbody>
    </table>

また、 jsfiddle with example

于 2012-07-27T08:19:07.843 に答える