gpt4 book ai didi

javascript - 多选下拉过滤器 AngularJS 逻辑

转载 作者:行者123 更新时间:2023-11-28 04:22:49 24 4
gpt4 key购买 nike

感谢您花时间阅读我的问题。我目前正在开发 AngularJS UI-Grid,我已经为“名字”列实现了多选下拉菜单。然而,我在理解如何实现逻辑时遇到了一些问题,就像我对“男性”和“女性”列所做的那样,因为这是多选的。如果有人可以帮助我解决我已经为第一列“名字”创建的示例的逻辑。至少一些指导将不胜感激。先感谢您!此外,您可能会注意到网格已损坏,标题行被卡住且无法水平滚动。此问题已修复!

这是笨蛋! https://plnkr.co/edit/cAiZOIBpYZgDpfwspABK?p=preview

这是我的一些 Angular 和 HTML 代码!我知道我的代码不是很 DRY,我很快就会重构我的代码。

  <head>
<meta charset="utf-8" />
<title>AngularJS Plunker</title>
<script>document.write('<base href="' + document.location + '" />');</script>

<script data-require="angular.js@1.5.x" src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.11/angular.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.11/angular-animate.js"></script>
<script data-require="angular-ui-bootstrap@*" data-semver="1.3.3" src="https://cdnjs.cloudflare.com/ajax/libs/angular-ui-bootstrap/1.3.3/ui-bootstrap-tpls.js"></script>
<script data-require="ui-grid@*" data-semver="4.0.2" src="https://cdnjs.cloudflare.com/ajax/libs/angular-ui-grid/4.0.2/ui-grid.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/angularjs-dropdown-multiselect/1.11.8/angularjs-dropdown-multiselect.min.js"></script>
<script type="text/javascript" src="script.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.5.0/angular-touch.js"></script>
<script data-require="jquery@*" data-semver="3.1.1" src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script data-require="bootstrap.js@*" data-semver="3.3.6" src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>

<!--LINK TAGS-->
<link data-require="ui-grid@*" data-semver="4.0.2" rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/angular-ui-grid/4.0.2/ui-grid.css" />
<link data-require="bootstrap-css@*" data-semver="4.0.0-alpha.4" rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.4/css/bootstrap.min.css" />
<link rel="stylesheet" href="style.css" />
</head>

<body ng-controller="mainCtrl">

<button id="filterButton" type="button" class="btn btn-primary" data-toggle="modal" data-target="#myModal">Filter Columns</button>

<div class="modal fade" id="myModal" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">&times;</button>
<h4 class="modal-title"><strong>Filter Columns</strong></h4>
</div>
<div class="modal-body">
<table>
<thead>
<tr>
<th>Column Name:</th>
<th>Filter:</th>
<th>Value:</th>
</tr>
</thead>
<tbody>
<tr>
<!-- FIRST COLUMN ROW -->
<td>
<select ng-model="columnName" class="form-control" class="columnName">
<option></option>
<option>First Name</option>
<option>Last Name</option>
<option>Company</option>
<option>Employed</option>
<option>Gender</option>
<option>Country</option>
<option>DOB</option>
<option>Age</option>
</select>
</td>
<td>
<select ng-model="filter" class="form-control" class="filter">
<option>Equal to</option>
<option>Contains</option>
<option>in between</option>
<option>Not equal to</option>
<option>Greater than</option>
<option>Less than</option>
</select>
</td>
<td>
<input class="form-control" type="text">
</td>
<!-- FIRST COLUMN ROW -->
</tr>
<tr>
<!-- SECOND COLUMN ROW -->
<td>
<select class="form-control" class="columnName">
<option></option>
<option>First Name</option>
<option>Last Name</option>
<option>Company</option>
<option>Employed</option>
<option>Gender</option>
<option>Country</option>
<option>DOB</option>
<option>Age</option>
</select>
</td>
<td>
<select class="form-control" class="filter">
<option>Equal to</option>
<option>Contains</option>
<option>in between</option>
<option>Not equal to</option>
<option>Greater than</option>
<option>Less than</option>
</select>
</td>
<td>
<input class="form-control" type="text">
<!-- SECOND COLUMN ROW -->
</tr>
<tr>
<!-- THIRD COLUMN ROW -->
<td>
<select class="form-control" class="columnName">
<option></option>
<option>First Name</option>
<option>Last Name</option>
<option>Company</option>
<option>Employed</option>
<option>Gender</option>
<option>Country</option>
<option>DOB</option>
<option>Age</option>
</select>
</td>
<td>
<select class="form-control" class="filter">
<option>Equal to</option>
<option>Contains</option>
<option>in between</option>
<option>Not equal to</option>
<option>Greater than</option>
<option>Less than</option>
</select>
</td>
<td>
<input class="form-control" type="text">
</td>
<!-- THIRD COLUMN ROW -->
</tr>
<tr>
<!-- FORTH COLUMN ROW -->
<td>
<select class="form-control" class="columnName">
<option></option>
<option>First Name</option>
<option>Last Name</option>
<option>Company</option>
<option>Employed</option>
<option>Gender</option>
<option>Country</option>
<option>DOB</option>
<option>Age</option>
</select>
</td>
<td>
<select class="form-control" class="filter">
<option>Equal to</option>
<option>Contains</option>
<option>in between</option>
<option>Not equal to</option>
<option>Greater than</option>
<option>Less than</option>
</select>
</td>
<td>
<input class="form-control" type="text">
</td>
<!-- FORTH COLUMN ROW -->
</tr>
<tr>
<!-- FIFTH COLUMN ROW -->
<td>
<select class="form-control" class="columnName">
<option></option>
<option>First Name</option>
<option>Last Name</option>
<option>Company</option>
<option>Employed</option>
<option>Gender</option>
<option>Country</option>
<option>DOB</option>
<option>Age</option>
</select>
</td>
<td>
<select class="form-control" class="filter">
<option>Equal to</option>
<option>Contains</option>
<option>in between</option>
<option>Not equal to</option>
<option>Greater than</option>
<option>Less than</option>
</select>
</td>
<td>
<input class="form-control" type="text">
</td>
<!-- FIFTH COLUMN ROW -->
</tr>
<tr>
<!-- SIXTH COLUMN ROW -->
<td>
<select class="form-control" class="columnName">
<option></option>
<option>First Name</option>
<option>Last Name</option>
<option>Company</option>
<option>Employed</option>
<option>Gender</option>
<option>Country</option>
<option>DOB</option>
<option>Age</option>
</select>
</td>
<td>
<select class="form-control" class="filter">
<option>Equal to</option>
<option>Contains</option>
<option>in between</option>
<option>Not equal to</option>
<option>Greater than</option>
<option>Less than</option>
</select>
</td>
<td>
<input class="form-control" type="text">
</td>
</tr>
<!-- SIXTH COLUMN ROW -->
</tbody>
</table>
<a href="#">add filter</a>
</div>
<div class="modal-footer">
<button id="applyButton" type="button" class="btn btn-primary">Apply Filters</button>
<button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
</div>
</div>
</div>
</div>
<br>
<br>
<h1>Hello World</h1>
<div ui-grid="gridOptions" ui-grid-pagination ui-grid-exporter ui-grid-resize-columns ui-grid-selection ui-grid-move-columns ui-grid-edit ui-grid-pinning class="myGrid" ng-model="columnName">
</div>
</body>
</html>

//下面的 Angular 代码

var myApp = angular.module("myApp.bootstrap",["ngAnimate",
"ui.bootstrap", "ui.grid", "ui.grid.moveColumns", "ui.grid.edit",
"ui.grid.selection", "ui.grid.resizeColumns", "ui.grid.exporter",
"ui.grid.pagination", "ui.grid.pinning", "angularjs-dropdown-multiselect"]);

myApp.controller('mainCtrl', ['$scope', 'uiGridConstants', function ($scope, uiGridConstants) {

$scope.example1model = [];
$scope.example1data = [{
id: 1, label: "Cox"},{
id: 2, label: "Lorraine"},{
id: 4, label: "Nancy"},{
id: 5, label: "Laura"},{
id: 6, label: "Ana"},{
id: 7, label: "Feli"},{
id: 8, label: "Victoria"},{
id: 9, label: "Relu"},{
id: 10, label: "Cristi"},{
id: 11, label: "Jacob"},{
id: 12, label: "Mike"},{
id: 13, label: "Minodoa"},{
id: 14, label: "Daciana"},{
id: 15, label: "Utu"},{
id: 16, label: "Kyle"}
];

$scope.example1settings = {
scrollableHeight: '300px',
scrollable: true,
enableSearch: true,
checkBoxes: true
};
$scope.gridOptions = {
enableFiltering: true,
enableFullRowSelection: true,
enableSelectAll: true,
enableGridMenu: true,
paginationPageSizes: [25,45,65],
paginationPageSize: 45,
enableSorting: true,
enableHorizontalScrollbar: 1,
columnDefs: [
{field: 'firstName', minWidth: 250, headerCellClass: 'blue',
filterHeaderTemplate:'<div class="ui-grid-filter-container"><div ng-dropdown-multiselect="" options="grid.appScope.example1data" selected-model="grid.appScope.example1model" checkboxes="true" extra-settings="grid.appScope.example1settings"></div></div>'},
{field: 'lastName', minWidth: 250, headerCellClass: 'blue'},
{field: 'company', minWidth: 250, headerCellClass: 'blue'},
{field: 'employed', minWidth: 250, headerCellClass: 'blue'},
{field: 'gender', minWidth: 250, headerCellClass: 'blue',
filter: {
term: '1',
type: uiGridConstants.filter.SELECT,
selectOptions: [ { value: '1', label: 'male' }, { value: '2', label: 'female' }]
},
cellFilter: 'mapGender'},
{field: 'country', minWidth: 250, headerCellClass: 'blue'},
{field: 'dob', minWidth: 250, headerCellClass: 'blue'},
{field: 'age', minWidth: 250, headerCellClass: 'blue'}
],
};




/*=========GRID OPTION========*/
$scope.gridOptions.multiSelect = true;
/*=========GRID OPTION=========*/

$scope.myData = [
{
"firstName": "Cox",
"lastName": "Carney",
"company": "Enormo",
"employed": true,
"gender": "male",
"country": "USA",
"dob": "08/14/1992",
"age": "25"
},


$scope.gridOptions.data = $scope.myData;

$scope.myData.forEach( function addDates( row, index ){
row.gender = row.gender==='male' ? '1' : '2';
});

}])

myApp.filter('mapGender', function() {
var genderHash = {
1: 'male',
2: 'female'
};

return function(input) {
if (!input){
return '';
} else {
return genderHash[input];
}
};
});

最佳答案

您的意思是要通过选中多个“名字”选项来过滤行吗?

我能想到的一件事是你可以自己做过滤器。每次选中/取消选中复选框选项时都会过滤网格数据,过滤后,您将数据重新分配回 $scope.myData

关于javascript - 多选下拉过滤器 AngularJS 逻辑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45310482/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com