[英]How do I instantly see an update after making changes to an item in an observable array?
I am attempting to edit the content of a table cell, I do not want to edit the value directly in the table, instead I need to use a textarea, enter some text and hit a button to update the table cell. 我正在尝试编辑表格单元格的内容,我不想直接在表格中编辑值,而是需要使用textarea,输入一些文本并单击按钮来更新表格单元格。
I tried doing this using JQuery
and updating the Observable Array
directly, however, I do not see the changes until I switch between the Select Options items. 我尝试使用
JQuery
进行此操作并直接更新Observable Array
,但是,直到在Select Options项之间进行切换,我才能看到更改。
Here is a JSFiddle showing the complete example mentioned above. 这是显示上述完整示例的JSFiddle 。
How do I write this so that I can see the changes instantly after making changes to the Observable Array item? 如何编写此代码,以便对Observable Array项进行更改后可以立即看到更改?
Your Bio property isn't observable, hence why it doesn't update. 您的生物财产不可观察,因此为什么它不会更新。 I've made some small changes to make your Bio observable and therefore update when you click the button rather than when you reload the artist https://jsfiddle.net/jpntrx41/ .
我进行了一些小更改,使您的Bio可见,因此在您单击按钮时更新,而不是在重新加载艺术家https://jsfiddle.net/jpntrx41/时更新 。 The changes are in the following areas:
更改包括以下几个方面:
self.artistDetail = ko.observableArray([{
"ArtistId": "1",
"Bio": ko.observable("Jon Secada is a Cuban American singer and songwriter. " +
"Secada was born in Havana, Cuba, and raised in Hialeah, Florida. " +
"He has won two Grammy Awards and sold 20 million albums since his " +
"English-language debut album in 1992")
},
{
"ArtistId": "2",
"Bio": ko.observable("Céline Marie Claudette Dion, CC OQ ChLD is a Canadian " +
"singer, songwriter, businesswoman and occasional actress.")
}
]);
$(function() {
$('#update').click(function(event) {
var updateText = $('#update-bio').val();
if (viewModelA.SelectedArtist()) {
var currText = viewModelB.artistDetail()[viewModelA.SelectedArtist().value - 1].Bio();
viewModelB.artistDetail()[viewModelA.SelectedArtist().value - 1].Bio().replace(currText,
viewModelB.artistDetail()[viewModelA.SelectedArtist().value - 1].Bio(updateText));
}
});
});
While the above meets the requirement of automatically updating the Bio when you click the button, I'm a bit confused by your code structure and mix of Knockout and jQuery. 尽管上面的代码满足您单击按钮时自动更新Bio的要求,但您的代码结构以及Knockout和jQuery的混合让我有些困惑。 Obviously I don't know the bigger picture of what it is exactly that you're trying to do but it's worth reviewing your structure to see if you can simplify it a bit.
显然,我不知道您要尝试执行的操作的全局图,但是值得回顾一下结构,看看是否可以简化一下。
Here's a really quick working example running it all from 1 view model with Knockout http://plnkr.co/edit/3UYAQjJmbdyn1rYxZj9m?p=preview 这是一个非常快速的工作示例,它使用Knockout从1个视图模型中运行所有操作http://plnkr.co/edit/3UYAQjJmbdyn1rYxZj9m?p=preview
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.