【问题标题】:ListView doesn't update when underlying object changes底层对象更改时 ListView 不更新
【发布时间】:2012-09-04 05:31:11
【问题描述】:

我遇到了ListView 未在其数据源中显示对象的最新详细信息的问题。通过调用 WinJS.Binding.List 对象的createSorted method 创建数据源。每个对象如下所示:

var obj = {
    title: 'First item',
    priority: 2
};

我像这样创建/设置数据源:

sortedItemList = itemList.createSorted(function (lhs, rhs) {
    return rhs.priority - lhs.priority;
});
listView.itemDataSource = sortedItemList.dataSource;

ListView 的 itemTemplate 如下所示:

<div id="itemTemplate" data-win-control="WinJS.Binding.Template">
    <div>
        <h4 data-win-bind="innerText: title"></h4>
    </div>
</div>

两个字段的更改处理程序如下所示:

titleControl.onchange = function () {
    curItem.title = titleControl.value;
    sortedItemList.notifyMutated(sortedItemList.indexOf(curItem););
};
priorityControl.onchange = function () {
    curItem.priority = priorityControl.value;
    sortedItemList.notifyMutated(sortedItemList.indexOf(curItem););
};

createSorted 的文档说要确保在对象更改时调用 notifyMutated。如果我更改优先级,则 ListView 将适当地移动项目。但是,如果我编辑标题,则 ListView 不会更新以显示新标题。我做错了什么?

【问题讨论】:

    标签: javascript windows-8 microsoft-metro


    【解决方案1】:

    当调用其底层数据源的 notifyMutated 时,ListView 似乎没有显式地重新绑定其元素。如果对 notifyMutated 的调用导致元素被移动,那么它将被反弹,因为元素被销毁并重新创建。否则,您需要导致重新绑定发生。我的变更处理程序现在看起来像这样:

    var notifyMutated = function () {
        var prevIndex,
            postIndex;
    
        prevIndex = sortedItemList.indexOf(curItem);
        sortedItemList.notifyMutated(prevIndex);
        postIndex = sortedItemList.indexOf(curItem);
    
        if (postIndex !== prevIndex) {
            WinJS.Binding.processAll(listView.elementFromIndex(postIndex), curItem);
        }
    };
    
    titleControl.onchange = function () {
        curItem.title = titleControl.value;
        notifyMutated();
    };
    priorityControl.onchange = function () {
        curItem.priority = priorityControl.value;
        notifyMutated();
    };
    

    【讨论】:

      猜你喜欢
      • 2020-09-02
      • 1970-01-01
      • 2016-08-24
      • 1970-01-01
      • 2014-05-24
      • 1970-01-01
      • 2011-01-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多