国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

Vue實(shí)現(xiàn)數(shù)據(jù)表格合并列rowspan效果

2019-11-19 11:10:06
字體:
供稿:網(wǎng)友

背景

現(xiàn)實(shí)中會遇到很多需求,合并列,例如要顯示一個名學(xué)生的各門課程成績。

html實(shí)現(xiàn)

使用html實(shí)現(xiàn)是比較簡單的,利用table標(biāo)簽的rowspan屬性即可,代碼如下:

<table> <thead> <tr>  <th>姓名</th>  <th>課程數(shù)</th>  <th>課程名稱</th>  <th>成績</th> </tr> </thead> <tbody> <tr>  <td rowspan="3">  張三  </td>  <td rowspan="3">  3  </td>  <td>語文</td>  <td>100</td> </tr> <tr>  <td>數(shù)學(xué)</td>  <td>90</td> </tr> <tr>  <td>英語</td>  <td>80</td> </tr> </tbody></table>

數(shù)據(jù)結(jié)構(gòu)

在實(shí)際工程中,表格數(shù)據(jù)一般來自后端,以json格式發(fā)送到前端后,學(xué)生和課程是一對多的關(guān)系,json格式數(shù)據(jù)結(jié)構(gòu)如下:

[ { name: '張三', courses: [  {  name: '語文',  score: '100'  },  {  name: '數(shù)學(xué)',  score: '90'  },  {  name: '英語',  score: '80'  } ] }]

Vue實(shí)現(xiàn)

我們對比表格結(jié)構(gòu)和json數(shù)據(jù)結(jié)構(gòu),分析出結(jié)論如下:

1.實(shí)際上每個課程對應(yīng)表格的一行
2.如果是第一列第二列(學(xué)生姓名、學(xué)生課程數(shù)),則應(yīng)設(shè)置其rowspan值為該學(xué)生擁有的課程數(shù)
3.如果是第一列第二列,則每個學(xué)生只需要輸出1次該列,因?yàn)樾枰磳W(xué)生合并列后展示。

分析完1-3條后,代碼實(shí)現(xiàn)也就簡單了:

<html><head> <style> th {  border: 1px solid black;  width: 100px; } td {  border: 1px solid black; } </style></head><body> <div id="app"> <table>  <thead>  <th>姓名</th>  <th>課程數(shù)</th>  <th>課程名稱</th>  <th>成績</th>  </thead>  <tbody>  <template v-for="(item,index) in students">   <tr v-for="(m,i) in item.courses">   <!-- 第1列每個學(xué)生只需要展示1次 -->   <td v-if="i==0" :rowspan="item.courses.length">    {{item.name}}   </td>   <!-- 第2列每個學(xué)生只需要展示1次 -->   <td v-if="i==0" :rowspan="item.courses.length">    {{item.courses.length}}   </td>   <td>{{m.name}}</td>   <td>{{m.score}}</td>   </tr>  </template>  </tbody> </table> </div> <script src="https://cdn.bootcss.com/vue/2.6.10/vue.js"></script> <script> var vm = new Vue({  el: "#app",  data: {  students: [   {   name: '張三',   courses: [    {    name: '語文',    score: '100'    },    {    name: '數(shù)學(xué)',    score: '90'    },    {    name: '英語',    score: '80'    }   ]   },   {   name: '李四',   courses: [    {    name: '語文',    score: '100'    },    {    name: '數(shù)學(xué)',    score: '90'    }   ]   }  ]  } }); </script></body></html>

效果:

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持武林網(wǎng)。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 凤山县| 秭归县| 察雅县| 军事| 广宗县| 南平市| 乌拉特后旗| 沙河市| 沂水县| 南城县| 中阳县| 镇巴县| 皮山县| 黑河市| 赫章县| 大渡口区| 三穗县| 吉林省| 乳源| 班戈县| 安陆市| 翁牛特旗| 江城| 沙坪坝区| 和顺县| 湛江市| 玛曲县| 柳林县| 虹口区| 永新县| 永川市| 山阴县| 武乡县| 蓝田县| 乐都县| 淮阳县| 定兴县| 伊宁县| 林西县| 体育| 同江市|