淘汰赛JS:获取所选数据的下拉列表并填充其他字段 [英] Knockout JS: Get dropdown selected data and populate other fields

查看:67
本文介绍了淘汰赛JS:获取所选数据的下拉列表并填充其他字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在此处使用Knockout JS.

我有一个HTML表,该表有4列.我有向表添加一行的按钮,然后针对每行删除按钮以将其删除.我在此表的第一列中也有一个下拉列表.下拉列表由表格外部的按钮单击事件填充.以下是我的html:

   <table class="table table-bordered">
    <thead class="mbhead">
      <tr class="mbrow">
       <th>Input</th>
       <th>First Name</th>
       <th>Last Name</th>
        <th>Address</th>
      </tr>
    </thead>
 <tbody data-bind="foreach: items">
<tr>
  <td>
    <select class="form-control" data-bind="options: $parent.ddl, optionsText: 'name', value: $parent.selectedColumnValue, optionsCaption: '--Select--',  event: { change: $parent.ddlChanged }">                          
                    </select>
  </td>
  <td><span class="input-small" data-bind="value: firstName" /></td>
  <td><span class="input-small" data-bind="value: lastName" /></td>
  <td><span class="input-small" data-bind="value: address" /></td>
  <td>
    <input type="button" value="Remove Row" data-bind="click: $parent.removeRow" class="btn btn-danger" />
  </td>
</tr>
</tbody>
</table>

<div class="col-xs-12 col-sm-6">
  <input type="button" value="Add Row" class="btn btn-primary" data-bind="click: addRow" />
 <input type="submit" value="Get Data" data-bind="click: GetData" class="btn btn-primary" />
</div>

我的敲除代码可以在jsfiddle链接中看到,如下所示.

我正在寻找的是:

当用户选择下拉菜单时,所选的下拉文本将填充到该行的第一列,而值将填充到该行的其他列/单元格.

我的问题:

1.)我无法从下拉列表中获取选定的文本和选定的值

当下拉列表中的选定索引更改事件被触发时,事件参数具有以下值,如控制台所示:

firstName:ƒc()

姓:ƒc()

地址:ƒc()

proto :对象

2.)其次,我不知道如何根据下拉选择来更新其他列值.

绑定到下拉列表的json如下:

'[{"FirstName":"Alex","LastName":"Sanders","Address":123},{"FirstName":"Sam","LastName":"Billings","Address":"Mahony Street"}]';

这是我的小提琴:

https://jsfiddle.net/aman1981/njbyumrs/12/

感谢您的投入.

解决方案

您混合了一些父级和级别的内容,并且表绑定在value而不是text上.我将下拉绑定selectedValue移到了Item,因为它位于行级别而不是父级别.我使用KO with绑定显示了HTML的那部分在selectedValue内部的值.

我还添加了带有KO值的<pre>标记,以便您查看与之交互以及KO模型数据更改时发生的情况.

旁注:Item中的三个属性在此演示中无需观察,因为在屏幕上时值不会更改.

 var ViewModel = function() {
  var self = this;

  //Empty Row
  self.items = ko.observableArray([new Item()]);
  self.ddl = ko.observableArray();  

  self.addRow = function() {
    self.items.push(new Item());
  };

  self.removeRow = function(data) {
    self.items.remove(data);
  };
  self.GetData = function() {
    if (self.ddl().length === 0) {
      var item1 = new Item("Alex", "Sanders", "Maple Street");
      self.ddl.push(item1);
      var item2 = new Item("Sam", "Billings", "Mahony Street");
      self.ddl.push(item2);
    }
  }
}

var Item = function(fname, lname, address) {
  var self = this;
  self.firstName = ko.observable(fname);
  self.lastName = ko.observable(lname);
  self.address = ko.observable(address);
  self.selectedValue = ko.observable();
};

vm = new ViewModel()
vm.GetData();
ko.applyBindings(vm); 

 <link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.4.2/knockout-min.js"></script>
<table class="table table-bordered">
  <thead class="mbhead">
    <tr class="mbrow">
      <th>Input</th>
      <th>First Name</th>
      <th>Last Name</th>
      <th>Address</th>
    </tr>
  </thead>
  <tbody data-bind="foreach: items">
    <tr>
      <td><select class="form-control" data-bind="options: $parent.ddl, optionsText: 'firstName', value: selectedValue, optionsCaption: '--Select--'"> </select></td>
      <td data-bind="with: selectedValue">
        <span data-bind="text: firstName"></span>
      </td>
      <td data-bind="with: selectedValue">
        <span data-bind="text: lastName"></span>
      </td>
      <td data-bind="with: selectedValue">
        <span data-bind="text: address"></span>
      </td>
      <td>
        <input type="button" value="Remove Row" data-bind="click: $parent.removeRow" class="btn btn-danger" />
      </td>
    </tr>
  </tbody>
</table>

<div class="col-xs-12 col-sm-6">  
  <input type="button" value="Add Row" class="btn btn-primary" data-bind="click: addRow" />
</div>

<pre data-bind="text: ko.toJSON($data, null, 2)"></pre> 

Using Knockout JS here.

I have a HTML table and the table has 4 columns. I have button to add a row to table and then remove button against each row to delete it. I also have a dropdown in the first column of this table. The dropdown is populated from the button click event outside the table. Below is my html:

   <table class="table table-bordered">
    <thead class="mbhead">
      <tr class="mbrow">
       <th>Input</th>
       <th>First Name</th>
       <th>Last Name</th>
        <th>Address</th>
      </tr>
    </thead>
 <tbody data-bind="foreach: items">
<tr>
  <td>
    <select class="form-control" data-bind="options: $parent.ddl, optionsText: 'name', value: $parent.selectedColumnValue, optionsCaption: '--Select--',  event: { change: $parent.ddlChanged }">                          
                    </select>
  </td>
  <td><span class="input-small" data-bind="value: firstName" /></td>
  <td><span class="input-small" data-bind="value: lastName" /></td>
  <td><span class="input-small" data-bind="value: address" /></td>
  <td>
    <input type="button" value="Remove Row" data-bind="click: $parent.removeRow" class="btn btn-danger" />
  </td>
</tr>
</tbody>
</table>

<div class="col-xs-12 col-sm-6">
  <input type="button" value="Add Row" class="btn btn-primary" data-bind="click: addRow" />
 <input type="submit" value="Get Data" data-bind="click: GetData" class="btn btn-primary" />
</div>

My knockout code can be seen in the jsfiddle link as below.

What I am looking for is:

When the user selects the dropdown the selected dropdown text gets populated to that rows one column and the value gets populated to that rows other columns/cell.

My Issue:

1.) I am not able to get the selected text and selected value from the dropdown

When the dropdown selected index change event is fired the event param has the below value as seen in console:

firstName : ƒ c()

lastName: ƒ c()

address : ƒ c()

proto : Object

2.) Secondly I don't know how I could update other column values based on the dropdown selection.

The json that gets binded to dropdown is like below:

'[{"FirstName":"Alex","LastName":"Sanders","Address":123},{"FirstName":"Sam","LastName":"Billings","Address":"Mahony Street"}]';

Here is my fiddle:

https://jsfiddle.net/aman1981/njbyumrs/12/

Inputs are appreciated.

解决方案

You've got some parent and level stuff mixed up, and your table was binding on value instead of text. I moved the drop down binding, selectedValue to Item since it's at the row level and not the parent level. I used the KO with binding to show the values inside selectedValue for that part of the HTML.

I also added a <pre> tag with the KO values so you can see what happens as you interact with it and the KO model data changes.

Side note: The three properties in Item don't need to be observable in this demo as the values do not change while on the screen.

var ViewModel = function() {
  var self = this;

  //Empty Row
  self.items = ko.observableArray([new Item()]);
  self.ddl = ko.observableArray();  

  self.addRow = function() {
    self.items.push(new Item());
  };

  self.removeRow = function(data) {
    self.items.remove(data);
  };
  self.GetData = function() {
    if (self.ddl().length === 0) {
      var item1 = new Item("Alex", "Sanders", "Maple Street");
      self.ddl.push(item1);
      var item2 = new Item("Sam", "Billings", "Mahony Street");
      self.ddl.push(item2);
    }
  }
}

var Item = function(fname, lname, address) {
  var self = this;
  self.firstName = ko.observable(fname);
  self.lastName = ko.observable(lname);
  self.address = ko.observable(address);
  self.selectedValue = ko.observable();
};

vm = new ViewModel()
vm.GetData();
ko.applyBindings(vm);

<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.4.2/knockout-min.js"></script>
<table class="table table-bordered">
  <thead class="mbhead">
    <tr class="mbrow">
      <th>Input</th>
      <th>First Name</th>
      <th>Last Name</th>
      <th>Address</th>
    </tr>
  </thead>
  <tbody data-bind="foreach: items">
    <tr>
      <td><select class="form-control" data-bind="options: $parent.ddl, optionsText: 'firstName', value: selectedValue, optionsCaption: '--Select--'"> </select></td>
      <td data-bind="with: selectedValue">
        <span data-bind="text: firstName"></span>
      </td>
      <td data-bind="with: selectedValue">
        <span data-bind="text: lastName"></span>
      </td>
      <td data-bind="with: selectedValue">
        <span data-bind="text: address"></span>
      </td>
      <td>
        <input type="button" value="Remove Row" data-bind="click: $parent.removeRow" class="btn btn-danger" />
      </td>
    </tr>
  </tbody>
</table>

<div class="col-xs-12 col-sm-6">  
  <input type="button" value="Add Row" class="btn btn-primary" data-bind="click: addRow" />
</div>

<pre data-bind="text: ko.toJSON($data, null, 2)"></pre>

这篇关于淘汰赛JS:获取所选数据的下拉列表并填充其他字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆