首页 > 代码库 > 一个简单的knockout.js 和easyui的绑定
一个简单的knockout.js 和easyui的绑定
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Basic DataGrid - jQuery EasyUI Demo</title>
<link rel="stylesheet" type="text/css" href="http://www.mamicode.com/easyui.css">
<link rel="stylesheet" type="text/css" href="http://www.mamicode.com/icon.css">
<link rel="stylesheet" type="text/css" href="http://www.mamicode.com/demo.css">
<script type="text/javascript" src="http://www.mamicode.com/jquery.min.js"></script>
<script type="text/javascript" src="http://www.mamicode.com/jquery.easyui.min.js"></script>
<script type="text/javascript" src="http://www.mamicode.com/knockout-2.2.1.js"></script>
<script type="text/javascript">
//预编译函数, 可以定义在另一个js文件中
(function(){
//自定义绑定
ko.bindingHandlers.mydatagrid={
update:function (element, viewModelAccessor, allBindings) {
//得到当前绑定的model
var viewModel = viewModelAccessor();
$(element).datagrid({
url:viewModel.url,
columns:viewmodel.columns
})
}
}
})();
</script>
</head>
<body>
<h2>Basic DataGrid</h2>
<p>The DataGrid is created from markup, no JavaScript code needed.</p>
<div style="margin:20px 0;"></div>
<table id="tt" title="Basic DataGrid" style="width:700px;height:250px" data-bind="mydatagrid:grid">
</table>
<script type="text/javascript">
var viewmodel = function() {
this.grid={
url:‘mydata.json‘,
columns:[
{field:‘code‘,title:‘Code‘,width:100},
{field:‘name‘,title:‘Name‘,width:100},
{field:‘price‘,title:‘Price‘,width:100,align:‘right‘}
]
}
};
ko.applyBindings(new viewmodel());
<!--easyui的写法
$(‘#tt‘).datagrid({
url:‘mydata.json‘, <!--跨域请求 在chrome的快捷方式上添加 --allow-file-access-from-files -->
columns:[[
{field:‘code‘,title:‘Code‘,width:100},
{field:‘name‘,title:‘Name‘,width:100},
{field:‘price‘,title:‘Price‘,width:100,align:‘right‘}
]]
});
-->
</script>
</body>
</html>
一个简单的knockout.js 和easyui的绑定