Dependent/Cascading Aurelia 中的下拉列表
Dependent/Cascading Dropdowns in Aurelia
我想在 Aurelia 中创建依赖下拉菜单。
我所说的依赖下拉菜单的意思是,如果您单击第一个下拉菜单中的项目,那么第二个下拉菜单中的选项将根据您单击的内容进行过滤。如果您查看我的 js-fiddle,您会注意到,如果您在第一个下拉列表中选择 select Toyota,则第二个下拉列表中会显示 Toyota 车型(Corolla、Camry 等)。如果您在第一个下拉列表中 select 本田,则第二个下拉列表中会填充本田车型(雅阁、思域等)。你明白了。
在 Angular 中执行此操作非常简单,只需要很少的代码,我想知道在 Aurelia 中执行此操作是否也简单明了。就如何在 Aurelia 中执行此操作而言,我无法找到太多资源。下面的 fiddle 准确地展示了我想要完成的事情(除了我想在 Aurelia 中完成,当然不是 Angular)。任何帮助将非常感激。谢谢!
https://jsfiddle.net/nhunt3/wjvvjwzx/
HTML:
<div ng-app="myApp" ng-controller="myController">
My Cars
<br />
<table>
<thead>
<tr>
<td>Make</td>
<td>Model</td>
</tr>
</thead>
<tbody>
<tr ng-repeat="car in myCars track by car.uid">
<td>
<select ng-model="car.MakeUID" ng-options="make.uid as make.name for make in makes" />
</td>
<td>
<select
ng-model="car.ModelUID"
ng-options="model.uid as model.name for model in models | filter: { parentID: car.MakeUID } : true" />
</td>
</tr>
</tbody>
</table>
</div>
JavaScript:
// the main (app) module
var myApp = angular.module("myApp", []);
angular.module('myApp', []).controller('myController', function($scope) {
$scope.makes = [
{name:'Toyota', uid:1},
{name:'Honda', uid:2},
{name:'Ford', uid:3}
];
$scope.models = [
{name:'Yaris', uid:1, parentID:1},
{name:'Corolla', uid:2, parentID:1},
{name:'Camry', uid:3, parentID:1},
{name:'Highlander', uid:4, parentID:1},
{name:'Accord', uid:5, parentID:2},
{name:'Civic', uid:6, parentID:2},
{name:'Pilot', uid:7, parentID:2},
{name:'Focus', uid:8, parentID:3},
{name:'Fiesta', uid:9, parentID:3},
{name:'Fusion', uid:10, parentID:3},
{name:'F-150', uid:10, parentID:3}
];
$scope.myCars = [
{uid:1, MakeUID:1, ModelUID:2},
{uid:2, MakeUID:1, ModelUID:3},
{uid:3, MakeUID:3, ModelUID:8}
];
});
找到此主题的其他用户:
app.html
<template>
<require from="./filter"></require>
<table>
<thead>
<tr>
<td>Make</td>
<td>Model</td>
</tr>
</thead>
<tbody>
<tr repeat.for="car of myCars">
<td>
<select value.bind="car.MakeUID" change.delegate="resetModel(car)">
<option value="0">select one of ...</option>
<option repeat.for="make of makes" model.bind="make.uid">${make.name}</option>
</select>
</td>
<td>
<select value.bind="car.ModelUID">
<option value="0">select one of ...</option>
<option repeat.for="model of models | filter:'parentID':car.MakeUID" model.bind="model.uid">${model.name}</option>
</select>
</td>
</tr>
</tbody>
</template>
app.js
export class App {
makes = [
{name:'Toyota', uid:1},
{name:'Honda', uid:2},
{name:'Ford', uid:3}
];
models = [
{name:'Yaris', uid:1, parentID:1},
{name:'Corolla', uid:2, parentID:1},
{name:'Camry', uid:3, parentID:1},
{name:'Highlander', uid:4, parentID:1},
{name:'Accord', uid:5, parentID:2},
{name:'Civic', uid:6, parentID:2},
{name:'Pilot', uid:7, parentID:2},
{name:'Focus', uid:8, parentID:3},
{name:'Fiesta', uid:9, parentID:3},
{name:'Fusion', uid:10, parentID:3},
{name:'F-150', uid:10, parentID:3}
];
myCars = [
{uid:1, MakeUID:1, ModelUID:2},
{uid:2, MakeUID:1, ModelUID:3},
{uid:3, MakeUID:3, ModelUID:8}
];
resetModel(car) {
car.ModelUID = 0;
}
}
filter.js
export class FilterValueConverter {
toView(array, propertyName, filter_on) {
return array.filter(i => i[propertyName] == filter_on);
}
}
旧答案
我认为这与您在 Aurelia 中的 Angular 代码相同:
<template>
<require from="./filter"></require>
<table>
<thead>
<tr>
<td>Make</td>
<td>Model</td>
</tr>
</thead>
<tbody>
<tr repeat.for="car of myCars">
<td>
<select value.bind="car.MakeUID" ref="selected_car">
<option repeat.for="make of makes" value.bind="make.uid">${make.name}</option>
</select>
</td>
<td>
<select value.bind="car.ModelUID">
<option repeat.for="model of models | filter:'parentID':selected_car.value">${model.name}</option>
</select>
</td>
</tr>
</tbody>
</template>
你还需要这个过滤器:
export class FilterValueConverter {
toView(array, propertyName, filter_on) {
return array.filter(i => i[propertyName] == filter_on);
}
}
我为你完成了你的解决方案,但你给了我一个非常关键的开始。我在你的 make td 中删除了一个不必要的 ref 并添加了一对 model.binds 并更改了你的过滤器匹配的内容。
查看我从你那里分叉出来的最终要点。
我想在 Aurelia 中创建依赖下拉菜单。
我所说的依赖下拉菜单的意思是,如果您单击第一个下拉菜单中的项目,那么第二个下拉菜单中的选项将根据您单击的内容进行过滤。如果您查看我的 js-fiddle,您会注意到,如果您在第一个下拉列表中选择 select Toyota,则第二个下拉列表中会显示 Toyota 车型(Corolla、Camry 等)。如果您在第一个下拉列表中 select 本田,则第二个下拉列表中会填充本田车型(雅阁、思域等)。你明白了。
在 Angular 中执行此操作非常简单,只需要很少的代码,我想知道在 Aurelia 中执行此操作是否也简单明了。就如何在 Aurelia 中执行此操作而言,我无法找到太多资源。下面的 fiddle 准确地展示了我想要完成的事情(除了我想在 Aurelia 中完成,当然不是 Angular)。任何帮助将非常感激。谢谢!
https://jsfiddle.net/nhunt3/wjvvjwzx/
HTML:
<div ng-app="myApp" ng-controller="myController">
My Cars
<br />
<table>
<thead>
<tr>
<td>Make</td>
<td>Model</td>
</tr>
</thead>
<tbody>
<tr ng-repeat="car in myCars track by car.uid">
<td>
<select ng-model="car.MakeUID" ng-options="make.uid as make.name for make in makes" />
</td>
<td>
<select
ng-model="car.ModelUID"
ng-options="model.uid as model.name for model in models | filter: { parentID: car.MakeUID } : true" />
</td>
</tr>
</tbody>
</table>
</div>
JavaScript:
// the main (app) module
var myApp = angular.module("myApp", []);
angular.module('myApp', []).controller('myController', function($scope) {
$scope.makes = [
{name:'Toyota', uid:1},
{name:'Honda', uid:2},
{name:'Ford', uid:3}
];
$scope.models = [
{name:'Yaris', uid:1, parentID:1},
{name:'Corolla', uid:2, parentID:1},
{name:'Camry', uid:3, parentID:1},
{name:'Highlander', uid:4, parentID:1},
{name:'Accord', uid:5, parentID:2},
{name:'Civic', uid:6, parentID:2},
{name:'Pilot', uid:7, parentID:2},
{name:'Focus', uid:8, parentID:3},
{name:'Fiesta', uid:9, parentID:3},
{name:'Fusion', uid:10, parentID:3},
{name:'F-150', uid:10, parentID:3}
];
$scope.myCars = [
{uid:1, MakeUID:1, ModelUID:2},
{uid:2, MakeUID:1, ModelUID:3},
{uid:3, MakeUID:3, ModelUID:8}
];
});
找到此主题的其他用户:
app.html
<template>
<require from="./filter"></require>
<table>
<thead>
<tr>
<td>Make</td>
<td>Model</td>
</tr>
</thead>
<tbody>
<tr repeat.for="car of myCars">
<td>
<select value.bind="car.MakeUID" change.delegate="resetModel(car)">
<option value="0">select one of ...</option>
<option repeat.for="make of makes" model.bind="make.uid">${make.name}</option>
</select>
</td>
<td>
<select value.bind="car.ModelUID">
<option value="0">select one of ...</option>
<option repeat.for="model of models | filter:'parentID':car.MakeUID" model.bind="model.uid">${model.name}</option>
</select>
</td>
</tr>
</tbody>
</template>
app.js
export class App {
makes = [
{name:'Toyota', uid:1},
{name:'Honda', uid:2},
{name:'Ford', uid:3}
];
models = [
{name:'Yaris', uid:1, parentID:1},
{name:'Corolla', uid:2, parentID:1},
{name:'Camry', uid:3, parentID:1},
{name:'Highlander', uid:4, parentID:1},
{name:'Accord', uid:5, parentID:2},
{name:'Civic', uid:6, parentID:2},
{name:'Pilot', uid:7, parentID:2},
{name:'Focus', uid:8, parentID:3},
{name:'Fiesta', uid:9, parentID:3},
{name:'Fusion', uid:10, parentID:3},
{name:'F-150', uid:10, parentID:3}
];
myCars = [
{uid:1, MakeUID:1, ModelUID:2},
{uid:2, MakeUID:1, ModelUID:3},
{uid:3, MakeUID:3, ModelUID:8}
];
resetModel(car) {
car.ModelUID = 0;
}
}
filter.js
export class FilterValueConverter {
toView(array, propertyName, filter_on) {
return array.filter(i => i[propertyName] == filter_on);
}
}
旧答案
我认为这与您在 Aurelia 中的 Angular 代码相同:
<template>
<require from="./filter"></require>
<table>
<thead>
<tr>
<td>Make</td>
<td>Model</td>
</tr>
</thead>
<tbody>
<tr repeat.for="car of myCars">
<td>
<select value.bind="car.MakeUID" ref="selected_car">
<option repeat.for="make of makes" value.bind="make.uid">${make.name}</option>
</select>
</td>
<td>
<select value.bind="car.ModelUID">
<option repeat.for="model of models | filter:'parentID':selected_car.value">${model.name}</option>
</select>
</td>
</tr>
</tbody>
</template>
你还需要这个过滤器:
export class FilterValueConverter {
toView(array, propertyName, filter_on) {
return array.filter(i => i[propertyName] == filter_on);
}
}
我为你完成了你的解决方案,但你给了我一个非常关键的开始。我在你的 make td 中删除了一个不必要的 ref 并添加了一对 model.binds 并更改了你的过滤器匹配的内容。
查看我从你那里分叉出来的最终要点。