gpt4 book ai didi

kendo-ui - listView 中的行未使用 Kendo MVVM 进行更新

转载 作者:行者123 更新时间:2023-12-02 09:11:43 28 4
gpt4 key购买 nike

我正在测试 Kendo 的 ui MVVM 功能,但似乎无法弄清楚如何更新 viewModel 项,以便它们更新 listView 中的行。

如果我用通过 websocket 发送给我的新(json)项目替换所有项目,它会更新得很好,但我试图简单地更新匹配的项目并期望看到这些更改出现在我的 View (listView div)中。

这是我的代码的摘录 - 我做错了什么?

<div id="listView">
<table>
<thead>
<tr>
<th>BatchID</th>
<th>Status</th>
<th>Progress</th>
<th>Owner</th>
<th>(Est)Completion Time</th>
</tr>
</thead>
<tbody data-template="row-template" data-bind="source: items"></tbody>
<tfoot data-template="footer-template" data-bind="source: this"></tfoot>
</table>
</div>

然后在我的脚本 block 中:

<script id="row-template" type="text/x-kendo-template">
<tr>
<td data-bind="text: BatchID"></td>
<td data-bind="text: Status"></td>
<td data-bind="text: Progress"></td>
<td data-bind="text: Owner"></td>
<td data-bind="text: EstCompletion"></td>
</tr>
</script>

<script id="footer-template" type="text/x-kendo-template">
<tr>
<td>
Batch count: #: total() #
</td>
</tr>
</script>

<script>

$(document).ready(function () {

var viewModel = kendo.observable({
items: [],
total: function () {
return this.get("items").length;
}
});

kendo.bind($("#listView"), viewModel);


// register this reporting widget
var webSocket = CreateSocket("screen2", "reporting", "default", true);

webSocket.onmessage = function (e) {
try {

var smd = JSON.parse(e.data);
var data = JSON.parse(smd.Data);

jQuery.each(data, function () {

var id = this.BatchID;
var newItem = this;

var hasRow = false;
jQuery.each(viewModel.items, function () {
var itemTemp = this;
if (itemTemp.BatchID == id) {
hasRow = true;
itemTemp.Status == newItem.Status;
itemTemp.Progress = newItem.Progress;
itemTemp.EstCompletion = newItem.EstCompletion;
itemTemp.Completed = newItem.Completed;
}
});

if (!hasRow) {
var reportItem = new kendo.data.ObservableObject({
BatchID: "",
Owner: "",
Status: "",
Progress: "",
EstCompletion: "",
Completed: ""
});

reportItem.BatchID = this.BatchID;
reportItem.Owner = this.Owner;
reportItem.Status = this.Status;
reportItem.Progress = this.Progress;
reportItem.EstCompletion = this.EstCompletion;
reportItem.Completed = this.Completed;

viewModel.items.push(reportItem);
}

});

}
catch (e) {
//alert(e.message);
}
};

webSocket.onopen = function (e) {

// now register our interest in receiving reporting monitoring data
var bor = new SymMsgUp(GlobalInfo.UserID, "reporting", "default", "dfregister");

// convert to Json and send it off the the server
webSocket.send(bor.toServer());
};

webSocket.onclose = function (e) {
//responseDiv.innerHTML += '<div>Disconnected.</div>';
};

webSocket.onerror = function (e) {
//responseDiv.innerHTML += '<div>Error</div>'
};



});

</script>

发生更新(我用调试器检查) - 但这些更改未反射(reflect)在 View 中..

另外,我创建了 var reportItem = new kendo.data.ObservableObject({ etc 等用于测试 - 我宁愿不必这样做,只需将 json 对象数组转储到我的可观察集合 (viewMode.Items) 中。工作得很好,但当我更新单个项目时,它又不会更新......

提前致谢!

最佳答案

刚刚收到 Kendo 的答复(近一周后,因为我使用的是试用许可证......),说明如何完成此操作:

$(function(){
var viewModel = kendo.observable({
products: [
{ name: "Hampton Sofa", price: 989.99, unitsInStock: 39 },
{ name: "Perry Sofa", price: 559.99, unitsInStock: 17 },
{ name: "Donovan Sofa", price: 719.99, unitsInStock: 29 },
{ name: "Markus Sofa", price: 839.99, unitsInStock: 3 }
],
newPrice: 0,
itemIndex: 0,
setPrice: function(e){
this.products[this.itemIndex].set("price", this.newPrice);
}
});

kendo.bind($("body"), viewModel);

});

编辑:只是为了解释一下,实现这一点的行是:

this.products[this.itemIndex].set("price", this.newPrice);

这就是我所追求的 - 一种更新可观察集合中条目的方法,该条目确实更新了 ui。

关于kendo-ui - listView 中的行未使用 Kendo MVVM 进行更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14092516/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com