【问题标题】:How I can do delete row for KnockOut Paged Grid?如何删除 KnockOut Paged Grid 的行?
【发布时间】:2017-05-17 14:49:58
【问题描述】:

我的看法:

<div data-bind='simpleGrid: gridViewModel'> </div>

    <button data-bind='click: addItem'>
        Add item
    </button>

    <button data-bind='click: sortByName'>
        Sort by name
    </button>

    <button data-bind='click: jumpToFirstPage, enable: gridViewModel.currentPageIndex'>
        Jump to first page
    </button> 

我的视图模型:

var initialData = [
    { name: "Well-Travelled Kitten", sales: 352, price: 75.95 },
    { name: "Speedy Coyote", sales: 89, price: 190.00 },
    { name: "Furious Lizard", sales: 152, price: 25.00 },
    { name: "Indifferent Monkey", sales: 1, price: 99.95 },
    { name: "Brooding Dragon", sales: 0, price: 6350 },
    { name: "Ingenious Tadpole", sales: 39450, price: 0.35 },
    { name: "Optimistic Snail", sales: 420, price: 1.50 }
];

var PagedGridModel = function(items) {
    this.items = ko.observableArray(items);

    this.addItem = function() {
        this.items.push({ name: "New item", sales: 0, price: 100 });
    };

    this.sortByName = function() {
        this.items.sort(function(a, b) {
            return a.name < b.name ? -1 : 1;
        });
    };

    this.jumpToFirstPage = function() {
        this.gridViewModel.currentPageIndex(0);
    };

    this.gridViewModel = new ko.simpleGrid.viewModel({
        data: this.items,
        columns: [
            { headerText: "Item Name", rowText: "name" },
            { headerText: "Sales Count", rowText: "sales" },
            { headerText: "Price", rowText: function (item) { return "$" + item.price.toFixed(2) } }
        ],
        pageSize: 4
    });
};

ko.applyBindings(new PagedGridModel(initialData));

这是我的 jsFiddle:http://jsfiddle.net/rniemeyer/QSRBR/

我希望能够删除每一行。我想在每行删除操作结束时获取。我该怎么办?先感谢您。我在等你们的答案。

【问题讨论】:

  • 从数组中移除对象
  • 感谢拉杰什的回答。但是如何? :)

标签: javascript knockout.js grid


【解决方案1】:

剔除组件基于与其关联的数据。

要删除任何行,您只需操作与其关联的数组。

Sample JSFiddle.

注意

我刚刚使用array.shiftarray.pop 进行演示。您可以查看 array.splice 以删除其间的任何行。

【讨论】:

猜你喜欢
  • 2017-05-20
  • 2017-03-14
  • 2011-01-23
  • 1970-01-01
  • 2016-11-06
  • 2020-12-08
  • 1970-01-01
  • 2019-05-17
  • 1970-01-01
相关资源
最近更新 更多