我在这方面一事无成。任何有关此的帮助将不胜感激!
我有两个用于经理和员工的下拉菜单。
经理下拉列表默认包含经理列表。
我想通过使用用户在经理下拉列表中选择的经理名称查询 SQL Server 数据库,用员工姓名填充员工下拉列表。
例如如果某人在经理下拉列表中选择汤姆作为经理,则员工下拉列表应填充员工姓名,其中经理 = 汤姆。
到目前为止,我有以下代码:
路由(我用它来查询 SQL Server 数据库以根据经理姓名获取员工列表):
@app.route ('/getEmployees', methods=['GET'])
def getEmployees():
engine = create_engine('mssql+pyodbc://<server name>/<DB name>?driver=SQL+Server+Native+Client+11.0')
Base = declarative_base(engine)
class Bookmarks(Base):
__tablename__ = '<table name>'
Employee = Column(String(50))
__table_args__ = {'autoload': True}
def __repr__(self):
return '{} '.format(self.Employee)
def loadSession():
metadata = Base.metadata
Session = sessionmaker(bind=engine)
session = Session()
return session
if __name__ == "__main__":
session = loadSession()
return session.query(Bookmarks).filter_by(Manager='<I want to populate this with the manager name a user selects in the Manager dropdown>')
index.html 中的经理下拉列表
<div class="form-group">
<select class="form-control" id="next" name="division" data-error="Required!" required>
<option value="default" disabled selected>Select something</option>
<option value="option1">Tom</option>
<option value="option2">Bob</option>
</select>
</div>
app.py 中的员工下拉列表
Employees = QuerySelectField('Employees', query_factory=getEmployees, allow_blank=True)
在 index.html 中
<div class="form-group" id="sel_user">
{{ render_field(form.Employees,class="form-control", required="required") }}
</div>
我正在使用 jQuery 和 ajax 来获取用户在管理器下拉列表中选择的管理器名称,然后用它做一些事情..
$(document).ready(function(){
$("#next").change(function() {
var Manager=($('#next option:selected').text());
console.log(Manager);
$.ajax({
url: "/getEmployees",
type: 'GET',
contentType: 'application/json',
dataType: 'json',
// not sure what to do next..?
});
});
});
一旦我获得了用户在“管理器”下拉列表中选择的值,谁能帮助我接下来应该做什么?
最佳答案
我想你快到了。我不懂 python 或 flask,但我可以告诉你主要思想。
当您选择经理时,您会得到经理的名字,您必须在 ajax 调用中发送该名字,这样您就可以在路由代码中获取它并使用它来过滤数组。您可以使用 ajax 调用的
data
参数发送该值...$(document).ready(function(){ $("select#next").change(function() { var managerName = $(this).find('option:selected').text(); $.ajax({ type: 'GET', url: "/getEmployees", data: { manager: managerName } contentType: 'application/json', dataType: 'json' }); }); });
在您的 ajax 调用中,创建一个成功回调函数,该函数将在您收到成功响应时调用。像这样的……
$(document).ready(function(){ $("select#next").change(function() { var managerName = $(this).find('option:selected').text(); $.ajax({ type: 'GET', url: "/getEmployees",, data: { manager: managerName } contentType: 'application/json', dataType: 'json', success: function(response) { } }); }); });
您还可以添加一个检查来验证您是否选择了经理。如果您取消选择经理,您可以清空员工选择、禁用它、显示所有员工或任何您想要的。
$(document).ready(function(){ $("select#next").change(function() { if ($(this).val() != 'default') { var managerName = $(this).find('option:selected').text(); $.ajax({ type: 'GET', url: "/getEmployees",, data: { manager: managerName } contentType: 'application/json', dataType: 'json', success: function(response) { } }); } else { // No manager selected. // Do what you wnat when there's no manager selected. // For example, if you'd want to empty and disable the employees select... $('select#sel_user').html('').prop('disabled',true); } }); });
现在我有问题可以帮助你,因为我缺乏 python/flask 的知识:
在您的路由代码中,您必须读取通过 GET ajax 调用发送的
manager
参数。我不知道你是如何在 python/flask 中做到这一点的,但它必须很容易。在 php 中只是$_GET['manager']
。通过快速搜索,它看起来像request.GET['username']
,但您会比我更了解它。您必须获取该参数并将该值放在最后一个返回行中,例如...return session.query(Bookmarks).filter_by(Manager=request.GET['username'])
我不清楚这个响应的格式,所以我不知道如何提取信息来创建员工选择。查看您的 ajax 调用,您说响应采用 JSON 格式,但我需要查看该响应的示例以向您展示确切的代码。这个想法是您必须获取该信息并在 ajax 调用的成功函数中创建选项,然后将该选项放入员工选择中。像...
// Imagine that your response is an array of objects similar to this // [{"name": "Tommy", "other": "value10"},{"name": "Jim", "other": "value32"},...] success: function(response) { var options = []; for (var i=0, l=response.length; i<l; i++) options.push('<options value="'+response[i].other+'">'+response[i].name+'<options>'); $('select#sel_user').html(options.join('')); }
我认为通过所有这些,您可以了解如何继续进行,并根据您的特定需求进行调整。希望对您有所帮助。
关于jquery - 使用 ajax 和 jQuery 根据在 flask 中第一个下拉列表中选择的值填充第二个下拉列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46758066/