简体   繁体   English

如何在WinJS listview数据源没有项目时显示消息?

[英]How to show a message when WinJS listview data source has no items?

Is there a way to handle the ListView (WinJS) in case of data source has zero items? 如果数据源没有项目,有没有办法处理ListView(WinJS)? ie property or a method to show message 即属性或显示消息的方法

I find the easiest thing to do is to just have a span or div containing your message that you only show when the listview has 0 items. 我发现最简单的方法就是只有一个包含你的消息的spandiv ,只有当listview有0个项目时才显示。 This is substantially easier to do when using a binding library like KnockoutJS . 使用像KnockoutJS这样的绑定库时,这实际上更容易实现。

Example (placed directly below your WinJS listview): 示例(直接放在WinJS列表视图下方):

<h4 data-bind="visible: (resultDataSource().length === 0)">No Results Found</h4>

Easy to do with the built in WinJS data-binding 使用内置的WinJS数据绑定轻松完成

<div 
  data-win-control="WinJS.UI.ListView"
  data-win-options="{itemDataSource: app.model.items.dataSource}"
  data-win-bind="style.display: app.model.items app.displayBlockIf"
  ></div>
<div data-win-bind="style.display: app.model.items app.displayBlockIfNot">No items.</div>

In Code: 在代码中:

isSet = function(value) {
  if (!value) {
    return false;
  }
  if (value.length === 0) {
    return false;
  } else {
    return true;
  }
};

WinJS.Namespace.define("app", {
  displayBlockIf: WinJS.Binding.converter(function(value) {
    if isSet(value) then "block" else "none";
  }),

  displayBlockIfNot: WinJS.Binding.converter(function(value) {
    if !isSet(value) then "block" else "none";
  }),

  model: {
    items: new WinJS.Binding.List()
  }

});

Then somewhere in your pages ready() function: 然后在你的页面ready()函数的某个地方:

ready: function(element, options) {
  WinJS.Binding.processAll(element, { app: app });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM