javascript 从选定的表行 Knockout.js 中获取值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21412696/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 20:52:17  来源:igfitidea点击:

Get value from selected Table row Knockout.js

javascriptjqueryknockout.js

提问by user3242446

I need a little help.

我需要一点帮助。

I have created a table that gets values from JSON response, but for this example lets create a hardcoded html table like following:

我创建了一个从 JSON 响应中获取值的表,但是对于这个例子,让我们创建一个硬编码的 html 表,如下所示:

<table id="devtable">
    <tr>
        <th>ID</th>
        <th>Name</th>
        <th>Status</th>
    </tr>
    <tr>
        <td>001</td>
        <td>Jhon</td>
        <td>Single</td>
    </tr>
    <tr>
        <td>002</td>
        <td>Mike</td>
        <td>Married</td>
    </tr>
    <tr>
        <td>003</td>
        <td>Marrie</td>
        <td>Complicated</td>
    </tr>
</table>
ID : <input type="text" name="ID" data-bind="value: ID" disabled/>
<br>
Name : <input type="text" name="Name" data-bind="value: Name" disabled/>
<br>
Status : <input type="text" name="Status" data-bind="value: Status" disabled/>
<br>
<input type="button" value="Send" disabled/>

Requirement is: when I select a row of table, values of columns goes to the input boxes and enable button as well. As I am trying to learn Knockout.js by doing this exercise. I think I have to make a viewmodel like this:

要求是:当我选择一行表格时,列的值也会进入输入框和启用按钮。因为我正在尝试通过做这个练习来学习 Knockout.js。我想我必须制作这样的视图模型:

var rowModel = function (id, name, status) {
    this.ID = ko.observable(id);
    this.Name = ko.observable(name);
    this.Status = ko.observable(status);
}

Link of project is here: http://jsfiddle.net/qWmat/

项目链接在这里:http: //jsfiddle.net/qWmat/

回答by Matt Burland

Here's an example of how you could do it:

这是一个如何做到的示例:

http://jsfiddle.net/qWmat/3/

http://jsfiddle.net/qWmat/3/

function MyVM(data) {
    var self = this;

    self.items = ko.observableArray(data.map(function (i) {
        return new rowModel(i.id, i.name, i.status);
    }));

    self.select = function(item) {
        self.selected(item);
    }; 

    self.selected = ko.observable(self.items()[0]);
} 

And you bind your textboxes to the properties in the selectedproperty:

然后将文本框绑定到属性中的selected属性:

<input type="text" name="ID" data-bind="value: selected().ID" disabled/>

And you bind the click handler in your trlike so:

然后tr像这样绑定点击处理程序:

<tr data-bind="click: $parent.select">

Updated to include enable binding (http://jsfiddle.net/qWmat/8/). Add a property for whether or not to edit:

更新以包含启用绑定 ( http://jsfiddle.net/qWmat/8/)。添加是否编辑的属性:

self.enableEdit = ko.observable(false);

Then update your selectfunction to turn it to true:

然后更新您的select函数以将其变为真:

self.select = function(item) {
    self.selected(item);
    self.enableEdit(true);
};

If / when you save or cancel you could the set it back to false if you want.

如果/当您保存或取消时,您可以根据需要将其设置回 false。

Update your bindings on the input boxes:

更新输入框上的绑定:

<input type="text" name="Status" data-bind="value: selected().Status, enable: enableEdit" />

回答by alexmac

I've created a demo for you, but to know how it works, you should investigate knockout documentation.

我已经为您创建了一个演示,但要了解它是如何工作的,您应该调查淘汰文档。

ViewModel:

视图模型:

<table id="devtable">
    <thead>
        <tr>
            <th>ID</th>
            <th>Name</th>
            <th>Status</th>
        </tr>
    </thead>
    <tbody data-bind="foreach: Items" > 
        <tr data-bind='click: $parent.setEditItem'>
            <td data-bind="text: ID"></td>
            <td data-bind="text: Name"></td>
            <td data-bind="text: Status"></td>
        </tr>
    </tbody>
</table>

<!-- ko with: SelectedItem -->
ID :
<input type="text" name="ID" data-bind="value: ID, attr: {disabled: !$parent.IsEditMode()}" />
<br>Name :
<input type="text" name="Name" data-bind="value: Name, attr: {disabled: !$parent.IsEditMode()}"/>
<br>Status :
<input type="text" name="Status" data-bind="value: Status, attr: {disabled: !$parent.IsEditMode()}"/>
<br>
<input type="button" value="Send" data-bind="attr: {disabled: !$parent.IsEditMode()}"/>
<!-- /ko -->

Html:

网址:

function ItemModel(id, name, status) {
    var self = this;

    self.ID = ko.observable(id);
    self.Name = ko.observable(name);
    self.Status = ko.observable(status);  
}

function ViewModel() {
    var self = this;

    self.Items = ko.observableArray([
        new ItemModel('001', 'Jhon', 'Single'),
        new ItemModel('002', 'Mike', 'Married'),
        new ItemModel('003', 'Marrie', 'Complicated')
    ]);
    self.SelectedItem = ko.observable(new ItemModel());
    self.IsEditMode = ko.observable();

    self.setEditItem = function(item) {
        self.SelectedItem(item);
        self.IsEditMode(true);
    }
}

var viewModel = new ViewModel();
ko.applyBindings(viewModel);

Demo

演示