KnockoutJs 参数化 dependentObservable 数组

发布于 2025-01-01 15:52:32 字数 804 浏览 1 评论 0原文

我有一个 viewModel 和一个从服务器更新的 observableArray,然后我需要能够为该 observableArray< 的过滤器定义动态数量的 div /em>。

这是一个 Scrum 板示例,想象一下您从服务器收到一系列故事。然后将其添加到 viewModel.stories() observableArray 中。

我希望为可观察数组中每个故事的属性的各种不同值提供模板绑定过滤器。

因此,鉴于

item.BoardState 是“Backlog”或“In Progress”

我想要一个依赖的可观察量,我可以将其参数化为仅显示“In Progress”的故事

    self.filterInProgress = ko.dependentObservable(function (filterParameter) {
    return ko.utils.arrayFilter(self.stories(), function (item) {
        console.log("Current Filter = " + filterParameter + "--- Current BoardState = " + item.BoardState);
        return ((item.BoardState === filterParameter));
    });
});

不幸的是,它说它不起作用。任何建议都非常感激。

I have a viewModel with an observableArray that gets updated from the server, then I need to be able to define a dynamic number of divs to filters of that observableArray.

It's a scrum board example, So imagine you receive an array of stories back from the server. and you add the to viewModel.stories() observableArray.

I'd like to have template bound filters for various different values of a property of each story within the observable array.

So given

item.BoardState is "Backlog" or "In Progress"

I want a dependent observable that I can paramatize to only show stories that are "In Progress"

    self.filterInProgress = ko.dependentObservable(function (filterParameter) {
    return ko.utils.arrayFilter(self.stories(), function (item) {
        console.log("Current Filter = " + filterParameter + "--- Current BoardState = " + item.BoardState);
        return ((item.BoardState === filterParameter));
    });
});

Unfortunately it says it doesn't work. any suggestions greatly appreciated.

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

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

发布评论

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

评论(3

面如桃花 2025-01-08 15:52:32

您始终可以将过滤器移出到单独的函数中,并为每个过滤器类型创建一个 dependentObservable:

function filterStories(stories, filterParameter) {
    return ko.utils.arrayFilter(stories, function (item) {
        console.log("Current Filter = " + filterParameter + "--- Current BoardState = " + item.BoardState);
        return ((item.BoardState === filterParameter));
    });
}

self.filterInProgress = ko.dependentObservale(function() {
    return filterStories(self.stories(), "InProgress");
});

self.filterBacklog = ko.dependentObservale(function() {
    return filterStories(self.stories(), "Backlog");
});

You can always move the filter out to a separate function and create a dependentObservable for each filterType:

function filterStories(stories, filterParameter) {
    return ko.utils.arrayFilter(stories, function (item) {
        console.log("Current Filter = " + filterParameter + "--- Current BoardState = " + item.BoardState);
        return ((item.BoardState === filterParameter));
    });
}

self.filterInProgress = ko.dependentObservale(function() {
    return filterStories(self.stories(), "InProgress");
});

self.filterBacklog = ko.dependentObservale(function() {
    return filterStories(self.stories(), "Backlog");
});
苯莒 2025-01-08 15:52:32

将参数移至视图模型并使用自定义绑定。所以你不会有任何问题。

看看这个例子,是我前段时间和我的大学讨论这个问题时诞生的。

演示 - http://jsbin.com/epojol/5

带预览的代码 - http://jsbin.com/epojol/5/edit#javascript,html,live

自定义绑定来自演示:

ko.bindingHandlers.rangeVisible = {
            update: function (element, valueAccessor, allBindings) {
                var selectedValue = ko.utils.unwrapObservable(valueAccessor());
                var itemRange = allBindings().range;

                if (selectedValue < itemRange.max && selectedValue >= itemRange.min)
                    $(element).show("slow");
                else
                    $(element).hide("slow");
            }
        };

绑定html:

<div class="plan red" data-bind="rangeVisible: selected, range: {min: 0, max:10}">

Move parameter to View-Model and use custom bindings. So You will have no problem with it.

Look for this example that were born while I discuss this question with my college some time ago.

Demo - http://jsbin.com/epojol/5

Code with preview - http://jsbin.com/epojol/5/edit#javascript,html,live

Custom binding from demo:

ko.bindingHandlers.rangeVisible = {
            update: function (element, valueAccessor, allBindings) {
                var selectedValue = ko.utils.unwrapObservable(valueAccessor());
                var itemRange = allBindings().range;

                if (selectedValue < itemRange.max && selectedValue >= itemRange.min)
                    $(element).show("slow");
                else
                    $(element).hide("slow");
            }
        };

Binding in html:

<div class="plan red" data-bind="rangeVisible: selected, range: {min: 0, max:10}">
稚然 2025-01-08 15:52:32

我会这样写:

View:

<div data-bind="foreach: filterInProgress">
    <h1 data-bind="text: Id"></h1>
</div>

ViewModel:

var viewModel = {

    stories: ko.observableArray(
        [
            {
                'Id': 0,
                'BoardState': 'In Progress'
            },
            {
                'Id': 1,
                'BoardState': 'Backlog'
            },
            {
                'Id': 2,
                'BoardState': 'In Progress'
            }            
        ]
    )
};

viewModel.filterInProgress = ko.computed(
    function() {
        return ko.utils.arrayFilter(viewModel.stories(), function(item) {
            return item.BoardState == 'In Progress';
        });
    }
)

ko.applyBindings(viewModel);

EDIT: now works as an ko.dependentObservable (ko.compulated in 2.0)

I would write it like this:

View:

<div data-bind="foreach: filterInProgress">
    <h1 data-bind="text: Id"></h1>
</div>

ViewModel:

var viewModel = {

    stories: ko.observableArray(
        [
            {
                'Id': 0,
                'BoardState': 'In Progress'
            },
            {
                'Id': 1,
                'BoardState': 'Backlog'
            },
            {
                'Id': 2,
                'BoardState': 'In Progress'
            }            
        ]
    )
};

viewModel.filterInProgress = ko.computed(
    function() {
        return ko.utils.arrayFilter(viewModel.stories(), function(item) {
            return item.BoardState == 'In Progress';
        });
    }
)

ko.applyBindings(viewModel);

EDIT: now works as an ko.dependentObservable (ko.computed in 2.0)

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