sort_order不会从django脆皮形式的toogle排序按钮更改
我正在使用django脆皮表单,并且有一个托格尔排序按钮可以通过asc
或desc
订单排序。
一个searchform
这样:
class SearchForm(forms.Form):
"""Text-searching form."""
...
sort_order = forms.CharField(required=False, widget=forms.HiddenInput)
....
def __init__(self, user, language=None, show_builder=True, **kwargs):
"""Generate choices for other components in the same project."""
self.user = user
self.language = language
super().__init__(**kwargs)
self.helper = FormHelper(self)
self.helper.disable_csrf = True
self.helper.form_tag = False
self.helper.layout = Layout(
Div(
....
Div(
Field("sort_by", template="snippets/sort-field.html"),
Field("sort_order", template="snippets/sort-order.html"),
css_class="btn-group search-group sort-field",
role="group",
),
css_class="btn-toolbar",
role="toolbar",
),
....
)
我有sort-header.html
这样的:
<div class="btn-group sort-order" role="group">
<button type="button" class="btn btn-default search-field query-sort-toggle">
<span title="Ascending" class="search-icon active asc">{% icon "sort-ascending.svg" %}</span>
<span title="Descending" class="search-icon desc">{% icon "sort-descending.svg" %}</span>
</button>
<input type="hidden" id="id_sort_order" name="sort_order" value="{{ sort_order|default:'asc' }}" aria-label="{% trans "Sort Order" %}" />
</div>
我有 Order 按钮:
$(".query-sort-toggle").click(function () {
var $this = $(this);
var $label = $this.find("span.search-icon");
$label.toggle();
var sort_order = $label.attr('class').replace('search-icon active', '')
var $toggleInput = $this.closest(".sort-order").find("input[name=sort_order]");
$toggleInput.val(sort_order)
....
if ($this.closest(".result-page-form").length) {
$this.closest("form").submit();
}
});
单击按钮时,此CSS隐藏了不活动的跨度:
.query-sort-toggle span:not(.active) {
display: none;
}
我有此切换按钮,并在单击时提交表单。
默认排序顺序为asc
订单,但是在表单提交后页面重新加载时,该按钮仍显示默认值。
我可以按asc
和desc
顺序排序,但是我无法更改span
的图标和sort_order < /代码>变量。
我在这里缺少什么?
I am using django crispy forms and I have a toogle sort button to sort by asc
or desc
order.
I have a SearchForm
like this:
class SearchForm(forms.Form):
"""Text-searching form."""
...
sort_order = forms.CharField(required=False, widget=forms.HiddenInput)
....
def __init__(self, user, language=None, show_builder=True, **kwargs):
"""Generate choices for other components in the same project."""
self.user = user
self.language = language
super().__init__(**kwargs)
self.helper = FormHelper(self)
self.helper.disable_csrf = True
self.helper.form_tag = False
self.helper.layout = Layout(
Div(
....
Div(
Field("sort_by", template="snippets/sort-field.html"),
Field("sort_order", template="snippets/sort-order.html"),
css_class="btn-group search-group sort-field",
role="group",
),
css_class="btn-toolbar",
role="toolbar",
),
....
)
I have the sort-header.html
like this:
<div class="btn-group sort-order" role="group">
<button type="button" class="btn btn-default search-field query-sort-toggle">
<span title="Ascending" class="search-icon active asc">{% icon "sort-ascending.svg" %}</span>
<span title="Descending" class="search-icon desc">{% icon "sort-descending.svg" %}</span>
</button>
<input type="hidden" id="id_sort_order" name="sort_order" value="{{ sort_order|default:'asc' }}" aria-label="{% trans "Sort Order" %}" />
</div>
and jquery
for handling click event on sort order
button:
$(".query-sort-toggle").click(function () {
var $this = $(this);
var $label = $this.find("span.search-icon");
$label.toggle();
var sort_order = $label.attr('class').replace('search-icon active', '')
var $toggleInput = $this.closest(".sort-order").find("input[name=sort_order]");
$toggleInput.val(sort_order)
....
if ($this.closest(".result-page-form").length) {
$this.closest("form").submit();
}
});
When the button is clicked, the inactive span gets hidden by this css:
.query-sort-toggle span:not(.active) {
display: none;
}
I have this toggle button and form gets submitted on the click.
The default sort order is asc
order but when page reloads after the form submittion, the button still shows the default value.
I am able to sort by asc
and desc
order but I am not able to change the icon of the span
and the sort_order
variable.
What I am missing here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以在JavaScript中添加一些调试代码,并查看浏览器控制台,以查看是否将正确的值设置为您的表单。
您甚至可以使用视图来验证提交的数据,
如果您确保提交正确的值,则可以调整模板以显示所选排序订单图标
You could add some debug code to your JavaScript and look at the browser console to see if the right value is set to your form.
You could even use your view to verify the submitted data
If you made sure the right value is submitted, you can adjust the template to show the selected sort order icon