淘汰赛可排序,带有两个列表和一个过滤数组 [英] Knockout Sortable with two lists and one filtered array

查看:112
本文介绍了淘汰赛可排序,带有两个列表和一个过滤数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用敲除可排序插件,以基于将一个敲除数组过滤为两个数组的两个html列表工作.我不断收到错误,我认为这可能与我返回的是计算的"而不是"observableArray"的对象有关,但我一直无法找到解决方案.

I'm attempting to use the knockout sortable plugin to work with two html lists based off of one knockout array filtered into two arrays. I keep getting errors and I believe it may have to do with the fact that I'm returning "computed" instead of "observableArray" objects, but I haven't been able to find a solution.

http://jsfiddle.net/thebassix/Eg2DG/4/

我相信问题的主要部分是:

I believe my main part of the problem is:

hiddenSeries = ko.computed(function() {
    {
        var seriesArray = self.series();
        return ko.utils.arrayFilter(seriesArray, function(item) {
            return item.Hidden();
        });
    }
});

unhiddenSeries = ko.computed(function() {
    {
        var seriesArray = self.series();
        return ko.utils.arrayFilter(seriesArray, function(item) {
            return !(item.Hidden());
        });
    }
});

推荐答案

可排序的插件需要使用observableArray进行操作,以便可以将其放回正确的位置.

The sortable plugin needs an observableArray to operate on, so that it can place the item back into the correct location.

您最好的选择是将数据分成两个observableArrays,然后创建一个计算值来表示组合的集合(如果需要将其发送回服务器).

Your best bet is probably to split the data into two observableArrays and then create a computed to represent the combined set (if you need to send it back to the server).

也许是这样的:

var data = ko.mapping.fromJSON('[{"__type":"mediacenter+Series","ID":1,"Name":"seriesname232","Hidden":true,"MediaCenterID":1,"_destroy":false},{"__type":"mediacenter+Series","ID":2,"Name":"kjhkuhkuh","Hidden":false,"MediaCenterID":1,"_destroy":false},{"__type":"mediacenter+Series","ID":3,"Name":"trrde","Hidden":false,"MediaCenterID":1,"_destroy":false},{"__type":"mediacenter+Series","ID":4,"Name":"1","Hidden":true,"MediaCenterID":1,"_destroy":false}]', mappingOptions);

//take one pass through the records and put them in the appropriate bucket
var hidden = [], 
    unhidden = [];

ko.utils.arrayForEach(data(), function(item) {
    if (item.Hidden()) {
       hidden.push(item);   
    }
    else {
       unhidden.push(item);   
    }
});

self.hiddenSeries = ko.observableArray(hidden);
self.unhiddenSeries = ko.observableArray(unhidden);

//define series as the two lists together
self.series = ko.computed(function() {
   return self.hiddenSeries().concat(self.unhiddenSeries()); 
});

示例: http://jsfiddle.net/rniemeyer/NhUEm/

这篇关于淘汰赛可排序,带有两个列表和一个过滤数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆