技术查缺补漏day03(vue3-具名插槽 el-table联合使用)

本文介绍了在Vue3中如何使用具名插槽,并结合Element UI的el-table组件进行自定义列表的实现。强调了查看官方文档对于深入学习的重要性,提供了具名插槽的写法示例以及插槽间传值的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

orz太久没更新了,应该会逐渐一口气补到day20左右

vue具名插槽(感悟到比起看别人的博客学习,还是更应该看官方文档,会学得更好些)

插槽 Slots | Vue.js

具名插槽写法

在一个 <BaseLayout> 组件中,有如下模板:

<div class="container">
  <header>
    <slot name="header"></slot>
  </header>
  <main>
    <slot></slot>
  </main>
  <footer>
    <slot name="footer"></slot>
  </footer>
</div>

在父组件中给对应的插槽传递内容

<BaseLayout>
  <template v-slot:header>
    <!-- header 插槽的内容放这里 -->
  </template>
</BaseLayout>

v-slot可以简写成#

#default会填入默认插槽中,即<slot></slot>中

<BaseLayout>
  <template #header>
    <h1>Here might be a page title</h1>
  </template>

  <template #default>
    <p>A paragraph for the main content.</p>
    <p>And another one.</p>
  </template>

  <template #footer>
    <p>Here's some contact info</p>
  </template>
</BaseLayout>

插槽间传值-默认插槽传值

 具名插槽传值

结合element-ui自定义列表

Element - The world's most popular Vue UI framework

### Vue 中结合 `el-table` 实现甘特图 在 Vue.js 开发环境中,Element Plus 的 `el-table` 是一种强大的组件,用于展示复杂的数据结构。然而,`el-table` 并未直接提供甘特图的功能支持。要实现甘特图效果,可以通过自定义列渲染的方式,在表格的右侧绘制时间轴和任务进度条。 以下是基于 VueElement Plus 的解决方案: #### 1. 数据准备 为了构建甘特图,需要设计合适的数据模型来存储任务的时间范围和其他属性。通常包括以下字段: - **name**: 任务名称。 - **start_date**: 起始日期。 - **end_date**: 结束日期。 - **progress**: 进度百分比(可选)。 ```javascript const tableData = [ { name: &#39;Task A&#39;, start_date: new Date(&#39;2023-09-01&#39;), end_date: new Date(&#39;2023-09-07&#39;), progress: 80 }, { name: &#39;Task B&#39;, start_date: new Date(&#39;2023-09-05&#39;), end_date: new Date(&#39;2023-09-12&#39;), progress: 60 } ]; ``` #### 2. 自定义甘特图表栏 通过 `el-table-column` 的 `scoped-slot` 功能,可以动态生成甘特图部分的内容。具体来说,利用 HTML `<div>` 或 SVG 来模拟甘特图的任务条形显示。 ##### 示例代码 ```vue <template> <el-table :data="tableData" style="width: 100%"> <!-- 左侧数据 --> <el-table-column prop="name" label="任务名称"></el-table-column> <el-table-column prop="start_date" label="开始日期"> <template #default="scope">{{ formatDate(scope.row.start_date) }}</template> </el-table-column> <el-table-column prop="end_date" label="结束日期"> <template #default="scope">{{ formatDate(scope.row.end_date) }}</template> </el-table-column> <!-- 右侧甘特图 --> <el-table-column label="甘特图" width="400"> <template #default="scope"> <div class="gantt-container"> <div class="task-bar" :style="{ left: calculatePosition(scope.row.start_date, scope.row.end_date), width: calculateWidth(scope.row.start_date, scope.row.end_date) }" ></div> </div> </template> </el-table-column> </el-table> </template> <script> export default { data() { return { tableData: [ { name: &#39;Task A&#39;, start_date: new Date(&#39;2023-09-01&#39;), end_date: new Date(&#39;2023-09-07&#39;) }, { name: &#39;Task B&#39;, start_date: new Date(&#39;2023-09-05&#39;), end_date: new Date(&#39;2023-09-12&#39;) } ] }; }, methods: { formatDate(date) { const options = { year: &#39;numeric&#39;, month: &#39;short&#39;, day: &#39;numeric&#39; }; return date.toLocaleDateString(undefined, options); }, calculatePosition(start, end) { // 假设整个甘特图宽度为 400px,计算起始位置 const totalDays = this.getTotalDays(); const daysFromStart = (this.getMinDate().getTime() - start.getTime()) / (1000 * 60 * 60 * 24); return `${(daysFromStart / totalDays) * 400}px`; }, calculateWidth(start, end) { // 计算任务持续天数占总天数的比例 const totalDays = this.getTotalDays(); const taskDuration = (end - start) / (1000 * 60 * 60 * 24); return `${(taskDuration / totalDays) * 400}px`; }, getTotalDays() { const minDate = this.getMinDate(); const maxDate = this.getMaxDate(); return (maxDate - minDate) / (1000 * 60 * 60 * 24); }, getMinDate() { return Math.min(...this.tableData.map(item => item.start_date)); }, getMaxDate() { return Math.max(...this.tableData.map(item => item.end_date)); } } }; </script> <style scoped> .gantt-container { position: relative; height: 20px; background-color: lightgray; } .task-bar { position: absolute; height: 100%; background-color: steelblue; } </style> ``` 上述代码实现了基本的甘特图功能[^1]。左侧展示了任务的基本信息,而右侧则通过 CSS 定位技术呈现了任务的时间跨度。 #### 3. 扩展功能建议 如果需要更复杂的交互体验,比如拖拽调整任务时间、缩放视图等功能,则可能需要引入专门的甘特图库,例如 DHTMLX Gantt 或者 vue-gantt-chart 等插件。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值