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

时间:2023-02-05 17:55:15

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。我想我必须创建一个这样的viewmodel:

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/

2 个解决方案

#1


5  

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

这是一个如何做到这一点的例子:

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 selected property:

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

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

And you bind the click handler in your tr like so:

并且你将tr中的click处理程序绑定为:

<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 select function to turn it to true:

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

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.

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

Update your bindings on the input boxes:

更新输入框上的绑定:

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

#2


1  

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

#1


5  

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

这是一个如何做到这一点的例子:

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 selected property:

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

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

And you bind the click handler in your tr like so:

并且你将tr中的click处理程序绑定为:

<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 select function to turn it to true:

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

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.

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

Update your bindings on the input boxes:

更新输入框上的绑定:

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

#2


1  

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