2017-06-06 27 views
1

在flask-admin模型视图中我想过滤下拉菜单,在编辑/创建视图中根据在前一个字段中输入的值,该字段是一种关系。 在特定情况下,我希望看到在显示在属于选择“categoria”Flask-Admin:如何根据前一个字段中输入的值过滤字段的可选数据,并在另一个表中搜索条件

class Tag(db.Model): 
    __tablename__ = 'tags' 
    nr_TAG = db.Column(db.Integer, primary_key=True) 
    categoria_id = db.Column(db.Integer, db.ForeignKey('categorie.id'), nullable=False) 
    categoria = db.relationship('Categoria', backref='tags') 
    posto_id = db.Column(db.Integer, db.ForeignKey('stalli.id'), nullable=False) 
    posto = db.relationship('Stallo', backref='tags') 
    cognome = db.Column(db.String(30), nullable=False) 
    nome = db.Column(db.String(30)) 
    auto = db.Column(db.String(20)) 
    targa = db.Column(db.String(10)) 
    recapito = db.Column(db.String(80)) 
    stato = db.Column(db.Integer) 
    posto_occ = db.Column(db.Integer) 
    orario = db.Column(db.Integer) 
    fuoriposto = db.Column(db.String(10)) 
    parcheggio = db.Column(db.String(40)) 

class TagsAdmin(sqla.ModelView): 
    can_edit = True 
    can_create = True 
    column_list = ['nr_TAG', 'categoria', 'posto', 'cognome', 'targa', 
         'stato', 'orario', 'fuoriposto', 'parcheggio'] 
    can_view_details = True 
    details_modal = True 
    form_columns = ['nr_TAG', 'categoria', 'posto', 'cognome', 'nome', 'auto', 'targa', 'recapito'] 

任何想法另一个表的发布内容“posto”只有那些? 感谢

+0

我想你会需要加载的所有值在下拉菜单中,并使用JavaScript来筛选根据用户输入值的值。 – MrLeeh

+0

你有这样的例子吗?事实上,我认为这是一个好主意 –

回答

0

我想出了以下解决方案,也许它并不完美,但它的工作:

master.html

function populateSelectField(to_populate, options, select) { 
 
    $('#s2id_' + to_populate + ' > a > span').text(''); 
 

 
    var el = $('select[name$="' + to_populate + '"]'); 
 
    el.children().remove(); 
 

 
    option = '<option value=""></option>'; 
 
    el.append($(option)); 
 

 
    options.forEach(function(row) { 
 
    var option_id = row.id, 
 
     option_title = row.title; 
 

 
    if (select && option_id == select) { 
 
     option = '<option value="' + option_id + '" selected>' + option_title + '</option>'; 
 
     $('#s2id_' + to_populate + ' > a > span').text(option_title); 
 
    } else { 
 
     option = '<option value="' + option_id + '">' + option_title + '</option>'; 
 
    } 
 

 
    el.append($(option)); 
 
    }); 
 
} 
 

 
function getOptions(el, to_populate, url, select) { 
 
    var item_id = el.val(), 
 
    url = window.location.origin + url + item_id + '/'; 
 

 
    $.get(url).done(function(options) { 
 
    populateSelectField(to_populate, options, select); 
 
    }).fail(function(err) { 
 
    console.error(err.message); 
 
    return false; 
 
    }); 
 
} 
 

 
function selectFieldManipulation(from_populate, to_populate, url) { 
 
    var main_select = $('select[name$="' + from_populate + '"]'), 
 
    to_populate_el = $('select[name$="' + to_populate + '"]'); 
 

 
    getOptions(main_select, to_populate, url, to_populate_el.val()); 
 

 
    main_select.change(function(e) { 
 
    getOptions(main_select, to_populate, url, null); 
 
    }); 
 
}

create.html上/edit.html

$(document).ready(function() { 
 
    selectFieldManipulation('from_populate', 'to_populate', '/api/route_to_view/'); 
 
});

相关问题