gpt4 book ai didi

javascript - 使用 ng-hide 通过 colResizable 切换表中的列

转载 作者:行者123 更新时间:2023-12-02 16:22:20 25 4
gpt4 key购买 nike

我创建了一个简单的指令,它利用 colResizable 插件,在渲染后简单地激活表格上的插件:

app.directive("columnResizable", this.columnResizable = function($timeout) {
return {
restrict: "A",
link: function(scope, element, attrs) {
// Initialize colResizable plugin after DOM
$timeout(function() {
element.colResizable({
liveDrag:true,
postbackSafe: true,
partialRefresh: true,
minWidth: 100
});
});
}
}
});

这个指令工作得很好,直到我需要添加一个功能来隐藏和显示表中的列。我使用 ng-hide 并通过更改 localStorage bool 变量来打开或关闭列。如果我从“显示全部”状态开始,列将按预期隐藏/显示。但拒绝显示我是否从隐藏状态开始:

<button class="btn btn-default"
ng-class="{active: emailHidden}"
ng-click="emailHidden = !emailHidden">Hide Email</button>

<table column-resizable class="table table-bordered table-striped">
<thead>
<tr>
<th>Name</th>
<th ng-hide="emailHidden">Email</th>
</tr>
</thead>
<tbody ng-if="!scheduleEntries || scheduleEntries.length == 0">
<tr>
<td>Foo Bar</td>
<td ng-hide="emailHidden">foo@bar.com</td>
</tr>
</tbody>
</table>

我创建了一个带有常规 bool 变量的plunker。如果您将 $scope.emailHidden 变量更改为开始隐藏,您可以看到单击按钮时“电子邮件”列将不会显示:http://plnkr.co/edit/Y20BH2

最佳答案

我最终在指令内为 emailHidden 变量添加了一个观察器,一旦更改,它就会重置列宽并重新初始化 colResizable 插件:

scope.$watch('emailHidden', function(newVal) {
element.find('th').css('width','auto');
$timeout(function() {
element.colResizable({
liveDrag:true,
postbackSafe: true,
partialRefresh: true,
minWidth: 100
});
});
});

updated plunker

如果有人有更好的方法来解决这个问题,我将不胜感激。最好是不涉及 $watch 的东西

关于javascript - 使用 ng-hide 通过 colResizable 切换表中的列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28995236/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com