[英]How to make a value in dropdown selected based on value from database
I have a dropdown
which has three options User
, Instructor
and Player
. 我有一个dropdown
,其中有三个选项User
, Instructor
和Player
。
Now i have a table
which displays the list of all users
from the database. 现在,我有一个table
,显示数据库list of all users
的list of all users
。
The username
, firstname
, lastname
and a 'change authority' column. username
, firstname
, lastname
和“更改权限”列。 This change authority column has the dropdown
with three options 'user', 'player' and 'instructor' and also a save button are present. 此更改权限列具有带三个选项的dropdown
,分别为“用户”,“玩家”和“教师”,并且还存在一个保存按钮。
Now i want to have the drop down to auto select the authority
of it's user
. 现在,我想有下拉自动选择authority
的它的user
。
For ex: if A
person has authority
of Player
the dropdown should have player as selected and if B
person has authority
as instructor
he should have the corresponding dropdown with instructor selected. 对于例如:如果A
人有authority
的Player
作为选择下拉应具备的球员,如果B
人有authority
作为instructor
,他应该有选择的教练对应的下拉。
Can anyone help me with this. 谁能帮我这个。 Thanks. 谢谢。
The initial one was 最初的是
<select >
<option value="${user[1]}" selected>
<c:out value="${user[1]}" /></option> // this gives the current authority of person from database
<option value="1"> User </option>
<option value="2"> Player </option>
<option value="3"> Instructor </option>
</select>
So, i tried something like 所以,我尝试了类似
<select>
<option value="1" <c:if test="${user[1] == User}">
</c:if>>User</option>
<option value="2" <c:if test="${user[1] == Player}">
</c:if>> Player </option>
<option value="3" <c:if test="${user[1] == Instructor}">
</c:if>> Instructor</option>
Solution to display the selected drop-box : code below where $dispatched is the value got from the database 显示所选投递箱的解决方案:下面的代码,其中$ dispatched是从数据库获取的值
<select name="dispatched">
<option></option>
<option value="YES"<?php if($dispatched=="YES"){ echo'selected="selected"';}>>YES</option>
<option value="NO"<?php if($dispatched=="NO"){ echo'selected="selected"';}?>>NO</option>
</select>
I think this is what you're looking for. 我认为这就是您要寻找的。 If you want to select a specific option in a <select>
dropdown by default, just add the selected
attribute to that <option>
如果要默认在<select>
下拉列表中选择特定选项,只需将selected
属性添加到该<option>
<select id="role">
<option value="user">User</option>
<option value="instructor">Instructor</option>
<option value="player" selected>Player</option><!-- selected by default -->
</select>
To set it in JavaScript: 要在JavaScript中进行设置:
document.getElementById('role').value = 'player';
Here is one example of many to automatically select a drop down value from the database. 这是从数据库自动选择下拉值的许多示例。
public List<DefaultDropDownListModel> LoadTurnAroundTimesTypes(ESAMPR_Entities db)
{
List<DefaultDropDownListModel> list = new List<DefaultDropDownListModel>();
var qry = db.AR_TAT.OrderBy(x => x.SEQUENCE);
foreach(var i in qry)
{
DefaultDropDownListModel model = new DefaultDropDownListModel();
model.Value = i.AR_TAT_NUM;
model.Name = i.AR_TAT_NUM;
model.Selected = i.DEFAULT_SELECTION == "1" ? true : false;
list.Add(model);
}
return list;
}
<select id="AR_TAT" class="form-control" name="arvm.AR_TAT"> <option value=""></option> @foreach (var item in Model.TurnAroundTypes) { if (item.Selected) { <option value="@item.Value" selected>@item.Name</option> } else { <option value="@item.Value">@item.Name</option> } }
Here a working solution using angular. 在这里使用角的工作解决方案。 You just have to provide a REST api on your server. 您只需要在服务器上提供REST api。 I can do an example in PHP and MySQL... 我可以在PHP和MySQL中做一个例子...
var app = angular.module('app', []) .controller('MainCtrl', function($scope, $http) { // you should get users from server /*$http.get('yourServerApiUrl').then(function(responseFromApi) { $scope.simulatedUser = responseFromApi; });*/ // lets just simulate them $scope.simulatedUsers = [{ username: 'username1', firstname: 'John', lastname: 'Bon', autority: 'Player' }, { username: 'user2', firstname: 'Alex', lastname: 'Noi', autority: 'User' }, { username: 'username3', firstname: 'Fred', lastname: 'Mer', autority: 'Player' }, { username: 'username4', firstname: 'Gal', lastname: 'Ban', autority: 'User' }, { username: 'user5', firstname: 'Va', lastname: 'Ben', autority: 'Instructor' }]; // array of possible autorities $scope.autorities = ['User', 'Player', 'Instructor']; //submit to the server $scope.submit = function() { //$http.post('yourServerApiUrl', $scope.simulatedUsers); alert('users saved'); }; });
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <!DOCTYPE html> <html ng-app="app"> <head> <link data-require="bootstrap-css@3.3.6" data-semver="3.3.6" rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.css" /> </head> <body ng-controller="MainCtrl"> <form ng-submit='submit()'> <table class="table table-responsive table-hover"> <thead> <tr> <th>username</th> <th>firstname</th> <th>lastname</th> <th>autority</th> </tr> </thead> <tbody> <tr ng-repeat="u in simulatedUsers"> <td>{{ u.username }}</td> <td>{{ u.firstname }}</td> <td>{{ u.lastname }}</td> <td> <select class='form-control' ng-model="u.autority" ng-options="o for o in autorities"></select> </td> </tr> </tbody> </table> <button class='btn btn-success'>Save</button> </form> </body> </html>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.