javascript - WinJS.UI.ListView 自定义列表项内容?

标签 javascript windows-8 winjs

我正在开发基于 Javascript 的 Windows 8 Metro 应用程序。它使用 WinJS.UI.ListView 来显示 1 列的项目列表。除了文档中所说的,我不知道太多:http://msdn.microsoft.com/en-us/library/windows/apps/br211837.aspx .

如何根据数据源自定义列表项内容?是否有可用的回调函数,以便对于每个列表项,我可以根据数据数组中相应对象的变量显示/隐藏某些 HTML 标记?

像这个 MSDN 文档的示例部分中的安排,我如何根据 picture 是否存在来显示/隐藏 img 标签?

最重要的是,我如何获得可变的列表项高度?基于上面提到的显示/隐藏功能,我的每个列表项都会有不同的高度(实际上所有项目只有 2 个不同的高度)。如何实现这种行为?

谢谢。

最佳答案

WinJS.UI.ListView 中的列表布局支持不同高度的项目;它必须是一个特定的高度。如果您需要该布局,则必须自己创建。根据您的数据源和项目数量,这要么是微不足道的,要么是困难的。 :)

说的是:

  • ListView 确实支持每个项目的不同模板。 itemTemplate 属性允许您提供一个函数。详情是here
  • 网格布局确实支持不同大小的项目,基于一些属性和计算。详情 here, at the end

如果您的数据集较小 (<50),那么您可以自己动态创建元素,并使用子 div 的自然流一个接一个地堆叠您的元素。这个解决方案的种子可以在我的回答中找到 here .

让我扩展示例控件,基于 VS 中的“空白”WWA 项目

将此代码添加到您的 default.js:

WinJS.Namespace.define("Samples", {
    ItemsControl: WinJS.Class.define(function (element, options) {
        this.domElement = element;
        WinJS.UI.setOptions(this, options);
    }, {
        domElement: null,
        _dataSource: null,
        dataSource: {
            get: function () {
                return this._dataSource;
            },
            set: function (v) {
                this._dataSource = v;
                this._renderItems(v);
            }
        },
        pickTemplate: function (item) {
            // The tempalte is found in the WinJS.Binding.Template instances that are
            // in default.html. They have ID attributes on them. We're going to pick
            // which one we want by setting the ID we're looking for, and then getting
            // that element from the DOM and returning the winControl
            var templateId = "template1"
            if (item.isType2) {
                // Because this is "isType2", we're going to use the other template
                templateId = "template2";
            }

            return document.getElementById(templateId).winControl;
        },
        _renderItems: function (source) {
            // This function renders all the items, thus when you set the datasource, and
            // expect it to render items, you probably would like all your items to replace
            // any existing items.
            WinJS.Utilities.empty(this.domElement);
            source.forEach(function (item) {
                var newElement = document.createElement("div");
                this.domElement.appendChild(newElement);

                this.pickTemplate(item).render(item, newElement);
            }.bind(this));
        }
    }),
});

function makeContent() {
    var data = [
        { label: "First", },
        { label: "Second", },
        { label: "Third", isType2: true },
        { label: "Fourth", isType2: true },
        { label: "Fifth", },
        { label: "Sixth", isType2: true }
    ];

    var control = document.getElementById("itemsControl").winControl;

    control.dataSource = data;
}

并将 bodycontent 替换为:

<div data-win-control="WinJS.Binding.Template"
    id="template1">
    <div class="type1"
        data-win-bind="textContent: label"></div>
</div>

<div data-win-control="WinJS.Binding.Template"
    id="template2">
    <div class="type2"
        data-win-bind="textContent: label"></div>
</div>
<button onclick="makeContent()">Make Content</button>
<div id="itemsControl"
    data-win-control="Samples.ItemsControl">

</div>

最后,在default.css中:

    .type1 {
        background-color: green;
        height: 50px;
        width: 100px;
    }

    .type2 {
        background-color: red;
        height: 100px;
        width: 100px;
    }

关于javascript - WinJS.UI.ListView 自定义列表项内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14659952/

相关文章:

javascript - 我可以禁用 Windows 8 共享栏上的屏幕截图选项吗?

Javascript 倒计时不起作用

javascript - Windows 应用商店应用程序 - WinJS : 0x800a1391 - JavaScript runtime error: 'Windows' is undefined

javascript - jQuery - 如何填充(数据注入(inject))嵌套 SheepIt 表单

python - 为什么在将 Unicode 写入 CMD 时会出现 IOErrors? (代码页 65001)

c# - 有没有什么简单的技巧可以让 FlipView 在当前项目的左右两侧显示一点点项目?

windows-8 - 使用 HTML/Javascript 和 XAML/C# 开发的 Windows 8 Store App 有什么区别

javascript - 是否可以在 React 组件的功能表示中包含静态属性?

javascript - HTML 如果文本框中存在数据且没有焦点并且第二行下拉列表不提供数据,则创建一个新行

javascript - array.push 在 forEach 循环内不起作用