我创建了一个自定义视图,该视图以类似手风琴的方式组织了我所有的产品和服务,因此数据是用户友好的。下面是我的自定义视图:
class productView(BaseView):
form_columns = ['name','price']
@expose('/')
def index(self):
myServ = Product.query.all()
myProd = Productcategories.query.all()
return self.render('admin/products.html', myProd = myProd, myServ = myServ)
admin.add_view(productView(name='Products and Services', endpoint='products'))
然后我继续制作HTML视图,该视图的效果非常好。我现在所处的位置是我创建了一些按钮,这些按钮使用我的jquery来加载模态。那也很好。但是,我有些犹豫。
点击“提交”按钮后,如何告诉它添加/删除/编辑特定项目?
//button scripts
$(document).ready(function() {
$(".add-category-item").click(function() {
$('.bg-modal').fadeIn('slow');
});
$(".close").click(function() {
$('.bg-modal').fadeOut('slow');
});
});
$(document).ready(function() {
$(".subtract-category-item").click(function() {
$('.bg-modal').fadeIn('slow');
});
$(".close").click(function() {
$('.bg-modal').fadeOut('slow');
});
});
$(document).ready(function() {
$(".edit-category-item").click(function() {
var parent = $(this).attr("data-parent");
$('#papa').val(parent);
console.log("the parent caregory is:" + parent);
$('.bg-modal').fadeIn('slow');
});
$(".close").click(function() {
$('.bg-modal').fadeOut('slow');
});
});
<script src="https://code.jquery.com/jquery-3.3.1.js" integrity="sha256-2Kok7MbOyxpgUVvAk/HJ2jigOSYS2auK4Pfzbm7uH60=" crossorigin="anonymous"></script>
<li> {{prod.category_name}} <a class="edit-category-item" data-parent="{{prod.parent_category_id}}" href="#"><span class="glyphicon glyphicon-pencil"></span></a> <a class="subtract-category-item" href="#"><span class="glyphicon glyphicon-minus"></span></a> <a class="add-category-item" href="#"><span class="glyphicon glyphicon-plus"></span></a></li>
<a class="edit-service-item" href="#"><span class="glyphicon glyphicon-pencil"></span></a> <a class="subtract-service-item" href="#"><span class="glyphicon glyphicon-minus"></span></a> <a class="add-service-item" href="#"><span class="glyphicon glyphicon-plus"></span></a>
<!-- Modal -->
<div style="display: none;" class="bg-modal">
<div class="modal-contents">
<div class="close">+</div>
<form action="">
<input type="text" placeholder="Name of category">
<input type="text" id="papa">
<a href="#" class="button">Submit</a>
</form>
</div>
</div>
答案 0 :(得分:0)
如果您不使用flask-admin通用删除和编辑视图,则可以公开一条将为您执行删除/编辑的路由,并将此路由添加为表单action
例如
class productView(BaseView):
form_columns = ['name','price']
@expose('/')
def index(self):
myServ = Product.query.all()
myProd = Productcategories.query.all()
return self.render('admin/products.html', myProd = myProd, myServ = myServ)
@expose('/custom_delete')
def custom_delete(self):
# write custom code to delete from the database
return self.render('admin/products.html', myProd = myProd, myServ = myServ)
然后在您的HTML中
<div style="display: none;" class="bg-modal">
<div class="modal-contents">
<div class="close">+</div>
<form action="{{url_for('.custom_delete')}}">
<input type="text" placeholder="Name of category">
<input type="text" id="papa">
<a href="#" class="button">Submit</a>
</form>
</div>
</div>
您的表单将提交到端点,您可以在其中处理后端的删除操作