javascript - 剑道用户界面 : Unable to bind data to list view using MVVM

标签 javascript mvvm kendo-ui

我是 kendo ui mvvm 新手,面临以下问题:

场景 我需要使用 MVVM 格式填充 Angular 色为 ListView 的 div 中的几个字段。

数据来自数据源,我收到一个不寻常的错误。我无法将数据源中的字段绑定(bind)到 div。

以下是我的 JSBin 示例:http://jsbin.com/ajoyug/6/edit

HTML:

<!DOCTYPE html>
<html>
<head>
<link href="http://cdn.kendostatic.com/2013.1.319/styles/kendo.common.min.css" rel="stylesheet" type="text/css" />
<link href="http://cdn.kendostatic.com/2013.1.319/styles/kendo.rtl.min.css" rel="stylesheet" type="text/css" />
<link href="http://cdn.kendostatic.com/2013.1.319/styles/kendo.default.min.css" rel="stylesheet" type="text/css" />
<link href="http://cdn.kendostatic.com/2013.1.319/styles/kendo.dataviz.min.css" rel="stylesheet" type="text/css" />
<link href="http://cdn.kendostatic.com/2013.1.319/styles/kendo.dataviz.default.min.css" rel="stylesheet" type="text/css" />
<link href="http://cdn.kendostatic.com/2013.1.319/styles/kendo.mobile.all.min.css" rel="stylesheet" type="text/css" />
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script src="http://cdn.kendostatic.com/2013.1.319/js/kendo.all.min.js"></script>
<meta charset=utf-8 />
<title>JS Bin</title>
</head>
<body>
  <div id="myListView" data-role="listView" data-bind="source:dataSource">
    <span data-bind="text:prodName"></span>
    <span data-bind="text:qty"></span>
    <span data-bind="text:total"></span>
    <span data-bind="text:price"></span>
  </div>
</body>
</html>

JavaScript:

$(document).ready(function(){
var data = [
    {
    "Id":1,
    "img":"../public/images/product/shoes.png",
    "code":"00021543",
    "fullProdName":"Jimmy Choo - Simone Pump Shoes",
    "prodName":"Simone Pump Shoes",
    "description":"A perfect Red carpet companion. Jimmy Choo shoes spells success and glamour. Be the talk of the town...",
    "price":"1500.0",
    "total":"1500.0",
    "qty":"1",
    "discount":"0.00",
    "brand":"Jimmy Choo",
    "category":"Shoes",
    "points":"100",
    "tax":"0.00" }
];

  var dataSource = new kendo.data.DataSource({
    data: data, 
    pagesize: 10,
    schema: {
      model: {
        id: "Id",
        fields: {
          prodName: { editable: false},
          qty: { editable: true},
          price: { editable: false},
          total : {editable :false}
        }
      }
    }
  });

dataSource.read();

  var listViewModel = kendo.observable({
    dataSource:dataSource
  });
  kendo.bind($("#myListView"), listViewModel);
});

请帮帮我。我在网上看到了很多可用的示例,但是他们使用模板来绑定(bind)多个值,或者它们不适合我的要求。因此我想到创建自己的 JSBin 示例并询问我在哪里陷入困境...

问题 我该如何绑定(bind)数据源中的字段?

我的最终动机是将div与数据源中的值绑定(bind)。如果不将其设置为 ListView ,还有其他方法可以做到这一点吗?

谢谢!!

哈迪克

最佳答案

你的 JavaScript 看起来不错。不过,您的 HTML 存在一些问题。 data-role 属性需要是“listview”。您实际上应该使用模板,并通过 ID 引用它,而不是将 4 个跨度放入 ListView div 中。

还需要注意的是,您的模板必须有一个根元素,因为 kendo 仅对模板中的第一个元素执行绑定(bind)。

HTML:

<!DOCTYPE html>
<html>
<head>
<link href="http://cdn.kendostatic.com/2013.1.319/styles/kendo.common.min.css" rel="stylesheet" type="text/css" />
<link href="http://cdn.kendostatic.com/2013.1.319/styles/kendo.rtl.min.css" rel="stylesheet" type="text/css" />
<link href="http://cdn.kendostatic.com/2013.1.319/styles/kendo.default.min.css" rel="stylesheet" type="text/css" />
<link href="http://cdn.kendostatic.com/2013.1.319/styles/kendo.dataviz.min.css" rel="stylesheet" type="text/css" />
<link href="http://cdn.kendostatic.com/2013.1.319/styles/kendo.dataviz.default.min.css" rel="stylesheet" type="text/css" />
<link href="http://cdn.kendostatic.com/2013.1.319/styles/kendo.mobile.all.min.css" rel="stylesheet" type="text/css" />
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script src="http://cdn.kendostatic.com/2013.1.319/js/kendo.all.min.js"></script>
<script id="tmp" type="text/x-kendo-template">
  <div>
    <span data-bind="text:prodName"></span><br/>
    <span data-bind="text:qty"></span><br/>
    <span data-bind="text:total"></span><br/>
    <span data-bind="text:price"></span>
  </div>
</script>
<meta charset=utf-8 />
<title>JS Bin</title>
</head>
<body>
  <div
    id="myListView"
    data-role="listview"
    data-bind="source: dataSource"
    data-template="tmp">
  </div>
</body>
</html>

JavaScript:

$(document).ready(function(){
var data = [
    {
    "Id":1,
    "img":"../public/images/product/shoes.png",
    "code":"00021543",
    "fullProdName":"Jimmy Choo - Simone Pump Shoes",
    "prodName":"Simone Pump Shoes",
    "description":"A perfect Red carpet companion. Jimmy Choo shoes spells success and glamour. Be the talk of the town...",
    "price":"1500.0",
    "total":"1500.0",
    "qty":"1",
    "discount":"0.00",
    "brand":"Jimmy Choo",
    "category":"Shoes",
    "points":"100",
    "tax":"0.00" }
];

  var dataSource = new kendo.data.DataSource({
    data: data, 
    pagesize: 10,
    schema: {
      model: {
        id: "Id",
        fields: {
          prodName: { editable: false},
          qty: { editable: true},
          price: { editable: false},
          total : {editable :false}
        }
      }
    }
  });

  var listViewModel = kendo.observable({
    dataSource:dataSource
  });
  kendo.bind($("#myListView"), listViewModel);
});

关于javascript - 剑道用户界面 : Unable to bind data to list view using MVVM,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16097878/

相关文章:

c# - 通过 MVVM 命令访问集合

javascript - kendoListBox 不是函数

javascript - Chrome - 离线页面

javascript - 转到单词突出显示的位置

silverlight - 多外壳应用程序?

kendo-ui - 我想在 Kendo UI Grid 上显示应用的过滤条件

javascript - Kendo UI TreeView 可选根

javascript - 将值保存到 Chrome 扩展中的文本框

javascript - 将复选框选中的值传递给脚本变量

wpf - 刷 MVVM 绑定(bind)不给命名颜色