Auto Resize TabContainer

本文介绍了一种解决TabContainer在动态加载TabPanels时无法自动调整边框大小的问题的方法。通过JavaScript调整TabContainer的高度以匹配当前TabPanel的高度,确保布局正确显示。
 

Introduction

While dynamically loading TabPanels into a TabContainer, I noticed that the border on theTabContainer wouldn't automatically resize itself. I also couldn't find anything on the web about it. Eventually, I set it to a certain height and hoped that no one would notice, but they did. The content ran on down the page, and theTabContainer border height was stuck at 400. I used this code to fix that problem.

Hope it works for you!

After creating this write-up, I also found that creating a panel (the equivalent of adiv tag, not a tab panel), setting the height to 100%, and wrapping all the controls in the tab with the panel works just as well - no JavaScript is needed when using this method.

I will leave this up in case someone needs to figure out how to resize a tab in JavaScript for some reason.

Using the code

Use the OnClientActiveTabChanged property to call some JavaScript.

<ajaxToolkit:TabContainer ID="tabContainer" runat="server" 
                          OnClientActiveTabChanged="doSizeUp" />

Then use this JavaScript function to resize:

function doSizeUp(){
    //get the tabContainer for later reference
    var tc = document.getElementById("<%=tabContainer.ClientId%>");
    
    //get the index of the tab you just clicked.
    var tabIndex = 
     parseInt($find("<%=tabContainer.ClientId%>").get_activeTabIndex(), 10);
    
    //set the tabcontainer height to the tab panel height.
    tc.childNodes[1].style.height = 
     tc.childNodes[1].childNodes[tabIndex].clientHeight;
}

Don't forget to call doSizeUp when the page loads. Here's something that does that:

var prm = Sys.WebForms.PageRequestManager.getInstance();
prm.add_pageLoaded(PageLoadedHandler);
function PageLoadedHandler(sender, args) {
    setTimeout('doSizeUp()', 1);
}

Points of Interest

Please be aware that I only checked this in IE6 and IE7, as that's all that's required when working on our intranet, but I believe it will work in most major browsers.

在 Vue 中,如果你看到 `props` 或属性名中出现类似 **`auto-resize`** 这样的命名,并且你在代码中使用时出现了**特殊符号划线(横线)被当作减法运算符处理**的问题,这是因为: > ❗ `auto-resize` 是一个包含 **连字符(kebab-case)** 的字符串,在 JavaScript 中,它会被解析器理解为“变量 auto 减去变量 resize”,从而导致语法错误或未定义行为。 --- ### ✅ 正确理解:`auto-resize` 是什么? #### 1. **HTML 属性命名惯例:kebab-case** Vue 组件的模板(template)中,推荐使用 **kebab-case(短横线分隔)** 来传递 prop: ```vue <MyComponent auto-resize="true" /> ``` 这是合法的 HTML 写法(因为 HTML 不区分大小写),Vue 会自动将 kebab-case 映射到 camelCase 的 props。 #### 2. **JavaScript 中必须用 camelCase** 但在 `.vue` 脚本部分(如 `<script setup>` 或 `props: {}` 定义中),你必须使用 **camelCase**: ```js const props = defineProps({ autoResize: Boolean // 注意:这里是 autoResize,不是 auto-resize }) ``` Vue 会自动把 HTML 中的 `auto-resize` 转换为 `autoResize`。 --- ### 🔧 示例完整代码 ```vue <!-- ParentComponent.vue --> <template> <div> <h3>父组件</h3> <!-- 使用 kebab-case 传参 --> <ChildComponent auto-resize :initial-value="100" /> </div> </template> <script setup> import ChildComponent from './ChildComponent.vue' // 示例数据 const initialValue = 100 </script> ``` ```vue <!-- ChildComponent.vue --> <template> <div :class="{ 'auto-resized': autoResize }"> <p>是否启用自动调整大小:{{ autoResize }}</p> <p>初始值:{{ initialValue }}</p> </div> </template> <script setup> // 定义 props(使用 camelCase) const props = defineProps({ autoResize: { type: Boolean, default: false }, initialValue: { type: Number, required: true } }) // 在 script 中只能通过 autoResize 访问,不能写成 auto-resize console.log('Auto Resize Enabled:', props.autoResize) </script> <style scoped> .auto-resized { border: 2px solid #007bff; padding: 16px; background-color: #f0f8ff; border-radius: 8px; } </style> ``` --- ### 📌 关键点总结 | 场景 | 写法 | 说明 | |------|------|------| | 模板中传参 | `auto-resize` | 使用 kebab-case(合法 HTML) | | script 中定义/使用 | `autoResize` | 使用 camelCase(合法 JS) | | 类型推导 | ✅ 支持自动映射 | Volar 插件能识别转换 | > Vue 编译器会在模板编译阶段自动将 `auto-resize` 转换为 `autoResize`,所以你可以放心使用。 --- ### ⚠️ 常见错误示例(会导致问题) ```vue <!-- ❌ 错误:在 JS 中使用带连字符的变量名 --> <script setup> const auto-resize = true // SyntaxError: Invalid left-hand side in assignment </script> ``` ```vue <!-- ❌ 错误:prop 名写错,不匹配 --> <ChildComponent auto-resize /> <!-- script 中却定义为 autoreSize(拼写错误) --> defineProps({ autoreSize: Boolean }) // ❌ 不生效! ``` --- ### 回答问题: 为什么 `props` 使用 `auto-resize` 会出错?如何正确使用带连字符的 prop? ### 解释: - `auto-resize` 是合法的 **HTML attribute 名称(kebab-case)**,用于在模板中传递 prop。 - 但它不是合法的 JavaScript 标识符,因此不能直接作为变量名(如 `auto-resize = xxx` 会报语法错误)。 - 在 `<script setup>` 中,必须使用对应的 **camelCase 形式(autoResize)** 来定义和访问该 prop。 - Vue 自动完成了 kebab-case 到 camelCase 的映射,开发者无需手动处理。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值