Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在淘汰中使用下拉列表进行筛选_Javascript_Jquery_Knockout.js - Fatal编程技术网

Javascript 如何在淘汰中使用下拉列表进行筛选

Javascript 如何在淘汰中使用下拉列表进行筛选,javascript,jquery,knockout.js,Javascript,Jquery,Knockout.js,我刚刚开始使用Knockout,我想通过从下拉列表中选择一个项目来过滤我在UI中显示的数据。到目前为止,我已经得到了,但是我还不能从下拉列表中获得所选的值,然后我需要根据该值实际过滤显示的数据。以下是我目前的代码: @model Models.Fixture @{ ViewBag.Title = "Fixtures"; Layout = "~/Areas/Development/Views/Shared/_Layout.cshtml"; } @Scripts.Rende

我刚刚开始使用Knockout,我想通过从下拉列表中选择一个项目来过滤我在UI中显示的数据。到目前为止,我已经得到了,但是我还不能从下拉列表中获得所选的值,然后我需要根据该值实际过滤显示的数据。以下是我目前的代码:

    @model Models.Fixture

@{
    ViewBag.Title = "Fixtures";
    Layout = "~/Areas/Development/Views/Shared/_Layout.cshtml";
}
@Scripts.Render("~/bundles/jqueryval")
<script type="text/javascript" src="@Url.Content("~/Scripts/knockout-2.1.0.js")"></script>
<script type="text/javascript">

    function FixturesViewModel() {
            var self = this;
            var baseUri = '@ViewBag.ApiUrl';
            self.fixtures = ko.observableArray();
            self.teams = ko.observableArray();

            self.update = function (fixture) {

                $.ajax({ type: "PUT", url: baseUri + '/' + fixture.Id, data: fixture });
            };

            self.sortByAwayTeamScore = function () {
                this.fixtures.sort(function(a, b) 
                { return a.AwayTeamScore < b.AwayTeamScore ? -1 : 1; });
            };

            self.select = function (team) {

            };

            $.getJSON("/api/fixture", self.fixtures);
            $.getJSON("/api/team", self.teams);
    }

    $(document).ready(function () {
        ko.applyBindings(new FixturesViewModel());
    }); 

  </script>

<div class="content">
    <div>
        <table><tr><td><select data-bind="options: teams, optionsText: 'TeamName', optionsCaption: 'Select...', optionsValue: 'TeamId', click: $root.select"> </select></td></tr></table>
        <table class="details ui-widget-content">
            <thead>
                <tr><td>FixtureId</td><td>Season</td><td>Week</td><td>AwayTeam</td><td><a id="header" data-bind='click: sortByAwayTeamScore'>AwayTeamScore</a></td><td>HomeTeam</td><td>HomeTeamScore</td></tr>
            </thead>    
            <tbody data-bind="foreach: fixtures">
                <tr>
                    <td><span data-bind="text: $data.Id"></span></td>
                    <td><span data-bind="text: $data.Season"></span></td>
                    <td><span data-bind="text: $data.Week"></span></td>
                    <td><span data-bind="text: $data.AwayTeamName"></span></td>
                    <td><input type="text" data-bind="value: $data.AwayTeamScore"/></td>
                    <td><span data-bind="text: $data.HomeTeamName"></span></td>
                    <td><input type="text" data-bind="value: $data.HomeTeamScore"/></td>
                    <td><input type="button" value="Update" data-bind="click: $root.update"/></td>
                </tr>
            </tbody>
        </table>
    </div>
</div>
编辑:解决了这个问题:

    <script type="text/javascript">

function FixturesViewModel() {
        var self = this;
        var baseUri = '@ViewBag.ApiUrl';
        self.fixtures = ko.observableArray();

        self.teams = ko.observableArray();
        self.TeamName = ko.observable('');

        self.filteredItems = ko.computed(function () {

            var TeamName = self.TeamName();
                if (!TeamName || TeamName == "None") {

                            return self.fixtures();
                        } else {
                            return ko.utils.arrayFilter(self.fixtures(), function (i) {
                                return i.AwayTeamName == TeamName;
                            });
                }
        });

        self.update = function (fixture) {
            $.ajax({ type: "PUT", url: baseUri + '/' + fixture.Id, data: fixture });
        };

        self.sortByAwayTeamScore = function () {
            this.fixtures.sort(function(a, b) 
            { return a.AwayTeamScore < b.AwayTeamScore ? -1 : 1; });
        };

        $.getJSON("/api/fixture", self.fixtures);
        $.getJSON("/api/team", self.teams);
}

$(document).ready(function () {
    ko.applyBindings(new FixturesViewModel());
}); 

敲除中的过滤通常是使用。下面是一个基本的ViewModel,它可以根据类型下拉列表进行过滤,包括“无”的过滤选项

var ViewModel = function(data) {
    var self = this;
    self.filters = ko.observableArray(data.filters);
    self.filter = ko.observable('');
    self.items = ko.observableArray(data.items);
    self.filteredItems = ko.computed(function() {
        var filter = self.filter();
        if (!filter || filter == "None") {
            return self.items();
        } else {
            return ko.utils.arrayFilter(self.items(), function(i) {
                return i.type == filter;
            });
        }
    });
};

这是a中的代码,因此您可以使用它。

在淘汰中过滤通常是使用a完成的。下面是一个基本的ViewModel,它可以根据类型下拉列表进行过滤,包括“无”的过滤选项

var ViewModel = function(data) {
    var self = this;
    self.filters = ko.observableArray(data.filters);
    self.filter = ko.observable('');
    self.items = ko.observableArray(data.items);
    self.filteredItems = ko.computed(function() {
        var filter = self.filter();
        if (!filter || filter == "None") {
            return self.items();
        } else {
            return ko.utils.arrayFilter(self.items(), function(i) {
                return i.type == filter;
            });
        }
    });
};
这是a中的代码,您可以使用它