gpt4 book ai didi

html - 固定表格中的列宽,可以搜索和隐藏列

转载 作者:行者123 更新时间:2023-11-28 05:29:54 25 4
gpt4 key购买 nike

如何将 width 应用于 table 列,以避免在未找到结果时更改列 width。此 table 中的列也可以动态隐藏,因此我无法在 th 元素上设置固定宽度。

<table>
<thead>
<tr>
<th>col1</th>
<th>col2 <i>Filter:</i> <input type="text" ng-model="searchText"></th>
<th ng-hide="hideCol3">col3 <button ng-click="hideCol3 = !hideCol3">Hide column</button></th>
</tr>
</thead>
<tbody>
<tr ng-repeat="d in data | filter: searchText">
<td>{{d.col1}}</td>
<td>{{d.col2}}</td>
<td ng-hide="hideCol3">{{d.col3}}</td>
</tr>
</tbody>
</table>

表中的列可能隐藏。因此,例如,如果我转到 plunker 示例,隐藏 col3,并搜索不存在的文本,假设“abc”是 width表格列宽变化。

我希望表格在某些列被隐藏时调整宽度(现在正在这样做),但是当我过滤并且没有结果时我不希望列宽改变

plunker

最佳答案

这是一种方法 https://jsfiddle.net/kblau237/qkn1z0LL/2/

<!DOCTYPE html>

<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>NNIndex</title>
<script src="~/Scripts/jquery-1.9.1.min.js"></script>
<script data-semver="1.2.13" src="http://code.angularjs.org/1.2.13/angular.js" data-require="angular.js@1.2.x"></script>
<script src="~/Scripts/app.js"></script>
<style>
th, td {
border: 1px solid black;
}

table {
width: 100%;
}
</style>
<script type="text/javascript">
$(function () {
//http://stackoverflow.com/questions/1636201/is-the-order-objects-are-return-by-a-jquery-selector-specified
var cols = $(".manageWidth"); //access each ids with [0]..[2] usejquery each
$.each(cols, function (index, value) {
$('<input>').attr({
type: 'hidden',
class: 'hdnDyn',
id: "hdnDyn" + index,
value: window.getComputedStyle(cols[index], null).getPropertyValue("width")
}).appendTo('body');

});

$("input").keyup(function () {
KeepWidthSame();
});

$(".col3").click(function () {
KeepWidthSame();
});

function KeepWidthSame() {
$("table").css("width", "auto");
var cols = $(".manageWidth");
$.each(cols, function (index, value) {
$(this).css("width", $(".hdnDyn")[index].value);
});
}
})

</script>
</head>
<body ng-app="plunker" ng-controller="MainCtrl">
<table>
<thead>
<tr>
<th class="manageWidth">col1</th>
<th class="manageWidth">col2 <i>Filter:</i> <input type="text" ng-model="searchText"></th>
<th class="manageWidth col3" ng-hide="hideCol3">col3 <button ng-click="hideCol3 = !hideCol3">Hide column</button></th>
</tr>
</thead>
<tbody>
<tr ng-repeat="d in data | filter: searchText">
<td>{{d.col1}}</td>
<td>{{d.col2}}</td>
<td ng-hide="hideCol3">{{d.col3}}</td>
</tr>
</tbody>
</table>
</body>
</html>

关于html - 固定表格中的列宽,可以搜索和隐藏列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38569202/

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