2016-06-22 26 views
2

在与Arelia一起玩耍时,我创建了一个包含重复表格行的表格,并添加了绑定到服务的选择元素。当我从select中选择一个选项时,我想用属性对象的值填充description字段。我怎样才能做到这一点?更改中继器内选定更改的另一个字段的值

它看起来像我可以用一个价值转换器 - 当toView在调试器中被击中,我可以看到正确的产品,看到了描述。那么,我如何获取价值并将其注入line.description

这里是我想要做一个样本:

<template> 
<div class="row"> 
    <div class="col-md-12"> 
     <div class="panel panel-default"> 
      <div class="panel-heading"> 
       <h3 class="panel-title"><strong>Summary</strong></h3> 
      </div> 
      <div class="panel-body"> 
       <div class="table-responsive"> 
        <table class="table table-condensed"> 
         <thead> 
          <tr> 
           <td class="col-md-2"><strong>Item</strong></td> 
           <td class="col-md-8"><strong>Description</strong></td> 
           <td class="col-md-2"><strong>Quantity</strong></td> 
          </tr> 
         </thead> 
         <tbody> 
          <tr repeat.for="line of vm.orderLines"> 
           <td> 
            <select class="form-control" value.bind='products[0] | productToId:products'> 
               <option value="">Select a product</option> 
               <option repeat.for="product of products" model.bind="product.id">${product.name}</option> 
            </select> 
           </td> 
           <td> 
            <input class="form-control" value.bind="line.description" placeholder="Description"> 
           </td> 
           <td> 
            <input value.bind="line.quantity" type="number" class="form-control" /> 
           </td> 
          </tr> 
          <tr> 
           <td colspan="3"> 
            <button class="btn btn-default btn-small" click.delegate="addLine()">Add new row</button> 
           </td> 
          </tr> 

         </tbody> 
        </table> 
       </div> 

      </div> 
     </div> 
    </div> 
</div> 

我的JS貌似

import {HttpClient, json} from 'aurelia-fetch-client'; 
import {ProductService} from '../../services/product-service'; 
import {autoinject} from 'aurelia-framework'; 
import 'fetch'; 
@autoinject(ProductService) 
export class Play { 
heading = 'Playtime'; 
products = []; 
vm = { 
    orderLines: [] 
}; 
selectedProduct = 0; 

constructor(private http: HttpClient, 
    private productService: ProductService) { 
    this.getProducts(); 
    this.addLine(); 
} 

getProducts() { 
    return this.productService.get() 
     .then(response => { 
      this.products = response; 
     } 
     ); 
} 

addLine() { 
    this.vm.orderLines.push(new OrderLine(0, 0, '')); 
} 
} 

export class OrderLine { 
productId: number; 
quantity: number; 
description: string; 

constructor(productId: number, 
    quantity: number, 
    description: string) { 
    this.productId = productId; 
    this.quantity = quantity; 
    this.description = description; 
} 
} 

export class ProductToIdValueConverter { 
toView(product, products, line) { 
    debugger 
    return product ? product.id : null; 
} 
fromView(id, products) { 
    debugger 
    return products.find(u => u.id === id); 
} 
} 
+0

[绑定一个选择对象的奥里利亚数组和匹配的ID(HTTP的可能的复制。 com/questions/33920610/binding-a-select-of-a-object-in-aurelia-and-matching-on-id) –

回答

1

如果我理解正确的问题,我们的目标是有选择元素“选择”OrderLine实例的productIddescription

试着改变你的选择的价值结合到这样的事情://计算器:

<select class="form-control" value.bind="line | orderLineToProduct:products:line"> 
    <option value="">Select a product</option> 
    <option repeat.for="product of products" model.bind="product">${product.name}</option> 
</select> 
export class OrderLineToProductValueConverter { 
    toView(orderLine, products, orderLine2) { 
    return products.find(p => p.id === orderLine.productId); 
    } 

    fromView(product, products, orderLine) { 
    orderLine.productId = product.id; 
    orderLine.description = product.name; 
    } 
} 
+0

是否存在一个用于此目的的内置匹配器函数? –

+0

匹配器用于比较两个值并返回true/false它们相等。我认为OP的问题是“如何在做出选择时分配**两个**属性”。 (如果我正确阅读它) –

相关问题