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.

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的更多相关文章

  1. fastboot 刷system.img 提示 sending 'system' (*KB)... FAILED (remote: data too large)

    华为G6-C00卡刷提示OEMSBL错误,只能线刷 ,但是官方找不到线刷img镜像,无奈 网上下了个可以线刷的工具套件 流氓ROM . 使用HuaweiUpdateExtractor(工具百度)把官方 ...

  2. 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 ...

  3. [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 ...

  4. [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 ...

  5. rdo(remote data objects) repo openstack icehouse

    problem making ssl connection Error: Cannot retrieve repository metadata (repomd.xml) for repository ...

  6. Js: Extensible Calendar Examples

    http://ext.ensible.comhttps://github.com/bmoeskau/Extensiblehttps://github.com/TeamupCom/extensibleh ...

  7. Jquery easyui 教程

            Jquery easyui教程                 目  录 1基本拖放... 4 2构建购物车型拖放... 5 3创建课程表... 8 4菜单和按钮Menu and Bu ...

  8. 基于 Angular Material 的 Data Grid 设计实现

    自 Extensions 组件库发布以来,Data Grid 成为了使用及咨询最多的组件.最开始 Data Grid 的设计非常简陋,经过一番重构,组件质量有了质的提升. Extensions 组件库 ...

  9. 【RDA】使用RDA(Remote Diagnostic Agent)工具对数据库进行健康检查

    [RDA]使用RDA(Remote Diagnostic Agent)工具对数据库进行健康检查 分类: Linux RDA英文全称叫做"Oracle Remote Diagnostic Ag ...

随机推荐

  1. PHP 运行方式(PHP SAPI介绍)

    SAPI:Server Application Programming Interface 服务器端应用编程端口.它就是PHP与其它应用交互的接口,PHP脚本要执行有很多种方式,通过Web服务器,或者 ...

  2. 20Mybatis_订单商品数据模型_一对一查询——resultType和resultMap两种方式以及两种方式的总结

    上一篇文章分析了数据模型,这篇文章就给出一个需求,这个需求是一对一查询,并完成这个需求. ------------------------------------------------------- ...

  3. Entity Framework4.0 (一)概述(EF4 的Database First方法)

    转自:http://www.cnblogs.com/marksun/archive/2011/12/15/2289582.html Entity Framework4.0(以后简称:EF4),是Mic ...

  4. R语言利器之ddply和aggregate

    ddply和aggregate是两个用来整合数据的功能强大的函数. aggregate(x, ...) 关于aggregate()函数的使用在<R语言实战>中P105有简单描述,这里重新说 ...

  5. 抓包排错-tcp.flags.reset

      一 排查思路: 1,了解协议运作过程 2,抓包 最小化原则 对比法 二 案例 微信连wifi问题: 不同地区的微信服务器的地址可能不同. 当出现认证问题: 1,不能跳转,点了按钮没反应 2,打开后 ...

  6. 2016科幻惊悚《第五波》HD720P.中英双字

    导演: J·布莱克森编剧: 苏珊娜·格兰特 / 阿齐瓦·高斯曼 / 杰夫·皮克纳 / 瑞克·杨西主演: 科洛·莫瑞兹 / 尼克·罗宾森 / 朗·里维斯顿 / 玛姬·丝弗 / 亚历克斯·罗伊 / 更多. ...

  7. Ubuntu Navicat for MySQL安装以及破解方案

    今天发现Navicat for MySQL有LINUX版本了哈, 开心的说,首先上官网上下载LINUX版本: http://www.navicat.com/download 1. 下载 navicat ...

  8. IOS开发之——登录加密也许用到的,反转字符串

    - (NSString *)stringByReversed{//    NSMutableString *s = [NSMutableString string];//    for (NSUInt ...

  9. 20145208 《Java程序设计》第9周学习总结

    20145208 <Java程序设计>第9周学习总结 教材学习内容总结 本周学习的内容有第十六周整合数据库,第十七章反射与类加载器,第十八章自定义泛型.枚举与注释. 在本周学习中,最大的难 ...

  10. 恢复windows 的快捷方式打开方法,亲测1-7恢复,

    相信有些用户曾试过错误地把LNK文件的打开方式更改其他文件,导致系统所有的快捷方式都失效.在vista与Windows7系统还不普遍使用的时候,相信大家会有点惊慌失措,不要紧,下面只要大家进行如下操作 ...