简体   繁体   English

对可观察数组中的项目进行更改后,如何立即看到更新?

[英]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.

相关问题 当选择下拉列表中的项目时,如何立即动态更新div? - How do I dynamically update a div instantly when an item in a dropdownlist is selected? 如何在敲除绑定处理程序中更新可观察数组? - How do i update observable array in knockout binding handler? 替换数组中的值后如何立即更新反应 state - How to instantly update react state after replacing the value from array 如何通过更改URL来检查网页的存在,请查看详细信息? - How do I check presence of web pages by making changes to a URL, please see details? 如何查看JSON更改的输出 - How Do I See the Output of Changes to JSON Vue3- [Provide and Inject] 当数据改变时,不做视觉更新,我使用 filter( ) 删除数组中的一个项目 - Vue3- [Provide and Inject] when data changes ,not do visual update ,I use filter( ) to del an item in array 如何为数组 map 中的单个项目更新 state? - How do I update the state for a single item in an array map? 如何添加一个下载按钮,以便在进行更改后下载图像? - How do I add a download button that will download the image after making changes? 我怎样才能使jquery.css不动画,而立即进行更改? - How can I make jquery.css not animate and instead do the changes instantly? 我应该如何将* .eslintrc *更新为ESLint以查看更改? - How should I update *.eslintrc* to ESLint see the changes?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM