Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save pbrocks/203a47ae17d005bf8a1157b7e80413b7 to your computer and use it in GitHub Desktop.
Save pbrocks/203a47ae17d005bf8a1157b7e80413b7 to your computer and use it in GitHub Desktop.
Filters wordpress admin tables live and sticks the headers to top on scroll. Do not use as is, problem with updating tables.
<?php
if ( !function_exists( 'stick_scroll_filter_wp_list_tables' ) ) {
function stick_scroll_filter_wp_list_tables() {
$screen = get_current_screen();
$base = $screen->base;
$type = $screen->post_type;
$name = $screen->id;
if ( (($base === 'edit') && ($type == 'page' || $type == 'post')) && (wp_script_is( 'jquery', 'done' )) )
{ ?>
<style type="text/css">
.filter-table .quick {
margin-left: 0.5em;
font-size: 0.8em;
text-decoration: none;
}
.fitler-table .quick:hover {
text-decoration: underline;
}
td.alt {
background-color: #ffc;
background-color: rgba(255, 255, 0, 0.2);
}
</style>
<!-- Sticky Table Headeers on Scroll and Filter Table CSS-->
<script type="text/javascript">
(function (e, i) {
"use strict";
function t(t, s) {
var d = this;
d.$el = e(t), d.el = t, d.id = l++, d.$window = e(i), d.$document = e(document), d.$el.bind("destroyed", e.proxy(d.teardown, d)), d.$clonedHeader = null, d.$originalHeader = null, d.isSticky = !1, d.hasBeenSticky = !1, d.leftOffset = null, d.topOffset = null, d.init = function () {
d.$el.each(function () {
var i = e(this);
i.css("padding", 0), d.$originalHeader = e("thead:first", this), d.$clonedHeader = d.$originalHeader.clone(), i.trigger("clonedHeader." + o, [d.$clonedHeader]), d.$clonedHeader.addClass("tableFloatingHeader"), d.$clonedHeader.css("display", "none"), d.$originalHeader.addClass("tableFloatingHeaderOriginal"), d.$originalHeader.after(d.$clonedHeader), d.$printStyle = e('<style type="text/css" media="print">.tableFloatingHeader{display:none !important;}.tableFloatingHeaderOriginal{position:static !important;}</style>'), e("head").append(d.$printStyle)
}), d.setOptions(s), d.updateWidth(), d.toggleHeaders(), d.bind()
}, d.destroy = function () {
d.$el.unbind("destroyed", d.teardown), d.teardown()
}, d.teardown = function () {
d.isSticky && d.$originalHeader.css("position", "static"), e.removeData(d.el, "plugin_" + o), d.unbind(), d.$clonedHeader.remove(), d.$originalHeader.removeClass("tableFloatingHeaderOriginal"), d.$originalHeader.css("visibility", "visible"), d.$printStyle.remove(), d.el = null, d.$el = null
}, d.bind = function () {
d.$scrollableArea.on("scroll." + o, d.toggleHeaders), d.isWindowScrolling || (d.$window.on("scroll." + o + d.id, d.setPositionValues), d.$window.on("resize." + o + d.id, d.toggleHeaders)), d.$scrollableArea.on("resize." + o, d.toggleHeaders), d.$scrollableArea.on("resize." + o, d.updateWidth)
}, d.unbind = function () {
d.$scrollableArea.off("." + o, d.toggleHeaders), d.isWindowScrolling || (d.$window.off("." + o + d.id, d.setPositionValues), d.$window.off("." + o + d.id, d.toggleHeaders)), d.$scrollableArea.off("." + o, d.updateWidth), d.$el.off("." + o), d.$el.find("*").off("." + o)
}, d.toggleHeaders = function () {
d.$el && d.$el.each(function () {
var i, t = e(this),
o = d.isWindowScrolling ? isNaN(d.options.fixedOffset) ? d.options.fixedOffset.outerHeight() : d.options.fixedOffset : d.$scrollableArea.offset().top + (isNaN(d.options.fixedOffset) ? 0 : d.options.fixedOffset),
l = t.offset(),
n = d.$scrollableArea.scrollTop() + o,
s = d.$scrollableArea.scrollLeft(),
a = d.isWindowScrolling ? n > l.top : o > l.top,
r = (d.isWindowScrolling ? n : 0) < l.top + t.height() - d.$clonedHeader.height() - (d.isWindowScrolling ? 0 : o);
a && r ? (i = l.left - s + d.options.leftOffset, d.$originalHeader.css({
position: "fixed",
"margin-top": 0,
left: i,
"z-index": 1
}), d.leftOffset = i, d.topOffset = o, d.$clonedHeader.css("display", ""), d.isSticky || (d.isSticky = !0, d.updateWidth()), d.setPositionValues()) : d.isSticky && (d.$originalHeader.css("position", "static"), d.$clonedHeader.css("display", "none"), d.isSticky = !1, d.resetWidth(e("td,th", d.$clonedHeader), e("td,th", d.$originalHeader)))
})
}, d.setPositionValues = function () {
var e = d.$window.scrollTop(),
i = d.$window.scrollLeft();
!d.isSticky || 0 > e || e + d.$window.height() > d.$document.height() || 0 > i || i + d.$window.width() > d.$document.width() || d.$originalHeader.css({
top: d.topOffset - (d.isWindowScrolling ? 0 : e),
left: d.leftOffset - (d.isWindowScrolling ? 0 : i)
})
}, d.updateWidth = function () {
if(d.isSticky) {
d.$originalHeaderCells || (d.$originalHeaderCells = e("th,td", d.$originalHeader)), d.$clonedHeaderCells || (d.$clonedHeaderCells = e("th,td", d.$clonedHeader));
var i = d.getWidth(d.$clonedHeaderCells);
d.setWidth(i, d.$clonedHeaderCells, d.$originalHeaderCells), d.$originalHeader.css("width", d.$clonedHeader.width())
}
}, d.getWidth = function (t) {
var o = [];
return t.each(function (t) {
var l, n = e(this);
if("border-box" === n.css("box-sizing")) l = n.outerWidth();
else {
var s = e("th", d.$originalHeader);
if("collapse" === s.css("border-collapse"))
if(i.getComputedStyle) l = parseFloat(i.getComputedStyle(this, null).width);
else {
var a = parseFloat(n.css("padding-left")),
r = parseFloat(n.css("padding-right")),
c = parseFloat(n.css("border-width"));
l = n.outerWidth() - a - r - c
} else l = n.width()
}
o[t] = l
}), o
}, d.setWidth = function (e, i, t) {
i.each(function (i) {
var o = e[i];
t.eq(i).css({
"min-width": o,
"max-width": o
})
})
}, d.resetWidth = function (i, t) {
i.each(function (i) {
var o = e(this);
t.eq(i).css({
"min-width": o.css("min-width"),
"max-width": o.css("max-width")
})
})
}, d.setOptions = function (t) {
d.options = e.extend({}, n, t), d.$scrollableArea = e(d.options.scrollableArea), d.isWindowScrolling = d.$scrollableArea[0] === i
}, d.updateOptions = function (e) {
d.setOptions(e), d.unbind(), d.bind(), d.updateWidth(), d.toggleHeaders()
}, d.init()
}
var o = "stickyTableHeaders",
l = 0,
n = {
fixedOffset: 0,
leftOffset: 0,
scrollableArea: i
};
e.fn[o] = function (i) {
return this.each(function () {
var l = e.data(this, "plugin_" + o);
l ? "string" == typeof i ? l[i].apply(l) : l.updateOptions(i) : "destroy" !== i && e.data(this, "plugin_" + o, new t(this, i))
})
}
})(jQuery, window);
</script><!-- Sticky Table Headeers on Scroll Script -->
<script type="text/javascript">
! function (i) {
i.fn.bindWithDelay = function (n, u, t, e, d) {
return i.isFunction(u) && (d = e, e = t, t = u, u = void 0), t.guid = t.guid || i.guid && i.guid++, this.each(function () {
function l() {
var n = i.extend(!0, {}, arguments[0]),
u = this,
l = function () {
o = null, t.apply(u, [n])
};
d || (clearTimeout(o), o = null), o || (o = setTimeout(l, e))
}
var o = null;
l.guid = t.guid, i(this).bind(n, u, l)
})
}
}(jQuery);
</script><!-- Bind With Delay for Filter Table Script -->
<script type="text/javascript">
! function (e) {
var t = e.fn.jquery.split("."),
i = parseFloat(t[0]),
a = parseFloat(t[1]);
e.expr[":"].filterTableFind = 2 > i && 8 > a ? function (t, i, a) {
return e(t).text().toUpperCase().indexOf(a[3].toUpperCase()) >= 0
} : jQuery.expr.createPseudo(function (t) {
return function (i) {
return e(i).text().toUpperCase().indexOf(t.toUpperCase()) >= 0
}
}), e.fn.filterTable = function (t) {
var i = {
autofocus: !1,
callback: null,
containerClass: "filter-table",
containerTag: "p",
hideTFootOnFilter: !1,
highlightClass: "alt",
inputSelector: null,
inputName: "",
inputType: "search",
label: "Filter:",
minRows: 8,
placeholder: "search this table",
quickList: [],
quickListClass: "quick",
quickListGroupTag: "",
quickListTag: "a",
visibleClass: "visible"
},
a = function (e) {
return e.replace(/&/g, "&amp;").replace(/"/g, "&quot;").replace(/</g, "&lt;").replace(/>/g, "&gt;")
},
l = e.extend({}, i, t),
n = function (e, t) {
var i = e.find("tbody");
"" === t ? (i.find("tr").show().addClass(l.visibleClass), i.find("td").removeClass(l.highlightClass), l.hideTFootOnFilter && e.find("tfoot").show()) : (i.find("tr").hide().removeClass(l.visibleClass), l.hideTFootOnFilter && e.find("tfoot").hide(), i.find("td").removeClass(l.highlightClass).filter(':filterTableFind("' + t.replace(/(['"])/g, "\\$1") + '")').addClass(l.highlightClass).closest("tr").show().addClass(l.visibleClass)), l.callback && l.callback(t, e)
};
return this.each(function () {
var t = e(this),
i = t.find("tbody"),
s = null,
r = null,
o = null,
c = !0;
"TABLE" === t[0].nodeName && i.length > 0 && (0 === l.minRows || l.minRows > 0 && i.find("tr").length > l.minRows) && !t.prev().hasClass(l.containerClass) && (l.inputSelector && 1 === e(l.inputSelector).length ? (o = e(l.inputSelector), s = o.parent(), c = !1) : (s = e("<" + l.containerTag + " />"), "" !== l.containerClass && s.addClass(l.containerClass), s.prepend(l.label + " "), o = e('<input type="' + l.inputType + '" placeholder="' + l.placeholder + '" name="' + l.inputName + '" />')), l.autofocus && o.attr("autofocus", !0), e.fn.bindWithDelay ? o.bindWithDelay("keyup", function () {
n(t, e(this).val())
}, 200) : o.bind("keyup", function () {
n(t, e(this).val())
}), o.bind("click search", function () {
n(t, e(this).val())
}), c && s.append(o), l.quickList.length > 0 && (r = l.quickListGroupTag ? e("<" + l.quickListGroupTag + " />") : s, e.each(l.quickList, function (t, i) {
var n = e("<" + l.quickListTag + ' class="' + l.quickListClass + '" />');
n.text(a(i)), "A" === n[0].nodeName && n.attr("href", "#"), n.bind("click", function (e) {
e.preventDefault(), o.val(i).focus().trigger("click")
}), r.append(n)
}), r !== s && s.append(r)), c && t.before(s))
})
}
}(jQuery);
</script>
<style type="text/css">
.tableFloatingHeaderOriginal {
background-color: #ffffff;
}
</style>
<script type="text/javascript">
jQuery(document).ready(function ($) {
$('table').stickyTableHeaders({
fixedOffset: 50
});
$('table.wp-list-table').filterTable({
inputSelector: '.search-box input[type=search][name=s]',
autofocus: true,
placeholder: 'What Are You Looking For?',
quickListTag: 'li',
hideTFootOnFilter: true
});
});
</script><!-- Filter Table Bind With Delay and Sticky Headers -->
<?php } // end if
} // end function
add_action( 'admin_print_footer_scripts', 'stick_scroll_filter_wp_list_tables', 99 );
} // end function_exists
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment