2

我在使用 W8 的模板时遇到了很大的麻烦:

    <div class="itemtemplate" data-win-control="WinJS.Binding.Template">
    <div class="item">
        <img class="item-image" src="#" data-win-bind="src: backgroundImage; alt: title" />
        <div class="item-overlay">
            <h4 class="item-title" data-win-bind="textContent: title" style="margin-left: 0px; margin-right: 291.08px;"></h4>
            <h6 class="item-subtitle win-type-ellipsis" data-win-bind="textContent: subtitle"></h6>
        </div>
    </div>
</div>

但是有些项目我需要它们具有不同的外观,我需要为它们添加一个 ID,例如此代码在 data.js 中:

        var sampleItems = [
        { group: sampleGroups[0], importancia: "top", title: "Item Title: 1", subtitle: "Item Subtitle: 1", description: itemDescription, content: itemContent, backgroundImage: lightGray },
        { group: sampleGroups[0], title: "Item Title: 2", subtitle: "Item Subtitle: 2", description: itemDescription, content: itemContent, backgroundImage: darkGray },
        { group: sampleGroups[0], title: "Item Title: 3", subtitle: "Item Subtitle: 3", description: itemDescription, content: itemContent, backgroundImage: mediumGray },
        { group: sampleGroups[0], title: "Item Title: 4", subtitle: "Item Subtitle: 4", description: itemDescription, content: itemContent, backgroundImage: darkGray },
        { group: sampleGroups[0], title: "Item Title: 5", subtitle: "Item Subtitle: 5", description: itemDescription, content: itemContent, backgroundImage: mediumGray }];

我如何添加 ID?,因为选项importancia确定了我哪个有 ID。

4

1 回答 1

2

我使用dynamic template binding将值分配给项目的id属性ListView。不过,请注意,在@DominicHopton 指出的“潜在重复”中,您可能不想将 an 分配id给您的项目。

function listViewItemTemplate(item) {
    // data has has information about each item
    var data = item.data._value;

    var itemElement = document.createElement('div');
    if (data.shouldHaveId) { // check whatever condition that you need
        itemElement.id = data.id; // assign an id to your element
    }

    ...

    return {element: itemElement}
}

...

// Then you can assign the template to your listview
youListViewControl.itemTemplate = listViewItemTemplate;
于 2012-12-28T17:40:02.653 回答