knockout.js 如何在knockout中的“with”绑定中实现分页

tcomlyy6  于 2022-11-10  发布在  其他
关注(0)|答案(2)|浏览(232)

我的项目中有一个需求,我需要将我选择的项目放在模态中,用户可以单击“下一步”显示下一个项目。
我正在使用具有绑定的来显示表单中选定的内容。我不知道如何在“具有”绑定中应用分页。

<div class="container" data-bind="with: itemForEditing">
 <div id="riskRegisterForm" class="modal hide fade">
    <div class="modal-header" style="background:#4bafef; height: 30px;">            
        <h5 style="color:#FFFFFF; font:16px Arial;">Item</h5>
    </div>
    <div class="modal-body" style="background:#fff">
        <div>
            <form class="form-horizontal">
              <div class="control-group">
                <label class="control-label" for="itemName">Name</label>
                <div class="controls">
                    <input type="text" id="itemName" data-bind="value: name" />
                </div>
              </div>
              <div class="control-group">
                <label class="control-label" for="itemPrice">Price</label>
                <div class="controls">
                    <input type="number" step=".01" id="itemPrice" data-bind="value: price" />
                </div>
              </div>                    
            </form>
        </div>
    </div>
    <div class="modal-footer">
        <button type="button" data-dismiss="modal" class="btn" data-bind="click:$parent.revertItem">Cancel</button>
        <button type="button" data-dismiss="modal" class="btn" data-bind="click:$parent.acceptItem">Update</button>            
    </div>
    <span><a href=#>next</a></span>
    <span><a href=#>prev</a></span>
 </div>
 </div>

当我点击下一个时,它应该自动选择下一个记录并放入控件中。

isr3a4wc

isr3a4wc1#

我已经尝试过了。我改变了我的策略。基本上我创建了2个方法,一个next和一个prev。在这些方法中,我找到了数组中所选/可编辑对象的索引,以及基于所用方法的增量或减量。然后,我更新了所选和可编辑属性对象:

var Item = function(data) {
    this.name = ko.observable();
    this.price = ko.observable();

    //populate our model with the initial data
    this.update(data);
};

//can pass fresh data to this function at anytime to apply updates or revert to a prior version
Item.prototype.update = function(data) { 
    this.name(data.name || "new item");
    this.price(data.price || 0);
};

var ViewModel = function(items) {
    this.index = 0;

    //turn the raw items into Item objects
    this.items = ko.observableArray(ko.utils.arrayMap(items, function(data) {
        return new Item(data);
    }));

    //hold the currently selected item
    this.selectedItem = ko.observable();

    //make edits to a copy
    this.itemForEditing = ko.observable();

    this.selectItem = this.selectItem.bind(this);
    this.acceptItem = this.acceptItem.bind(this);
    this.revertItem = this.revertItem.bind(this);
    this.next = this.next.bind(this);
    this.prev = this.prev.bind(this);
};

ko.utils.extend(ViewModel.prototype, {
    //select an item and make a copy of it for editing
    selectItem: function(item) {
        this.selectedItem(item);
        this.itemForEditing(new Item(ko.toJS(item)));
    },

    next:function(){
        var pos = this.items.indexOf(this.selectedItem()) + 1;
        if(pos > this.items().length - 1){pos = 0};

        this.selectedItem(this.items()[pos]);
        this.itemForEditing(new Item(ko.toJS(this.items()[pos])));
    },

    prev:function(){
        var pos = this.items.indexOf(this.selectedItem()) - 1;
        if(pos < 0){pos = this.items().length - 1};

        this.selectedItem(this.items()[pos]);
        this.itemForEditing(new Item(ko.toJS(this.items()[pos])));
    },

    acceptItem: function(item) {
        var selected = this.selectedItem(),
            edited = ko.toJS(this.itemForEditing()); //clean copy of edited

        //apply updates from the edited item to the selected item
        selected.update(edited);

        //clear selected item
        this.selectedItem(null);
        this.itemForEditing(null);
    },

    //just throw away the edited item and clear the selected observables
    revertItem: function() {
        this.selectedItem(null);
        this.itemForEditing(null);
    }
});

ko.applyBindings(new ViewModel([
    { name: "Cheese", price: 2.50 },
    { name: "Pepperoni", price: 3.25 },
    { name: "Deluxe", price: 4.25 }
]));

这里有一个链接到我的JS Fiddle

uinbv5nw

uinbv5nw2#

一种方法是这样做的:

<div class="container" data-bind="with: itemForEditing">
    <!-- ... -->
    <span><a href=# data-bind="click: $root.nextItem">next</a></span>
    <span><a href=# data-bind="click: $root.prevItem">prev</a></span>
</div>

ko.utils.extend(ViewModel.prototype, {
    // offset the selected item by a certain amount (i.e. -1/+1 for next/prev)
    offsetItem: function (by) {
        var items = this.items(),
            i = ko.utils.arrayIndexOf(items, this.selectedItem()),
            newItem = (i > -1) ? items[i + by] : null;

        if (newItem) {
            this.selectItem(newItem);
        }
    },
    prevItem: function () {
        this.offsetItem(-1);
    },
    nextItem: function () {
        this.offsetItem(1);
    },
    /* ... */
}

观看现场直播http://jsfiddle.net/Tt96J/11/

相关问题