AngularJs:从Json绑定ui-select并过滤table
AngularJs: Bind ui-select from Json and filter table
使用 AngularJS 1.3.4.
我有一个 html table 是从网络 api 填充的,我在其中发出 http 请求以获取该数据并填充我的 html table。我的示例 json 如下:
{
id: 1,
firstName: "John",
lastName: "Rein",
status: 'available'
},
{
id: 2,
firstName: "David",
lastName: "Gumry",
status: 'not available'
}
现在我在 table 下方有一个下拉菜单,我正在为它使用 ui-select。我想根据 json 中的状态填充此下拉列表。例如,在我上面的 json 中,我有 2 个状态可用和不可用。我希望我的下拉菜单具有这些值。填充下拉列表后,我想根据 selected 的下拉值过滤 table。我的下拉列表为:
<ui-select tagging ng-model="selected" theme="bootstrap">
<ui-select-match placeholder="Pick one...">{{$select.selected.value}}</ui-select-match>
<ui-select-choices repeat="val in values | filter: $select.search track by val.value">
<div ng-bind="val.value | highlight: $select.search"></div>
</ui-select-choices>
</ui-select>
我在以下位置创建了我的 jsfiddle:
https://jsfiddle.net/aman1981/wfL1374x/
我不确定如何将 json 的结果绑定到我的 DDL 并过滤我的 table。
您有一些问题需要处理,包括重复使用 ng-app 和 ng-controller。此外,似乎 ui-select 使用 ControllerAs 语法效果最好,这通常是首选方法。
经过这些更改和其他更改(太多无法列出),这是工作代码:
angular.module('myApp', ['ui.select'])
.controller("PeopleCtrl", function($http) {
var vm = this;
vm.people = [];
vm.isLoaded = false;
vm.values = [];
vm.loadPeople = function() {
// *** I had to comment this out as it is not allowed in the SO Code Snippet but is fine for your code
//$http({
// method: 'POST',
// url: '/echo/json/',
// data: mockDataForThisTest
//}).then(function(response, status) {
// console.log(response.data);
// vm.people = response.data;
//});
vm.people = [{
id: 1,
firstName: "John",
lastName: "Rein",
status: 'available'
},
{
id: 2,
firstName: "David",
lastName: "Gumry",
status: 'not available'
}
];
vm.values = [...new Set(vm.people.map(obj => ({
value: obj.status
})))];
};
vm.selected = {
key: null,
value: null
};
var mockDataForThisTest = "json=" + encodeURI(JSON.stringify([{
id: 1,
firstName: "John",
lastName: "Rein",
status: 'available'
},
{
id: 2,
firstName: "David",
lastName: "Gumry",
status: 'not available'
}
]));
})
<head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<link href="https://cdnjs.cloudflare.com/ajax/libs/angular-ui-select/0.20.0/select.min.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular-sanitize/1.2.23/angular-sanitize.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular-ui-select/0.20.0/select.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body ng-app="myApp">
<div ng-controller="PeopleCtrl as ctrl">
<br>
<p> Click <a ng-click="ctrl.loadPeople()">here</a> to load data.</p>
<h2>Data</h2>
<div class="row-fluid">
<table class="table table-hover table-striped table-condensed">
<thead>
<tr>
<th>Id</th>
<th>First Name</th>
<th>Last Name</th>
<th>Status</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="person in ctrl.people | filter: {status: ctrl.selected.value} : true">
<td>{{person.id}}</td>
<td>{{person.firstName}}</td>
<td>{{person.lastName}}</td>
<td>{{person.status}}</td>
</tr>
</tbody>
</table>
</div>
<br><br>
<div width="50px">
<ui-select tagging ng-model="ctrl.selected" theme="bootstrap">
<ui-select-match placeholder="Pick one...">{{$select.selected.value}}</ui-select-match>
<ui-select-choices repeat="val in ctrl.values | filter: $select.search track by val.value">
<div ng-bind="val.value | highlight: $select.search"></div>
</ui-select-choices>
</ui-select>
</div>
</div>
</body>
使用 AngularJS 1.3.4.
我有一个 html table 是从网络 api 填充的,我在其中发出 http 请求以获取该数据并填充我的 html table。我的示例 json 如下:
{
id: 1,
firstName: "John",
lastName: "Rein",
status: 'available'
},
{
id: 2,
firstName: "David",
lastName: "Gumry",
status: 'not available'
}
现在我在 table 下方有一个下拉菜单,我正在为它使用 ui-select。我想根据 json 中的状态填充此下拉列表。例如,在我上面的 json 中,我有 2 个状态可用和不可用。我希望我的下拉菜单具有这些值。填充下拉列表后,我想根据 selected 的下拉值过滤 table。我的下拉列表为:
<ui-select tagging ng-model="selected" theme="bootstrap">
<ui-select-match placeholder="Pick one...">{{$select.selected.value}}</ui-select-match>
<ui-select-choices repeat="val in values | filter: $select.search track by val.value">
<div ng-bind="val.value | highlight: $select.search"></div>
</ui-select-choices>
</ui-select>
我在以下位置创建了我的 jsfiddle: https://jsfiddle.net/aman1981/wfL1374x/
我不确定如何将 json 的结果绑定到我的 DDL 并过滤我的 table。
您有一些问题需要处理,包括重复使用 ng-app 和 ng-controller。此外,似乎 ui-select 使用 ControllerAs 语法效果最好,这通常是首选方法。
经过这些更改和其他更改(太多无法列出),这是工作代码:
angular.module('myApp', ['ui.select'])
.controller("PeopleCtrl", function($http) {
var vm = this;
vm.people = [];
vm.isLoaded = false;
vm.values = [];
vm.loadPeople = function() {
// *** I had to comment this out as it is not allowed in the SO Code Snippet but is fine for your code
//$http({
// method: 'POST',
// url: '/echo/json/',
// data: mockDataForThisTest
//}).then(function(response, status) {
// console.log(response.data);
// vm.people = response.data;
//});
vm.people = [{
id: 1,
firstName: "John",
lastName: "Rein",
status: 'available'
},
{
id: 2,
firstName: "David",
lastName: "Gumry",
status: 'not available'
}
];
vm.values = [...new Set(vm.people.map(obj => ({
value: obj.status
})))];
};
vm.selected = {
key: null,
value: null
};
var mockDataForThisTest = "json=" + encodeURI(JSON.stringify([{
id: 1,
firstName: "John",
lastName: "Rein",
status: 'available'
},
{
id: 2,
firstName: "David",
lastName: "Gumry",
status: 'not available'
}
]));
})
<head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<link href="https://cdnjs.cloudflare.com/ajax/libs/angular-ui-select/0.20.0/select.min.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular-sanitize/1.2.23/angular-sanitize.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular-ui-select/0.20.0/select.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body ng-app="myApp">
<div ng-controller="PeopleCtrl as ctrl">
<br>
<p> Click <a ng-click="ctrl.loadPeople()">here</a> to load data.</p>
<h2>Data</h2>
<div class="row-fluid">
<table class="table table-hover table-striped table-condensed">
<thead>
<tr>
<th>Id</th>
<th>First Name</th>
<th>Last Name</th>
<th>Status</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="person in ctrl.people | filter: {status: ctrl.selected.value} : true">
<td>{{person.id}}</td>
<td>{{person.firstName}}</td>
<td>{{person.lastName}}</td>
<td>{{person.status}}</td>
</tr>
</tbody>
</table>
</div>
<br><br>
<div width="50px">
<ui-select tagging ng-model="ctrl.selected" theme="bootstrap">
<ui-select-match placeholder="Pick one...">{{$select.selected.value}}</ui-select-match>
<ui-select-choices repeat="val in ctrl.values | filter: $select.search track by val.value">
<div ng-bind="val.value | highlight: $select.search"></div>
</ui-select-choices>
</ui-select>
</div>
</div>
</body>