Knockout Grid - Loading Remote Data
http://wijmo.com/grid-with-knockout-viewmodel-loading-remote-data/
 We were hearing quite a few people asking how to best create a knockout ViewModel for our Grid with data fetched from a remote service. In order to help guide people through this scenario, we sat down and built an implementation. Along the way we added some features (as of Wijmo 2.2.0) to the Grid to make this even easier. Follow along to build your own Grid ViewModel using knockout. You can also see the knockout Grid demo online.
We were hearing quite a few people asking how to best create a knockout ViewModel for our Grid with data fetched from a remote service. In order to help guide people through this scenario, we sat down and built an implementation. Along the way we added some features (as of Wijmo 2.2.0) to the Grid to make this even easier. Follow along to build your own Grid ViewModel using knockout. You can also see the knockout Grid demo online.
Add JavaScript & CSS Dependencies
Add the following dependencies to your page: jQuery, jQuery UI, Wijmo & Knockout.
<!-- jQuery -->
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.23/jquery-ui.min.js"></script>
<!-- Wijmo CSS and script -->
<link type="text/css" href="http://cdn.wijmo.com/themes/metro/jquery-wijmo.css" rel="stylesheet" title="metro-jqueryui" />
<link type="text/css" href="http://cdn.wijmo.com/jquery.wijmo-complete.all.2.2.0.min.css" rel="stylesheet" />
<script type="text/javascript" src="http://cdn.wijmo.com/jquery.wijmo-open.all.2.2.0.min.js"></script>
<script type="text/javascript" src="http://cdn.wijmo.com/jquery.wijmo-complete.all.2.2.0.min.js"></script>
<!-- KnockoutJS for MVVM-->
<script type="text/javascript" src="http://cdn.wijmo.com/external/knockout-2.1.0.js"></script>
<script type="text/javascript" src="http://cdn.wijmo.com/external/knockout.wijmo.js"></script>
Create the ViewModel
Now we need to create the ViewModel for our Grid. This is where all the magic is. Our ViewModel is tailored to the Grid and has properties that the Grid will bind to for its options. The load method will be called from outside the ViewModel when new data is needed from the server. This web service happens to be an OData service, but any service type can be used here.
//Create ViewModel
var viewModel = {
pageSize: ko.observable(10),
pageIndex: ko.observable(0),
sortCommand: ko.observable("ProductID asc"),
dataRows: ko.observableArray([]),
totalRows: ko.observable(0),
sorted: function (e, data) {
viewModel.sortCommand(data.sortCommand);
},
paged: function (e, data) {
viewModel.pageIndex(data.newPageIndex);
},
load: function () {
$.ajax({
url: "http://services.odata.org/Northwind/Northwind.svc/Products",
dataType: "jsonp",
jsonp: "$callback",
data: {
$format: "json",
$inlinecount: "allpages",
$select: "ProductID,ProductName,UnitPrice,UnitsInStock",
$orderby: viewModel.sortCommand(),
$top: viewModel.pageSize(),
$skip: viewModel.pageIndex() * viewModel.pageSize(),
"paging[pageIndex]": viewModel.pageIndex(),
"paging[pageSize]": viewModel.pageSize()
},
success: function (result) {
var data = result.d.results;
var arr = []; $.each(data, function (i) {
arr.push(new product(data[i]));
});
viewModel.totalRows(result.d.__count);
viewModel.dataRows(arr);
}
});
} }; //Class constructor for grid row. Returns observable properties.
var product = function (data) {
return {
ProductID: ko.observable(data.ProductID),
ProductName: ko.observable(data.ProductName),
UnitPrice: ko.observable(data.UnitPrice),
UnitsInStock: ko.observable(data.UnitsInStock)
};
};
Create the View
This markup is really simple. We are just adding a Wijmo Dropdown (select) and Grid (table) to the page and binding their options to the ViewModel.
<div class="toolbar">
<label>Display: </label>
<select data-bind="value: pageSize, wijdropdown: {}">
<option value="5">5</option>
<option value="10">10</option>
<option value="20">20</option>
</select>
</div>
<table id="dataGrid" data-bind="
wijgrid: {
data: dataRows,
pageSize: pageSize,
pageIndex: pageIndex,
totalRows: totalRows,
allowPaging: true,
allowSorting: true,
sorted: sorted,
pageIndexChanged: paged,
columns: [
{ sortDirection: 'ascending', dataType: 'number', dataFormatString: 'n0', headerText: 'ID', width: 60 },
{ headerText: 'Product' },
{ dataType: 'currency', headerText: 'Price', width: 100},
{ dataType: 'number', dataFormatString: 'n0', headerText: 'Units', width: 100}]
}">
</table>
Initializing the App
Now the we have a ViewModel and View, we can initialize the app. This code initializes the KO bindings and adds listeners for critical components of the ViewModel in order to call the load() method when new data is needed.
//Bind ViewModel and Event Handlers
$(document).ready(function () {
ko.applyBindings(viewModel);
viewModel.load();
viewModel.sortCommand.subscribe(function (newValue) {
viewModel.load();
});
viewModel.pageIndex.subscribe(function (newValue) {
viewModel.load();
});
viewModel.pageSize.subscribe(function (newValue) {
viewModel.load();
$(":wijmo-wijdropdown").wijdropdown("refresh");
});
});
Run It!
That's it, just run your app and you have a Grid that fetches remote data when paging and sorting. You could also add other widgets to the app and bind to the same data in the ViewModel. This is the ideal solution for using knockout and the Wijmo Grid.
This demo is included in the download under Wijmo-Complete/development-bundle/demo-apps/knockout-grid. You can also play with the live version of this knockout Grid demo online.
Knockout Grid - Loading Remote Data的更多相关文章
- fastboot 刷system.img 提示 sending 'system' (*KB)... FAILED (remote: data too large)
		华为G6-C00卡刷提示OEMSBL错误,只能线刷 ,但是官方找不到线刷img镜像,无奈 网上下了个可以线刷的工具套件 流氓ROM . 使用HuaweiUpdateExtractor(工具百度)把官方 ... 
- Open Flash Chart IO ERROR Loading test data Error #2032
		http://blog.sina.com.cn/s/blog_6754464e0100qfvd.html Open Flash Chart 2 提示Open Flash Chart IO ERROR ... 
- [GraphQL] Query Local and Remote Data in Apollo Link State
		In this lesson, you will learn how to query local and remote data in Apollo Link State in the same c ... 
- [Vue-rx] Cache Remote Data Requests with RxJS and Vue.js
		A Promise invokes a function which stores a value that will be passed to a callback. So when you wra ... 
- rdo(remote data objects) repo openstack icehouse
		problem making ssl connection Error: Cannot retrieve repository metadata (repomd.xml) for repository ... 
- Js: Extensible Calendar  Examples
		http://ext.ensible.comhttps://github.com/bmoeskau/Extensiblehttps://github.com/TeamupCom/extensibleh ... 
- Jquery easyui 教程
		Jquery easyui教程 目 录 1基本拖放... 4 2构建购物车型拖放... 5 3创建课程表... 8 4菜单和按钮Menu and Bu ... 
- 基于 Angular Material 的 Data Grid 设计实现
		自 Extensions 组件库发布以来,Data Grid 成为了使用及咨询最多的组件.最开始 Data Grid 的设计非常简陋,经过一番重构,组件质量有了质的提升. Extensions 组件库 ... 
- 【RDA】使用RDA(Remote Diagnostic Agent)工具对数据库进行健康检查
		[RDA]使用RDA(Remote Diagnostic Agent)工具对数据库进行健康检查 分类: Linux RDA英文全称叫做"Oracle Remote Diagnostic Ag ... 
随机推荐
- javascript中的数组操作
			1.数组的创建 var arrayObj = new Array(); //创建一个数组 var arrayObj = new Array([size]); //创建一个数组并指定长度,注意不是上限, ... 
- Netty关闭客户端
			在启动客户端的时候,我们一般会 channelFuture.channel().closeFuture().sync(); 这是一段阻塞的代码,除非链路断了,否则是不会终止的,我们可以在handler ... 
- Expression<Func<T,TResult>>和Func<T,TResult>
			1.Expression<Func<T,TResult>>是表达式 //使用LambdaExpression构建表达式树 Expression<Func<int, ... 
- Android studio disign 问题
			有些低配置的电脑使用android studio 写xml的时候,disign会一直处于rendering,有可能是xml使用的图片过大导致渲染不出来 
- The Skyline Problem
			A city's skyline is the outer contour of the silhouette formed by all the buildings in that city whe ... 
- 一个Eclipse代码显示主题
- 20145208 实验二 Java面向对象程序设计
			20145208 实验二 Java面向对象程序设计 实验内容 初步掌握单元测试和TDD 理解并掌握面向对象三要素:封装.继承.多态 初步掌握UML建模 熟悉S.O.L.I.D原则 了解设计模式 实验步 ... 
- Ubuntu连接L2TP的VPN设置
			作为程序猿,FQ查资料那是经常的事情.电信不知怎么的pptp方式连接不上.蛋疼!o(╯□╰)o没有办法自己只好折腾l2tp连接方式了\(^o^)/~ 1. 添加PPA sudo apt-add-rep ... 
- php文件上传之多文件上传
			在胡说之前,首先声明,本文是建立在掌握php单文件上传的基础上,所以这里就不赘述文件上传服务器配置,表单设置该注意的地方了. 话不多少,直入主题,在请求页面方面有两种写法(只呈现表单部分,以上传三个文 ... 
- 【JVM】模板解释器--字节码的resolve过程
			1.背景 上文探讨了:[JVM]模板解释器--如何根据字节码生成汇编码? 本篇,我们来关注下字节码的resolve过程. 2.问题及准备工作 上文虽然探讨了字节码到汇编码的过程,但是: mov %ra ... 
