Jquery 如何在计算宽度时省略最后一列
我有以下JQuery:Jquery 如何在计算宽度时省略最后一列,jquery,html,css,Jquery,Html,Css,我有以下JQuery: $(document).ready(function () { //Get the first tr var firstRow = $('.header'); //Clone the first row firstRow.clone().attr('class', 'fixedHeader').prependTo('#ResultsTable'); //Match the th widths of the cloned tr
$(document).ready(function () {
//Get the first tr
var firstRow = $('.header');
//Clone the first row
firstRow.clone().attr('class', 'fixedHeader').prependTo('#ResultsTable');
//Match the th widths of the cloned tr
firstRow.find('th').each(function (i) {
var thWidth = $(this).width();
$('.fixedHeader th').eq(i).css({
width: thWidth
});
});
});
代码获取第一行(标题)并复制并附加到表,并使其固定,以便表滚动且标题保持不变
如何修改代码,使其复制头的每一列(最后一列除外)
CSS:
因此,如果一个表中有7列,我希望它只复制6列。原因是我在运行时隐藏了最后一列,因此它会计算标题并在末尾显示一个空格
ASP.net代码:
<asp:GridView ID="BookingResults" ClientIDMode="Static" runat="server" EnableModelValidation="True" AutoGenerateColumns="False" AllowSorting="true" ForeColor="Black" Width="100%" HeaderStyle-CssClass="header">
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<asp:Label runat="server" ID="commHdr" Text="Show Guideline" CssClass="sg" />
</HeaderTemplate>
<ItemTemplate>
<asp:LinkButton runat="server" ID="btnShow3" CssClass="btnSearch3" Text="VIEW" OnClientClick="javascript:test(this);return false;"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="ID" HeaderText="ID" ItemStyle-VerticalAlign="Top" />
<asp:BoundField DataField="Topic" HeaderText="Topic" ItemStyle-VerticalAlign="Top" ItemStyle-CssClass="tTitle" />
<asp:BoundField DataField="Location" HeaderText="Location" ItemStyle-VerticalAlign="Top" />
<asp:BoundField DataField="Specialty" HeaderText="Specialty" ItemStyle-VerticalAlign="Top" />
<asp:TemplateField HeaderText="Provider">
<ItemTemplate>
<div id="dvHide" class="clsHide">
<asp:Label ID="lblEllipsis" runat="server" Text='<%#Eval("Provider") %>' ToolTip='<%#Eval("Provider") %>'></asp:Label>
</div>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Summary" ItemStyle-VerticalAlign="Top" ItemStyle-CssClass="sumM">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Summary") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("Summary") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Guideline" HeaderText="Guideline" ItemStyle-CssClass="gLine" HeaderStyle-CssClass="gLine" />
</Columns>
</asp:GridView>
您可以尝试firstRow.find('th')。not('last')。each(…)
或者firstRow.find('th:not('last'))。each(…)
将其压缩为一条语句。您可以尝试firstRow.find('th')。not('last')。each(…)
或者firstRow.find('th:not('last')。each(…)
将其压缩为一条语句。您可以使用.slice
firstRow.find('th').slice(0, -1).each(function () {
...
});
您可以使用
.slice
firstRow.find('th').slice(0, -1).each(function () {
...
});
我现在正在测试它。非常感谢。更新:那没用。它仍然显示:/I我现在正在测试它。非常感谢。更新:那没用。它仍然会显示:“
宽度
仍然是根据最后一列计算得出的:”/我只是不知道该包括什么。。。我应该包括生成的HTML的快照吗?我认为OP希望在执行每个之前删除最后一列,因此在计算宽度时,它会给出正确的值。假设有5列,但我们只需要前4列,因此在计算时,宽度返回25%,而不是20%。基本上,克隆的列数应该比原始列少1列(最后一列被删除)。这是运行中的代码,带有一个小演示。@BillCriswell我毫不怀疑它应该工作:)我的不是出于某种原因:宽度仍然是基于最后一列计算的:/我只是不知道该包括什么。。。我应该包括生成的HTML的快照吗?我认为OP希望在执行每个之前删除最后一列,因此在计算宽度时,它会给出正确的值。假设有5列,但我们只需要前4列,因此在计算时,宽度返回25%,而不是20%。基本上,克隆应该比原始的少1列(最后一列被删除)。这是运行中的代码,带有一个小演示。@BillCriswell我毫不怀疑它应该工作:)我的不是出于某种原因:/DoesfirstRow.clone().attr('class','fixedHeader')。find('th:last')。remove()
work?@Huangism我应该在哪里添加行还是无关紧要?实际上我现在不确定你到底想要什么,你想要什么?您看到的标题是fixedHeader类itemsI-understand。我更新了我的问题。我正在生成一个列(标题和正文)并将其隐藏在前端,因为我将使用每行中的文本作为列的其他函数。我希望这更有意义。firstRow.clone().attr('class','fixedHeader')。find('th:last')。remove()
work?@Huangism我应该在哪里添加行还是无关紧要?实际上我现在不确定最后你想要什么,你想要什么?您看到的标题是fixedHeader类itemsI-understand。我更新了我的问题。我正在生成一个列(标题和正文)并将其隐藏在前端,因为我将使用每行中的文本作为列的其他函数。我希望这更有意义。