我使用bootstap-table的分页,搜索和可点击行的实现。当未使用搜索过滤器时,该表按预期工作,即,当单击该行时,它转到正确的链接。但是,在使用搜索过滤器时,表的逻辑认为第一行仍然是第一项,这不是真的,因为它基于用户输入。
有没有办法可以在输入字段的按键事件上刷新jQuery,请记住搜索的输入字段在bootstrap-table.js文件中?
html / django代码
<link href={% static "css/bootstrap-table.css" %} rel="stylesheet">
<script type="text/javascript" src={% static "js/bootstrap-table.js"%}></script>
<h1>Book Exchange Library</h1>
<table class="table table-condensed" id="table" data-url={% static "json/data.json" %} data-height="619" data-pagination="true" data-search="true">
<thead>
<tr>
<th data-field="title">Title</th>
<th data-field="author">Author</th>
<th data-field="price">Price</th>
<th id="id_col" data-field="pk">ID</th>
</tr>
</thead>
</table>
<!-- need this script to refresh on keypress -->
<script>
$(function() {
$("#table").bootstrapTable({
onClickRow: function(row) {
window.location.href = "/books/get/" + row.pk;
}
});
});
</script>
bootstrap-table.js片段(可能会有帮助)
if (this.options.search) {
html = [];
html.push(
'<div class="pull-right search">',
sprintf('<input id="search" class="form-control" type="text" placeholder="%s">',
this.options.formatSearch()),
'</div>');
this.$toolbar.append(html.join(''));
$search = this.$toolbar.find('.search input');
$search.off('keyup').on('keyup', $.proxy(this.onSearch, this));
}
};
答案 0 :(得分:1)
看看jQuery's .keypress()功能。有了这个,你可以这样做:
$("#table").keypress(function() {
$("#table").bootstrapTable({
onClickRow: function(row) {
window.location.href = "/books/get/" + row.pk;
}
});
}
它可能不是您问题的完美解决方案;您可能必须更改调用.keypress的元素。但是只要有一个按键事件就会运行你在函数体中放入的任何代码,这就是你正在寻找的东西。