Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add jump to the specified page #304

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 53 additions & 1 deletion src/itables/html/datatables_template.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,29 @@
<table id="table_id"><thead><tr><th>A</th></tr></thead></table>
<style>
.change-into-page-button{
box-sizing: border-box;
display: inline-block;
min-width: 1.5em;
padding: .5em 1em;
margin-left: 2px;
text-align: center;
text-decoration: none!important;
cursor: pointer;
border-radius: 2px;
color: inherit!important;
border: 1px solid rgba(0,0,0,.3);
background: linear-gradient(to bottom,#e6e6e60d,#0000000d);
}


</style>
<div style="float: right; color: #BBB; font-size: 13.333px;">
<div style="margin-top: 10px;">
跳转到第 <input type="number" id="jumpPageInputtable_id" min="1" style="width: 35px;"> 页
<button id="jumpPageBtntable_id" class="change-into-page-button">跳转</button>
</div>
</div>

<link href="https://www.unpkg.com/dt_for_itables/dt_bundle.css" rel="stylesheet">
<script type="module">
import {DataTable, jQuery as $} from 'https://www.unpkg.com/dt_for_itables/dt_bundle.js';
Expand All @@ -12,6 +37,33 @@
dt_args["data"] = data;

// [pre-dt-code]
new DataTable(table, dt_args);
var dataTable = new DataTable(table, dt_args);

// ---------------------------hack for pagination jump start---------------------------------

$('#jumpPageBtntable_id').click(() => {
const pageNum = parseInt($('#jumpPageInputtable_id').val()) - 1;
const totalPages = dataTable.page.info().pages;

if (pageNum >= 0 && pageNum < totalPages) {
dataTable.page(pageNum).draw('page');
} else {
alert(`请输入有效的页码!(1-${totalPages})`);
}
});

setTimeout(() => {
document.querySelectorAll('.dt-paging').forEach(dtEndElement => {
if (dtEndElement.closest(`#${table.id}_wrapper`)) {
dtEndElement.addEventListener('click', event => {
const buttonText = event.target.textContent || event.target.innerText;
document.getElementById('jumpPageInputtable_id').value = parseFloat(buttonText.replace(/,/g, ""));
});
}
});
}, 500);
// ---------------------------hack for pagination jump end---------------------------------


});
</script>
8 changes: 8 additions & 0 deletions src/itables/javascript.py
Original file line number Diff line number Diff line change
Expand Up @@ -810,6 +810,14 @@ def html_table_from_template(
output = replace_value(output, 'dt_args["data"] = data;', "")
output = replace_value(output, "const data = [];", "")

# ---------------------------hack for pagination jump start---------------------------------

output = output.replace("jumpPageBtntable_id", "jumpPageBtn"+table_id)
output = output.replace("jumpPageInputtable_id", "jumpPageInput"+table_id)
output = output.replace("itable_id", table_id)

# ---------------------------hack for pagination jump end---------------------------------

return output


Expand Down