我正在为项目使用devexpress,并且对编码还不陌生。我有一个带有复选框和提交按钮的数据网格。 当我选择一些复选框并点击提交按钮时,我想呈现一个新表。我通过使用带有ajax的“ getselectdRowKeys()”来获取所选复选框的值。但是没有任何效果。 1.如果成功通过警报,它将显示选定的复选框值。 2.如果我使用preventDefault(),则会在django views(filter_list)中获取选定的值,但是根据脚本发布内容时的脚本,应该在views.py中执行“ IF”语句,从而使“其他”部分。这没有发生。 3.还有其他替代方法可以实现这一目标吗? 请分享您的想法。
谢谢
HTML文件:
{% extends "base.html" %}
{% block body %}
{% if Vertical_header %}
<script>
$(document).ready(function(){
$("#gridContainer").dxDataGrid({
dataSource: {{Vertical_value|safe}},
showBorders: true,
keyExpr: "RGN",
showBorders: true,
selection: {
mode: "multiple"
},
filterRow: {
visible: true
},
columns: {{Vertical_header|safe}},
showBorders: true
});
});
</script>
<div class="demo-container">
<form id='my_form' method="post" action="{% url 'mytablesm' %}">
{% csrf_token %}
<div id="gridContainer"></div>
<button type="submit" class="btn btn-primary btn-lg"><i class="fa fa-angle-double-down"></i></button>
</form>
</div>
<script type="text/javascript">
var csrftoken = jQuery("[name=csrfmiddlewaretoken]").val();
function csrfSafeMethod(method) {
// these HTTP methods do not require CSRF protection
return (/^(GET|HEAD|OPTIONS|TRACE)$/.test(method));
}
$.ajaxSetup({
beforeSend: function(xhr, settings) {
if (!csrfSafeMethod(settings.type) && !this.crossDomain) {
xhr.setRequestHeader("X-CSRFToken", csrftoken);
}
}
});function csrfSafeMethod(method) {
// these HTTP methods do not require CSRF protection
return (/^(GET|HEAD|OPTIONS|TRACE)$/.test(method));
}
$.ajaxSetup({
beforeSend: function(xhr, settings) {
if (!csrfSafeMethod(settings.type) && !this.crossDomain) {
xhr.setRequestHeader("X-CSRFToken", csrftoken);
}
}
});
$(document).on('submit','#my_form',function data(e){
e.preventDefault();
var dataGrid = $("#gridContainer").dxDataGrid("instance");
var selectedKeys = dataGrid.getSelectedRowKeys();
$.ajax({
type: 'POST',
url: '{% url "mytablesm" %}',
data:{
regionwiselist:selectedKeys[0],
'csrfmiddlewaretoken': "{{ csrf_token }}"
},
success: function (){
// What has to be written here to pass the posted above data (selected keys) to my django view??
// In my django view I have to get the posted data in regionwise list which inturn renders the same html page runs the else part of the HTML pager
}
});
});
</script>
<button onclick="data()" type="button" name="button"></button>
{% else %}
<h2>kd: {{filter_list|safe}}</h2>
{% endif %}
{% endblock %}
Views.py:
def mytablesm(request):
if request.method == 'POST':
filter_list = request.POST.get('regionwiselist')
context={'filter_list':filter_list}
return render(request, 'mytables.html', context)
else:
sbc = MTD.pdobjects.all()
df = sbc.to_dataframe().reset_index(drop=True)
df1=pandas.pivot_table(df,index='RGN',columns='Vertical',values=['Sales_Value'],aggfunc='sum').replace(numpy.nan,0)
df1=df1.apply(lambda x: round(x, 2))
df1.reset_index(inplace=True)
dict1=df1.transpose().to_dict()
dict=[]
for k,v in dict1.items():
dict.append(v)
col=list(df1.columns)
context={
'Vertical_value':dict,
'Vertical_header':col
}
return render(request, 'mytables.html', context)