使用 Knockout JS 跟踪表中的更改

发布于 2025-01-04 13:57:57 字数 2274 浏览 0 评论 0原文

我在淘汰赛 js 方面遇到了一些问题。我正在尝试创建评论审核页面。 在 Web 服务中,我获取所有评论并将它们转换为 JSON。 结果 JSON 如下所示:

{"Comments":[{"Title":"Title-01","Body":"Body-01","Id":41,"IsDeleted":false},
{"Title":"Title-02","Body":"Body-02","Id":41,"IsDeleted":true},
{"Title":"Title-03","Body":"Body-03","Id":41,"IsDeleted":true},
{"Title":"Title-04","Body":"Body-04","Id":41,"IsDeleted":false},
...
]}

我的视图模型是:

    $(document).ready(function() {
        var moderationViewModel = function () {
            var self = this;
            this.Comments = ko.mapping.fromJS([]);
            this.GetComments = function () {
                $.ajax({
                  type: "POST",
                  url: "Web service url",
                  contentType: "application/json; charset=utf-8",
                  success: function (data) {
                    self.SuccessfullyRetrievedCommentsFromService(data);
                  }
                });
             };

             this.SuccessfullyRetrievedCommentsFromService = function (result) {
                ko.mapping.fromJS(result.Comments, self.Comments);
             };
       };

       var moderationCommentsViewModel = new moderationViewModel();
       moderationCommentsViewModel.GetComments();
       ko.applyBindings(moderationCommentsViewModel);  
});

表的标记是:

<table class="actable">
  <colgroup>
    <col width="575px" />
    <col width="25px" />
  </colgroup>
  <tbody>
    <!-- ko foreach: Comments -->
    <tr>
      <td>
        <h5><q data-bind="text: Title() + ' (' + Id() + ')'"></q></h5>
        <p data-bind="text: Body"></p>
      </td>
      <td class="tcenter">
        <input type="checkbox" data-bind="checked: IsDeleted">
      </td>
    </tr>
    <!-- /ko -->
  </tbody>
</table>

在服务器端,我有一个存储有关评论信息的数据库。编辑可以检查评论是否已删除,该评论不会向本侧访客显示。 我想以某种方式跟踪 html 表中已更改(选中/未选中)的行并将它们传递给 Web 服务。如果选中/取消选中评论复选框,我必须更改数据库表中有关其状态的信息。 我不想遍历整个 Comments 数组来查找在发送到服务器之前检查/取消检查哪些项目,因为表可以包含数千行,我认为这会影响性能。 有没有正确的方法可以使用knoit找出哪些行被更改?我的想法是创建单独的数组,并在复选框值更改时向其中添加/删除项目。但我不知道如何正确处理复选框的点击。是否可以?

I have some problems with knockout js. I'm trying to make page for comments moderation.
In web service I get all comments and convert them to JSON.
The result JSON looks like this:

{"Comments":[{"Title":"Title-01","Body":"Body-01","Id":41,"IsDeleted":false},
{"Title":"Title-02","Body":"Body-02","Id":41,"IsDeleted":true},
{"Title":"Title-03","Body":"Body-03","Id":41,"IsDeleted":true},
{"Title":"Title-04","Body":"Body-04","Id":41,"IsDeleted":false},
...
]}

My view model is:

    $(document).ready(function() {
        var moderationViewModel = function () {
            var self = this;
            this.Comments = ko.mapping.fromJS([]);
            this.GetComments = function () {
                $.ajax({
                  type: "POST",
                  url: "Web service url",
                  contentType: "application/json; charset=utf-8",
                  success: function (data) {
                    self.SuccessfullyRetrievedCommentsFromService(data);
                  }
                });
             };

             this.SuccessfullyRetrievedCommentsFromService = function (result) {
                ko.mapping.fromJS(result.Comments, self.Comments);
             };
       };

       var moderationCommentsViewModel = new moderationViewModel();
       moderationCommentsViewModel.GetComments();
       ko.applyBindings(moderationCommentsViewModel);  
});

And the markup of table is:

<table class="actable">
  <colgroup>
    <col width="575px" />
    <col width="25px" />
  </colgroup>
  <tbody>
    <!-- ko foreach: Comments -->
    <tr>
      <td>
        <h5><q data-bind="text: Title() + ' (' + Id() + ')'"></q></h5>
        <p data-bind="text: Body"></p>
      </td>
      <td class="tcenter">
        <input type="checkbox" data-bind="checked: IsDeleted">
      </td>
    </tr>
    <!-- /ko -->
  </tbody>
</table>

On the server side I have a database that stores info about comments. Editor may check comment as deleted and this comment will not show for the visitors of the side.
I want somehow to track rows in the html table that were changed (checked/unchecked) and pass these to the web service. If comments' checkbox were checked/unchecked I have to change info about it status int the database' table.
I do not want to iterate over through the whole Comments array to find which items were checked/unchecked before sending to server because table can contain thousands rows and I think it will be performance hit.
Is there right way to find out which rows were changed using knockoit? My thoughts were to create separate array and add/delete items to it when checkbox value was changed. But I do not know how to handle click on the checbox right way. Is it possible?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

十六岁半 2025-01-11 13:57:57

我在 NuGet 和 codeplex 上添加了 Knockout 的更改跟踪实用程序。您可以从这些链接之一获取它。 http://kochangetracker.codeplex.com/https://nuget.org/packages/Knockout.ChangeTracker

I added a change tracking utility for Knockout to NuGet and on codeplex. You can grab it from one of these links. http://kochangetracker.codeplex.com/ or https://nuget.org/packages/Knockout.ChangeTracker

残月升风 2025-01-11 13:57:57

我在 jsFiddle http://jsfiddle.net/AlfeG/vMU7z/3/ (不包括ajax部分)

在我看来,您唯一的错误与映射一致:

ko.mapping.fromJS(result.Comments, self.Comments);

映射插件的第二个参数是映射配置。所以正确的用法是:

ko.mapping.fromJS(result.Comments, {}, self.Comments);

另外 self.Comments 可以指定为 ko.observableArray()

<块引用>

但我不知道如何正确处理复选框上的点击。可能吗?

正如您在我提供的演示中看到的那样,KnockoutJS 正在跟踪对复选框的所有更改。因此您可以通过 viewModel 对象轻松访问数据。

I've have reproduced code on jsFiddle http://jsfiddle.net/AlfeG/vMU7z/3/ (excluding ajax part)

And it's seems to me that the only error You have is in line with mapping:

ko.mapping.fromJS(result.Comments, self.Comments);

Second parameter for mapping plugin is mapping configuration. So the correct use would be:

ko.mapping.fromJS(result.Comments, {}, self.Comments);

Also self.Comments can be specified as ko.observableArray()

But I do not know how to handle click on the checbox right way. Is it possible?

As You can see on the demo that I've provided KnockoutJS is tracing all changes to checkbox. So You can easily access data via viewModel object.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文