使用 jQuery 调整网格中所有可编辑字段的大小
我的一个 Web 表单上有一个 RadGrid,并且我正在使用 InPlace
(内联)编辑,底部有一个空行用于插入。
当网格加载时,使用 JavaScript(我假设)在渲染后自动调整列的大小(您实际上可以看到页面加载时调整的列),但列中的输入不会调整大小以适应调整列宽,留下大量空白。我尝试连接到自动调整列大小的 JavaScript 方法,但似乎不可能。
我想使用 jQuery 来查找列大小调整后的所有输入,并调整它们的大小以适合列的宽度。 唯一需要调整大小的输入是 TextBoxes
和 DropDowns
,而且我不需要计算宽度 - 只需设置将控件设置为 100%,以便它们填充列中的可用空间。
编辑
我还应该注意,输入在标记中设置为 100% 宽度,但在调整列大小时它们不会缩放。看来这种行为是不正确的。
RadGrid 确实有一个 ClientSettings 部分,您可以在其中订阅许多客户端事件,但是当网格自动调整大小时,调整大小事件不会触发;仅当用户手动调整列大小时。
一些精通 jQuery 的人可以建议我如何做到这一点吗?
以下是 RadGrid 的一些示例标记:
<telerik:RadGrid ID="grdVendorAddresses" runat="server">
<MasterTableView AutoGenerateColumns="false" InsertItemDisplay="Bottom" EditMode="InPlace" DataKeyNames="AddressID, AddressTypeID, IsActive">
<ItemStyle Font-Size="12px" Font-Names="Verdana" Wrap="false" />
<HeaderStyle Font-Size="12px" Font-Names="Verdana" Wrap="false" />
<AlternatingItemStyle Font-Size="12px" Font-Names="Verdana" Wrap="false" />
<Columns>
<telerik:GridEditCommandColumn UniqueName="EditButton" ButtonType="ImageButton" EditImageUrl="/images/edit_icon_pencil.png" InsertImageUrl="/images/save_icon_small.png" UpdateImageUrl="/images/save_icon_small.png" CancelImageUrl="/images/cancel_icon.png">
<ItemStyle HorizontalAlign="Center" Width="60" />
</telerik:GridEditCommandColumn>
<telerik:GridTemplateColumn HeaderText="Address" DataField="StreetAddress" Resizable="false">
<ItemStyle HorizontalAlign="Left" />
<HeaderStyle HorizontalAlign="Left" />
<ItemTemplate>
<%#Eval("StreetAddress")%>
</ItemTemplate>
<EditItemTemplate>
<div style="padding:5px 0px 5px 0px;">
<telerik:RadTextBox ID="txtStreetAddress" runat="server" Skin="Sunset" Text='<%#Bind("StreetAddress")%>' Font-Size="12px" Font-Names="Verdana" Width="100%"></telerik:RadTextBox>
</div>
</EditItemTemplate>
</telerik:GridTemplateColumn>
<telerik:GridTemplateColumn HeaderText="City" DataField="City">
<ItemStyle HorizontalAlign="Left" />
<HeaderStyle HorizontalAlign="Left" />
<ItemTemplate>
<%#Eval("City")%>
</ItemTemplate>
<EditItemTemplate>
<div style="padding:5px 0px 5px 0px;">
<telerik:RadTextBox ID="txtCity" runat="server" Skin="Sunset" Text='<%#Bind("City")%>' Font-Size="12px" Font-Names="Verdana" Width="100%"></telerik:RadTextBox>
</div>
</EditItemTemplate>
</telerik:GridTemplateColumn>
<telerik:GridTemplateColumn HeaderText="Country" DataField="CountryID">
<ItemStyle HorizontalAlign="Center" />
<HeaderStyle HorizontalAlign="Center" />
<ItemTemplate>
<%#Eval("CountryName")%>
</ItemTemplate>
<EditItemTemplate>
<div style="padding:5px 5px 5px 5px;">
<telerik:RadComboBox ID="ddlCountry" runat="server" Skin="Sunset" Font-Size="12px" Font-Names="Verdana" DataSourceID="ddsCountries" Height="200" DataTextField="CountryName" DataValueField="CountryID" Width="100%" SelectedValue='<%#Bind("CountryID")%>'></telerik:RadComboBox>
</div>
</EditItemTemplate>
</telerik:GridTemplateColumn>
</MasterTableView>
</telerik:RadGrid>
I have a RadGrid on one of my webforms, and I'm using InPlace
(inline) editing, with an empty row at the bottom for inserts.
When the grid loads, the columns are being automatically resized post-render (you can actually see the columns adjusted when the page is loading) using JavaScript (I'm assuming), but the inputs in the columns are not being resized to fit the adjusted column widths, which leaves a lot of white space. I have tried to hook into the JavaScript method that auto-resizes the columns, but it doesn't appear possible.
I would like to use jQuery to find all the inputs after the columns have been resized, and resize them to fit the width of the columns. The only inputs that need to be resized are TextBoxes
and DropDowns
, and I don't need to calculate the width - just set the width of the controls to 100%, so they fill the available space in the columns.
EDIT
I should also note that the inputs are set to 100% width in the markup, but they don't scale when the columns are resized. It seems like this behavior is incorrect.
The RadGrid does have a ClientSettings
section where you can subscribe to many client-side events, but the resizing events do not fire when the grid is auto-resized; only when a user resizes the column manually.
Can some jQuery savvy individuals please suggest some ideas on how I can do this?
Here is some sample markup of the RadGrid:
<telerik:RadGrid ID="grdVendorAddresses" runat="server">
<MasterTableView AutoGenerateColumns="false" InsertItemDisplay="Bottom" EditMode="InPlace" DataKeyNames="AddressID, AddressTypeID, IsActive">
<ItemStyle Font-Size="12px" Font-Names="Verdana" Wrap="false" />
<HeaderStyle Font-Size="12px" Font-Names="Verdana" Wrap="false" />
<AlternatingItemStyle Font-Size="12px" Font-Names="Verdana" Wrap="false" />
<Columns>
<telerik:GridEditCommandColumn UniqueName="EditButton" ButtonType="ImageButton" EditImageUrl="/images/edit_icon_pencil.png" InsertImageUrl="/images/save_icon_small.png" UpdateImageUrl="/images/save_icon_small.png" CancelImageUrl="/images/cancel_icon.png">
<ItemStyle HorizontalAlign="Center" Width="60" />
</telerik:GridEditCommandColumn>
<telerik:GridTemplateColumn HeaderText="Address" DataField="StreetAddress" Resizable="false">
<ItemStyle HorizontalAlign="Left" />
<HeaderStyle HorizontalAlign="Left" />
<ItemTemplate>
<%#Eval("StreetAddress")%>
</ItemTemplate>
<EditItemTemplate>
<div style="padding:5px 0px 5px 0px;">
<telerik:RadTextBox ID="txtStreetAddress" runat="server" Skin="Sunset" Text='<%#Bind("StreetAddress")%>' Font-Size="12px" Font-Names="Verdana" Width="100%"></telerik:RadTextBox>
</div>
</EditItemTemplate>
</telerik:GridTemplateColumn>
<telerik:GridTemplateColumn HeaderText="City" DataField="City">
<ItemStyle HorizontalAlign="Left" />
<HeaderStyle HorizontalAlign="Left" />
<ItemTemplate>
<%#Eval("City")%>
</ItemTemplate>
<EditItemTemplate>
<div style="padding:5px 0px 5px 0px;">
<telerik:RadTextBox ID="txtCity" runat="server" Skin="Sunset" Text='<%#Bind("City")%>' Font-Size="12px" Font-Names="Verdana" Width="100%"></telerik:RadTextBox>
</div>
</EditItemTemplate>
</telerik:GridTemplateColumn>
<telerik:GridTemplateColumn HeaderText="Country" DataField="CountryID">
<ItemStyle HorizontalAlign="Center" />
<HeaderStyle HorizontalAlign="Center" />
<ItemTemplate>
<%#Eval("CountryName")%>
</ItemTemplate>
<EditItemTemplate>
<div style="padding:5px 5px 5px 5px;">
<telerik:RadComboBox ID="ddlCountry" runat="server" Skin="Sunset" Font-Size="12px" Font-Names="Verdana" DataSourceID="ddsCountries" Height="200" DataTextField="CountryName" DataValueField="CountryID" Width="100%" SelectedValue='<%#Bind("CountryID")%>'></telerik:RadComboBox>
</div>
</EditItemTemplate>
</telerik:GridTemplateColumn>
</MasterTableView>
</telerik:RadGrid>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不需要javascript,用CSS就可以了
CSS
在您的情况下,您的列将根据内容调整大小,
然后在列调整大小完成后调用此脚本
No need for javascript, do it with CSS
CSS
In your case your columns will be resized by content do this
then call this script when column resize is finished
我同意 Senad 的观点,CSS 是正确的选择。
但是,如果您确实想要执行 JavaScript,这里是 docs:
并设置客户端设置:
在
ColumnResized
JavaScript 函数内,您可以直接设置子控件。I agree with Senad, CSS is the way to go.
But, if you really want to do JavaScript, here's the RadGrid function from the docs:
And set the client settings:
Inside the
ColumnResized
JavaScript function, you can set child controls directly.