简体   繁体   English

淘汰赛js。 从表中获取元素并将其设置为当前元素。

[英]Knockout js. Get element from table and set it as current.

I have some trouble. 我有麻烦了 I write a web app with geo services. 我用地理服务编写了一个Web应用程序。 There is a ViewModel thar contains an observableCollection of 'Queuers' and the propery SelectedItem that represent a 'Queue' from that collection. 有一个ViewModel,其中包含一个可观察到的“ Queuers”集合和代表该集合中“ Queue”的属性SelectedItem。 The value of SelectedItem sets from table: 从表中设置的SelectedItem的值:

<tbody data-bind="foreach: Queuers">
                        <tr>
                            <td class="text-center">
                                <span data-bind="text: Number"></span>
                            </td>
                            <td class="text-center">
                                <i class="icon-flag icon-2x" data-bind="style: { color: Color }"></i>
                            </td>
                            <td class="text-center">
                                <span data-bind="text: Length"></span>
                            </td>
                            <td class="text-center">
                                <span data-bind="text: Resolution"></span>
                            </td>
                            <td class="text-center">
                                <button style="background: none; border: none" data-bind="click: $root.getData" @*onclick="$('#myModal').modal()"*@>
                                    <i class="icon-thumbs-up-alt icon-2x"></i>
                                </button>
                            </td>
                            <td class="text-center">
                                <button style="background: none; border: none" data-bind="click: $root.remove">
                                    <i class="icon-trash icon-2x"></i>
                                </button>
                            </td>
                        </tr>
                    </tbody>

and ViewModel: 和ViewModel:

 var Query = function (number, color, length, res, data) {

        this.Number = ko.observable(number);
        this.Color = ko.observable(color);
        this.Length = ko.observable(length);
        this.Resolution = ko.observable(res);
        this.Data = ko.observable(data);

    };

    function TwoDimViewModel() {
        var self = this;
        self.SelectedItem = ko.observable();
        self.SelectedColor = ko.observable(); //just for test
        self.Queuers = ko.observableArray();
        self.remove = function (q) {
            self.Queuers.remove(q);
        };
        self.getData = function (q) {
            self.SelectedItem = q;
            self.SelectedColor = q.Color(); //just for test
        self.addQ = function (q) {
            self.Queuers.push(q);
        };
        self.removeAll = function () {
            self.Queuers([]);
        };
    }

As you see, there is some logic to manipulate with ObservaleCollection. 如您所见,ObservaleCollection有一些逻辑可以操纵。 And all work perfect expect one: 所有完美的工作都期望一个:

self.getData = function (q) {
            self.SelectedItem = q; 
    }

I want for in my 我想要我的

<div class="row" data-bind="visible: Queuers().length >= 1">
                <button class="btn btn-default" onclick="clearAll()">Clear all</button>
                <br />
                Current selected id: <span data-bind="text: SelectedItem() ? SelectedItem().Number() : 'select element'"></span>
                <br />
                Выбран цвет: <span data-bind="text: SelectedColor() ?  SelectedColor: 'nulll'"></span>
            </div>

there will be current value of SelectedElement. 将会有SelectedElement的当前值。 And how to get access to is property (Number, Color, etc...) 以及如何访问的是属性(数字,颜色等)

Change: 更改:

self.SelectedItem = q;
self.SelectedColor = q.Color();

To: 至:

self.SelectedItem(q);
self.SelectedColor(q.Color());

Reference: http://knockoutjs.com/documentation/observables.html#observables 参考: http : //knockoutjs.com/documentation/observables.html#observables

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM