从API绑定动态数据到FlipView控件
Bind Dynamic Data from an API to FlipView control
我一直试图将数据从json API调用绑定到flipView,没有运气。下面是HTML代码:
<body>
<!--Define the Template Here-->
<div style="width:100%;height:100%" id="DataTemplate" data-win-control="WinJS.Binding.Template">
<div>
<img src="#" data-win-bind="src : image; alt: name" style="width:100%;height:100%"/>
<div>
<h3 data-win-bind="innerText : title" ></h3>
<h4 data-win-bind="innerText : title" ></h4>
</div>
</div>
</div>
<!--Ends Here-->
<div id="trgFilpView"
data-win-control="WinJS.UI.FlipView"
data-win-options="{itemDataSource:TrainersInformation.trList.dataSource,itemTemplate:DataTemplate}">
</div>
</body>
和Javascript:
var dataUrl = "https://movie.apisample?api=v1";
var vidArr = [];
WinJS.xhr({ url: dataUrl, responseType: "json" }).then(
function (d) {
var jsonResults = d.responseText;
var objdata = JSON.parse(d.responseText);
objdata.data.videos.forEach(function (item) {
vidArr.push({
title: item.title,
image: item.image.url
});
});
$.each(vidArr, function (index, movie) {
console.log(movie.title + ' | ' + movie.image);
});
},
function (e) {
// handle errors
}
);
//Define the List from the Array
var trainersList = new WinJS.Binding.List(vidArr);
var trainersInfo =
{
trList: trainersList
};
WinJS.Namespace.define("TrainersInformation", trainersInfo);
ajax调用工作,我可以成功地解析JSON,但是我有麻烦让它与flipview控件绑定。这段代码源自一个FlipView示例,该示例在代码中手动设置了Javascript对象。我正试图修改它以从API获取数据。
原始代码:
var trainerArray = [
{ name: "Singapore 01", image: "images/SlideShow/1.jpg", description: "2012" },
{ name: "Singapore 02", image: "images/SlideShow/2.jpg", description: "2012" },
{ name: "Singapore 03", image: "images/SlideShow/3.jpg", description: "2012" },
{ name: "Singapore 04", image: "images/SlideShow/4.jpg", description: "2012" },
{ name: "Singapore 05", image: "images/SlideShow/5.jpg", description: "2012" },
{ name: "Singapore 06", image: "images/SlideShow/6.jpg", description: "2012" },
{ name: "Singapore 07", image: "images/SlideShow/7.jpg", description: "2012" },
{ name: "Singapore 08", image: "images/SlideShow/8.jpg", description: "2012" },
{ name: "Singapore 09", image: "images/SlideShow/9.jpg", description: "2012" },
{ name: "Singapore 10", image: "images/SlideShow/10.jpg", description: "2012" },
{ name: "Singapore 11", image: "images/SlideShow/11.jpg", description: "2012" },
{ name: "Singapore 12", image: "images/SlideShow/12.jpg", description: "2012" },
];
//Define the List from the Array
var trainersList = new WinJS.Binding.List(trainerArray); //This is the Private data
//To expose Data publically, define namespace which defines
//The object containing the Property-Value pair.
//The property is the public name of the member and the value is variable which contains data
var trainersInfo =
{
trList: trainersList
};
WinJS.Namespace.define("TrainersInformation", trainersInfo);
try this:
var dataUrl = "https://movie.apisample?api=v1";
var vidArr = [];
WinJS.xhr({ url: dataUrl, responseType: "json" }).then(
function (d) {
var jsonResults = d.responseText;
var objdata = JSON.parse(d.responseText);
objdata.data.videos.forEach(function (item) {
vidArr.push({
title: item.title,
image: item.image.url
});
});
$.each(vidArr, function (index, movie) {
console.log(movie.title + ' | ' + movie.image);
});
//Define the List from the Array
var trainersList = new WinJS.Binding.List(vidArr);
trgFilpView.winControl.itemDataSource = trainersList.dataSource;
},
function (e) {
// handle errors
}
);
您可以通过trainersList.push({})
添加数据
相关文章:
- 音频控件在mouseover上显示,在mouseout上淡出
- 后焦点更改为IE 11中的地址栏,而不是转移到表单中的下一个控件
- 如何在读取XLS/XLSX本地文件时,使用IE的javascript代码启用未标记为安全的ActiveX控件
- Windows形成web浏览器控件和Javascript更改的DOM
- 如何将javascript事件从web浏览器wpf控件发送到wpf的c#代码
- 如何更改文本框控件的不透明度值
- 所有控件的组合框
- 创建一个方法,通过一个窗口进行迭代并获取Titanium中的所有控件
- 在angularjs中创建自定义控件的推荐方法
- 自定义控件中的双向绑定在SAPUI5中不起作用
- 如何在ASP中为用户控件添加Javascript对象网
- Dynamics 2016内部部署Crm客户端脚本.选项集控件类型缺少方法
- 从服务器获取数据并在选择控件中使用ng选项无法显示选项
- 在HTML5画布上添加按钮和控件
- 隐藏HTML5视频控件
- 向ASP控件添加自定义事件
- 从IE中的不同元素调用时,文件输入控件未正确启动
- 当用户点击音频控件时,无法接收点击事件
- 在codeigniter中具有控件的looper.js
- 从API绑定动态数据到FlipView控件