Skip to content

Commit

Permalink
Add datatables order.neutral plugin and use it to allow resetting of …
Browse files Browse the repository at this point in the history
…sort order on datatables
  • Loading branch information
wormania committed Aug 17, 2023
1 parent 29c1a25 commit bc41bd3
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
17 changes: 17 additions & 0 deletions bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -77192,6 +77192,23 @@ const applyDatatables = () => {
}
}
});

// Setup a custom handler to reset sort order after descending instead of going back to ascending
// Accomplished by finding and intercepting the mutation of the sorting th's class from desc to asc
const callback = (mutationList) => {
for (const mutation of mutationList) {
if (mutation.type === "attributes" &&
mutation.attributeName == "class" &&
mutation.oldValue.includes("sorting_desc") &&
mutation.target.classList.contains("sorting_asc")
) {
$(`#${element.id}`).DataTable().order.neutral().draw();
}
}
};
document.querySelectorAll('th.sorting').forEach((el) => {
new MutationObserver(callback).observe(el, { attributes: true, attributeFilter: ['class'], attributeOldValue: true });
});
} catch(e){}
});
}
Expand Down
1 change: 1 addition & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
<!--Datatables-->
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/v/bs5/dt-1.13.1/cr-1.6.1/datatables.min.css"/>
<script type="text/javascript" src="https://cdn.datatables.net/v/bs5/dt-1.13.1/cr-1.6.1/datatables.min.js"></script>
<script type="text/javascript" src="https://cdn.datatables.net/plug-ins/1.13.6/api/order.neutral().min.js"></script>

<!--Simple Markdown Editor-->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/simplemde/latest/simplemde.min.css">
Expand Down
17 changes: 17 additions & 0 deletions scripts/datatables.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,23 @@ const applyDatatables = () => {
}
}
});

// Setup a custom handler to reset sort order after descending instead of going back to ascending
// Accomplished by finding and intercepting the mutation of the sorting th's class from desc to asc
const callback = (mutationList) => {
for (const mutation of mutationList) {
if (mutation.type === "attributes" &&
mutation.attributeName == "class" &&
mutation.oldValue.includes("sorting_desc") &&
mutation.target.classList.contains("sorting_asc")
) {
$(`#${element.id}`).DataTable().order.neutral().draw();
}
}
};
document.querySelectorAll('th.sorting').forEach((el) => {
new MutationObserver(callback).observe(el, { attributes: true, attributeFilter: ['class'], attributeOldValue: true });
});
} catch(e){}
});
}
Expand Down

0 comments on commit bc41bd3

Please sign in to comment.