粘头不会根据我的变化更新
Sticky header wont update according to my changes
我有很多用Angular创建的复选框。每个复选框控制表中列的外观。因此,如果我点击3号复选框,那么表的3号列将不再可见。
我通过简单地执行
来实现这一点$('#dynamic tr > *:nth-child('+idx+')').toggle();
我偶然发现了这篇文章,我想在我的表中使用它。的事情是,粘头是从javascript文件创建的,不会遵循我的实际表。因此,当第2列应该被删除时,它将坚持显示它。
我认为,如果我运行这个每次我隐藏或显示一个列,然后它会正确更新,但如果你这样做,即使标题确实更新,由于某种原因,很多空白空间开始出现在每次点击后。如果点击次数超过几次,页面就会变慢。
由于粘头的代码是在javascript代码中生成的,我试图隐藏和显示适当的项目,就像我用实际的表做的一样。
$('.sticky-thead th > *:nth-child('+idx+')').toggle();
这没有任何影响。有人能想出一个更好的方法来使粘头动态,所以它只显示那些在我的表中可见的项目,在每一刻?
编辑
我提到的javascript文件包含以下代码,用于生成一种粘头。
$(function(){
$('table').each(function() {
if($(this).find('thead').length > 0 && $(this).find('th').length > 0) {
var $w = $(window),
$t = $(this),
$thead = $t.find('thead').clone(),
$col = $t.find('thead, tbody').clone();
$t
.addClass('sticky-enabled')
.css({
margin: 0,
width: '100%'
}).wrap('<div class="sticky-wrap" />');
if($t.hasClass('overflow-y')) $t.removeClass('overflow-y').parent().addClass('overflow-y');
$t.after('<table class="sticky-thead" />');
if($t.find('tbody th').length > 0) {
$t.after('<table class="sticky-col" /><table class="sticky-intersect" />');
}
var $stickyHead = $(this).siblings('.sticky-thead'),
$stickyCol = $(this).siblings('.sticky-col'),
$stickyInsct = $(this).siblings('.sticky-intersect'),
$stickyWrap = $(this).parent('.sticky-wrap');
$stickyHead.append($thead);
$stickyCol
.append($col)
.find('thead th:gt(0)').remove()
.end()
.find('tbody td').remove();
$stickyInsct.html('<thead><tr><th>'+$t.find('thead th:first-child').html()+'</th></tr></thead>');
var setWidths = function () {
$t
.find('thead th').each(function (i) {
$stickyHead.find('th').eq(i).width($(this).width());
})
.end()
.find('tr').each(function (i) {
$stickyCol.find('tr').eq(i).height($(this).height());
});
$stickyHead.width($t.width());
$stickyCol.find('th').add($stickyInsct.find('th')).width($t.find('thead th').width())
},
repositionStickyHead = function () {
var allowance = calcAllowance();
if($t.height() > $stickyWrap.height()) {
if($stickyWrap.scrollTop() > 0) {
$stickyHead.add($stickyInsct).css({
opacity: 1,
top: $stickyWrap.scrollTop()
});
} else {
$stickyHead.add($stickyInsct).css({
opacity: 0,
top: 0
});
}
} else {
if($w.scrollTop() > $t.offset().top && $w.scrollTop() < $t.offset().top + $t.outerHeight() - allowance) {
$stickyHead.add($stickyInsct).css({
opacity: 1,
top: $w.scrollTop() - $t.offset().top
});
} else {
$stickyHead.add($stickyInsct).css({
opacity: 0,
top: 0
});
}
}
},
repositionStickyCol = function () {
if($stickyWrap.scrollLeft() > 0) {
$stickyCol.add($stickyInsct).css({
opacity: 1,
left: $stickyWrap.scrollLeft()
});
} else {
$stickyCol
.css({ opacity: 0 })
.add($stickyInsct).css({ left: 0 });
}
},
calcAllowance = function () {
var a = 0;
$t.find('tbody tr:lt(3)').each(function () {
a += $(this).height();
});
if(a > $w.height()*0.25) {
a = $w.height()*0.25;
}
// Add the height of sticky header
a += $stickyHead.height();
return a;
};
setWidths();
$t.parent('.sticky-wrap').scroll($.throttle(250, function() {
repositionStickyHead();
repositionStickyCol();
}));
$w
.load(setWidths)
.resize($.debounce(250, function () {
setWidths();
repositionStickyHead();
repositionStickyCol();
}))
.scroll($.throttle(250, repositionStickyHead));
}
});
});
通过id (#
)选择表。对于页面上的每个元素,每个id都应该是唯一的。如果脚本克隆表,它将无法工作。请尝试使用class属性。
相关文章:
- 在我的网站上创建一个在1-10之间不断变化的数字时遇到了问题.Javascript
- jQuery没有'我不认识阶级的变化
- 为什么我的用户范围的自定义维度会根据过去的匹配而发生变化
- jquery函数不反映类的变化使我的另一个事件
- Angular ngClass 没有更新我的类,即使我的条件按预期发生了变化
- 我如何使用 AngularJS 对窗口大小的变化做出反应
- 我的手机间隙应用程序没有变化
- 摘要循环显示我的数组没有变化(视图不会刷新)
- RequestAnimationFrame调用时间根据我的画布大小而变化
- 角度2.x变化<标题>在头脑中(在我的应用程序之外)
- 我应该在什么时候用CSS和Javascript控制背景图像的变化
- 当我的函数在javascript中运行时,我的随机数生成器不断变化
- 为什么我的变量“c”发生了变化
- 我如何使用jQueryUI日期picker onSelect,但也在我的主干视图的变化事件
- 由于缓存,我的网页没有加载变化
- 当我不知道jQuery的变化时刻时,触发隐藏字段的变化事件
- 当我插入MySQL时,我的代码发生了变化
- 当我将字符串从Javascript传递到Java applet时,字符串会发生奇怪的变化
- 如何创建不同的按钮导航列表,所以当我点击按钮的内容/文本将根据我点击按钮的变化
- 粘头不会根据我的变化更新