vue实现table课程表

2020-03-23 10:32:57

参考地址 vue实现table课程表

方案一

采用二维数组,固定显示每周7天及固定课节数,无数据的天及课节也会固定占位




<template>

    <div>

        <div>

            <div>


                <table>

                    <thead>

                        <tr>

                            <th>时间</th>

                            <th v-for="(weekNum, weekIndex) in classTableData.courses.length" :key="weekIndex"> {{'周' + digital2Chinese(weekIndex, 'week')}}</th>

                        </tr>

                    </thead>

                    <tbody>

                        <tr v-for="(lesson, lessonIndex) in classTableData.lessons" :key="lessonIndex">

                            <td>

                                <p>{{'第' + digital2Chinese(lessonIndex+1) + "节"}}</p>

                                <p>{{ lesson }}</p>

                            </td>


                            <td v-for="(course, courseIndex) in classTableData.courses" :key="courseIndex">

                                {{classTableData.courses[courseIndex][lessonIndex] || '-'}}

                            </td>

                        </tr>

                    </tbody>

                </table>

            </div>

        </div>

    </div>

</template>


<script>


export default {

    data() {

        return {

            classTableData: {

                lessons: [

                    '08:00-09:00',

                    '09:00-10:00',

                    '10:00-11:00',

                    '11:00-12:00',

                    '13:00-14:00',

                    '14:00-15:00',

                    '15:00-16:00',

                    '16:00-17:00'

                ],

                courses: [

                    ['', '', '', '', '', '', '', ''],

                    ['生物', '物理', '化学', '政治', '历史', '英语', '', '语文'],

                    ['语文', '数学', '英语', '历史', '', '化学', '物理', '生物'],

                    ['生物', '', '化学', '政治', '历史', '英语', '数学', '语文'],

                    ['语文', '数学', '英语', '历史', '政治', '', '物理', '生物'],

                    ['生物', '物理', '化学', '', '历史', '英语', '数学', '语文'],

                    ['语文', '数学', '英语', '', '', '', '', ''],

                ]

            }


        };

    },

    created() {

     // /* mock随机数据*/

        // Mock.mock({

//     lessons: ['08:00-09:00', '09:00-10:00', '10:00-11:00', '11:00-12:00', '13:00-14:00', '14:00-15:00', '15:00-16:00', '16:00-17:00'],

//     'courses|7': [['生物', '物理', '化学', '政治', '历史', '英语', '', '语文']]

// });

    },

    methods: {

        /**

        * 数字转中文

        * @param {Number} num 需要转换的数字

        * @param {String} identifier 标识符

        * @returns {String} 转换后的中文

        */

        digital2Chinese(num, identifier) {

            const character = ['零', '一', '二', '三', '四', '五', '六', '七', '八', '九', '十', '十一', '十二'];

            return identifier === 'week' && (num === 0 || num === 7) ? '日' : character[num];

        }

    }

};

</script>


<style scoped>

.class-table {

    .table-wrapper {

        width: 100%;

        height: 100%;

        overflow: auto;

    }

    .tabel-container {

        margin: 7px;


        table {

            table-layout: fixed;

            width: 100%;


            thead {

                background-color: #67a1ff;

                th {

                    color: #fff;

                    line-height: 17px;

                    font-weight: normal;

                }

            }

            tbody {

                background-color: #eaf2ff;

                td {

                    color: #677998;

                    line-height: 12px;

                }

            }

            th,

            td {

                width: 60px;

                padding: 12px 2px;

                font-size: 12px;

                text-align: center;

            }


            tr td:first-child {

                color: #333;

                .period {

                    font-size: 8px;

                }

            }

        }

    }

}

</style>

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

方案二

固定显示每周7天及固定课节数,无数据的天及课节也会固定占位



<template>

    <div>

        <div>

            <div>

                <table>

                    <thead>

                        <tr>

                            <th>时间</th>

                            <th v-for='(week, index) in weeks' :key='index'> {{'周' + digital2Chinese(index+1, 'week')}}</th>

                        </tr>

                    </thead>

                    <tbody>

                        <tr v-for='(item, index) in classTableData' :key='index'>

                            <td>

                                <p>{{'第' + digital2Chinese(index+1) + '节'}}</p>

                                <p>{{ item.classesTime }}</p>

                            </td>


                            <td v-for='(week, index) in weeks' :key='index'>

                                {{item[week]  || '-'}}

                            </td>

                        </tr>

                    </tbody>

                </table>

            </div>

        </div>

    </div>

</template>


<script>


export default {

    data() {

        return {

            weeks: ['monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday'],

            classTableData: [{

                'classesTime': '08:00-09:00',

                'monday': '物理',

                'tuesday': '英语',

                'wednesday': '政治',

                'thursday': '历史',

                'friday': '化学',

                'saturday': '历史',

                'sunday': '化学'

            }, {

                'classesTime': '09:00-10:00',

                'monday': '生物',

                'tuesday': '物理',

                'wednesday': '化学',

                'thursday': '英语',

                'friday': '化学',

                'saturday': '生物',

                'sunday': '化学'

            }, {

                'classesTime': '10:00-11:00',

                'monday': '生物',

                'tuesday': '物理',

                'wednesday': '生物',

                'thursday': '历史',

                'friday': '生物',

                'saturday': '英语',

                'sunday': '政治'

            }, {

                'classesTime': '11:00-12:00',

                'monday': '',

                'tuesday': '政治',

                'wednesday': '物理',

                'thursday': '政治',

                'friday': '历史',

                'saturday': '历史',

                'sunday': '生物'

            }, {

                'classesTime': '13:00-14:00',

                'monday': '生物',

                'tuesday': '历史',

                'wednesday': '历史',

                'thursday': '历史',

                'friday': '',

                'saturday': '英语',

                'sunday': '化学'

            }, {

                'classesTime': '14:00-15:00',

                'monday': '化学',

                'tuesday': '英语',

                'wednesday': '物理',

                'thursday': '化学',

                'friday': '语文',

                'saturday': '物理',

                'sunday': '英语'

            }, {

                'classesTime': '15:00-16:00',

                'monday': '历史',

                'tuesday': '历史',

                'wednesday': '语文',

                'thursday': '历史',

                'friday': '生物',

                'saturday': '英语',

                'sunday': ''

            }],

            tableShow: false

        };

    },

    created() {

     // /* mock随机数据*/

        //  Mock.mock({

        //     'data|7': [

        //         {

        //             'classesTime|+1': ['08:00-09:00', '09:00-10:00', '10:00-11:00', '11:00-12:00', '13:00-14:00', '14:00-15:00', '15:00-16:00', '16:00-17:00'],

        //             'monday|1': ['生物', '物理', '化学', '政治', '历史', '英语', '', '语文'],

        //             'tuesday|1': ['生物', '物理', '化学', '政治', '历史', '英语', '', '语文'],

        //             'wednesday|1': ['生物', '物理', '化学', '政治', '历史', '英语', '', '语文'],

        //             'thursday|1': ['生物', '物理', '化学', '政治', '历史', '英语', '', '语文'],

        //             'friday|1': ['生物', '物理', '化学', '政治', '历史', '英语', '', '语文'],

        //             'saturday|1': ['生物', '物理', '化学', '政治', '历史', '英语', '', '语文'],

        //             'sunday|1': ['生物', '物理', '化学', '政治', '历史', '英语', '', '语文']

        //         }

        //     ]

        // });

    },

    methods: {

        /**

        * 数字转中文

        * @param {Number} num 需要转换的数字

        * @param {String} identifier 标识符

        * @returns {String} 转换后的中文

        */

        digital2Chinese(num, identifier) {

            const character = ['零', '一', '二', '三', '四', '五', '六', '七', '八', '九', '十', '十一', '十二'];

            return identifier === 'week' && (num === 0 || num === 7) ? '日' : character[num];

        }

    }

};

</script>


<style scoped>

.class-table {

    .table-wrapper {

        width: 100%;

        height: 100%;

        overflow: auto;

    }

    .tabel-container {

        margin: 7px;


        table {

            table-layout: fixed;

            width: 100%;


            thead {

                background-color: #67a1ff;

                th {

                    color: #fff;

                    line-height: 17px;

                    font-weight: normal;

                }

            }

            tbody {

                background-color: #eaf2ff;

                td {

                    color: #677998;

                    line-height: 12px;

                }

            }

            th,

            td {

                width: 60px;

                padding: 12px 2px;

                font-size: 12px;

                text-align: center;

            }


            tr td:first-child {

                color: #333;

                .period {

                    font-size: 8px;

                }

            }

        }

    }

}

</style>

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

方案三

根据随机周数及随机课节数渲染




<template>

    <div>

        <div>

            <div>

                <table>

                    <thead>

                        <tr>

                            <th>时间</th>

                            <th v-for="(weekNum, weekIndex) in weeks" :key="weekIndex"> {{'周' + digital2Chinese(weekNum, 'week')}}</th>

                        </tr>

                    </thead>

                    <tbody>

                        <tr v-for="(courseNum, courseIndex) in coursesLen" :key="courseIndex">


                            <td>

                                <p>{{'第' + digital2Chinese(courseNum) + "节"}}</p>

                                <p>{{ classTableData.period[courseIndex] }}</p>

                            </td>


                            <td v-for="(weekNum, weekIndex) in weeks" :key="weekIndex">

                                {{ fieldCharacter(weekIndex, courseIndex) }}

                            </td>

                        </tr>

                    </tbody>

                </table>

            </div>

        </div>

    </div>

</template>


<script>

export default {

    data() {

        return {

            weeks: [], //周集合

            coursesLen: 0, //最大课节数

            classTableData: { //mock模拟的数据

                period: ['08:00-09:00', '09:00-10:00', '10:00-11:00', '11:00-12:00', '13:00-14:00', '14:00-15:00', '15:00-16:00', '16:00-17:00'], //时间段

                weekCourse: [

                    {

                        'week': 0,

                        'courses': [

                            { 'index': 1, 'title': '物理' },

                            { 'index': 3, 'title': '语文' }

                        ]

                    },

                    {

                        'week': 1,

                        'courses': [

                            { 'index': 3, 'title': '生物' },

                            { 'index': 4, 'title': '语文' },

                            { 'index': 5, 'title': '历史' },

                            { 'index': 6, 'title': '数学' },

                            { 'index': 7, 'title': '英语' },

                            { 'index': 8, 'title': '生物' },

                            { 'index': 1, 'title': '生物' }

                        ]

                    },

                    {

                        'week': 3,

                        'courses': [

                            { 'index': 5, 'title': '英语' },

                            { 'index': 6, 'title': '英语' },

                            { 'index': 7, 'title': '物理' },

                            { 'index': 8, 'title': '英语' },

                            { 'index': 1, 'title': '生物' },

                            { 'index': 2, 'title': '数学' },

                            { 'index': 3, 'title': '英语' }

                        ]

                    },

                    {

                        'week': 4,

                        'courses': [

                            { 'index': 4, 'title': '语文' },

                            { 'index': 5, 'title': '英语' },

                            { 'index': 6, 'title': '生物' },

                            { 'index': 7, 'title': '历史' }

                        ]

                    },

                    {

                        'week': 5,

                        'courses': [

                            { 'index': 8, 'title': '化学' },

                            { 'index': 1, 'title': '英语' }

                        ]

                    }

                ]

            }

        }

    },

    created() {

        this.updateData();

        this.initWeekCourses();

    },

    methods: {

        /**

         * 更新mock模拟的数据,对数据进行排序

         */

        updateData() {


            /* 将数据按从周日到周六排序 */

            this.classTableData.weekCourse.sort((a, b) => {

                return a.week - b.week;

            });


            /* 将数据按从第一节到第n节排序 */

            for (let v of this.classTableData.weekCourse) {

                for (let k in v) {

                    if (k === 'courses') {

                        v[k].sort((a, b) => {

                            return a.index - b.index;

                        });

                    }

                }

            }


            console.log(JSON.stringify(this.classTableData.weekCourse));

        },

        /**

         * 计算周数据及课节数

         */

        initWeekCourses() {

            const that = this;

            this.weeks = []; //周集合

            this.coursesLen = 0; //最大的课节数


            this.weeks = this.classTableData.weekCourse.map((item, index) => {

                for (let k in item) {

                    if (k === 'courses') {

                        let maxCoursesLen = 0;

                        /* 取出一周中最大的课节数及当天的最大课节数 */

                        for (let j of item[k]) {

                            j.index > that.coursesLen && (that.coursesLen = j.index); //取所有一周里最大课节值

                            j.index > maxCoursesLen && (maxCoursesLen = j.index); //取当天最大课节值

                        }



                        /* 如果当天的课节总数小于当天的最大课节值 */

                        if (item[k].length < maxCoursesLen) {

                            for (let i = 0; i < maxCoursesLen; i++) { //以最大课节值为终点遍历当天课节

                                if (!item[k][i] || item[k][i].index != (i + 1)) { //如果下标课节不存在或着与循环的下标不匹配

                                    item[k].splice(i, 0, ''); //填充空课节

                                }

                            }

                        }

                    }

                }

                return item.week;

            });


            console.log(JSON.stringify(this.classTableData.weekCourse));





        },

        /**

         * 处理格子数据,无数据转换为字符串'-'

         * @param {Number} weekIndex 周几对应的下标

         * @param {Number} courseNum 第几节课对应的下标

         * @returns {String} 返回对应的字符

         */

        fieldCharacter(weekIndex, courseIndex) {

            if (

                this.classTableData.weekCourse[weekIndex]

                &&

                this.classTableData.weekCourse[weekIndex].courses[courseIndex]

                &&

                this.classTableData.weekCourse[weekIndex].courses[courseIndex].index === courseIndex + 1

            ) {

                return this.classTableData.weekCourse[weekIndex].courses[courseIndex].title;

            }

            return '-';

        },



        /**

        * 数字转中文

        * @param {Number} num 需要转换的数字

        * @param {Boolean} identifier 标识符

        * @returns {String} 转换后的中文

        */

        digital2Chinese(num, identifier) {

            const character = ['零', '一', '二', '三', '四', '五', '六', '七', '八', '九', '十', '十一', '十二'];

            return identifier === 'week' && (num === 0 || num === 7) ? '日' : character[num];

        },

    }

};

</script>


<style scoped>

.class-table {

    .table-wrapper {

        width: 100%;

        height: 100%;

        overflow: auto;

    }

    .tabel-container {

        margin: 7px;


        table {

            table-layout: fixed;

            width: 100%;


            thead {

                background-color: #67a1ff;

                th {

                    color: #fff;

                    line-height: 17px;

                    font-weight: normal;

                }

            }

            tbody {

                background-color: #eaf2ff;

                td {

                    color: #677998;

                    line-height: 12px;

                }

            }

            th,

            td {

                width: 60px;

                padding: 12px 2px;

                font-size: 12px;

                text-align: center;

            }


            tr td:first-child {

                color: #333;

                .period {

                    font-size: 8px;

                }

            }

        }

    }

}

</style>

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237



  • 2020-11-22 23:00:16

    androidx中的lifecycle组件

    Lifecycle-aware components生命周期感知组件执行操作,以响应另一个组件生命周期状态的更改,例如Activity和Fragment。这些组件可以帮助您生成更有组织、更容易维护的轻量级代码。

  • 2020-11-22 23:02:50

    Android数据存储之DataBase的Room

    Room是Google在AndroidX中提供的一个ORM(Object Relational Mapping,对象关系映射)库。它是在SQLite上提供的一个抽象层,可以使用SQLite的全部功能,同时可以更好更便捷流畅地访问数据库。(关于AndroidX可以参考

  • 2020-11-22 23:04:39

    Android组件 LiveData与MutableLiveData教程

    LiveData与ViewMode是经常搭配在一起使用的,但是为了不太混乱,我还是拆分开来说明,此篇博客只讲解 LiveData 与 MutableLiveData的概念与使用方式(但是会涉及到ViewMode的部分代码).

  • 2020-11-22 23:14:52

    Dagger 2 在 Android 上的用法

    在前面的文章我们介绍了Dagger2 中的大部分注解的使用,接下来我们从源码角度分析下第一篇文章中例子的原理。

  • 2020-11-22 23:18:59

    Android开发从Dagger2迁移至Kodein的感受

    最近个人在尝试构建 Kotlin版本 的Android MVVM开发框架,在依赖注入框架的选型上,我最终选择了 Kodein 。这是一个非常轻量级的DI框架,相比于配置繁琐的Dagger(繁琐的配置也是导致Dagger学习成本一直居高不下的原因!),它的配置过程更清晰且简单,并且,这个库的源码也是 Kotlin 的。

  • 2020-11-22 23:25:56

    Dagger2源码解析inject过程

    添加inject后,通过编译生成的DaggerMainComponent类来导入,说明编译以后生成了一些类,那到底生成了什么类呢。 Module和Component又是什么,该怎么里理解 在这篇文章后里将一一讨论。

  • 2020-11-22 23:27:28

    dagger学习教程

    dagger android 学习(一):dagger基础使用 dagger android 学习(二):AndroidInjector的使用 dagger android 学习(三):ContributesAndroidInjector的进一步优化 dagger android 学习(四):基于dagger2的mvp架构

  • 2020-11-22 23:31:22

    Dagger2与AndroidInjector详解

    相信使用过Dagger开发Android应用的小伙伴会知道(如果你还不是很了解Daager,可以先看我之前的一篇基本介绍:Dagger2使用攻略),我们会在Activity或Fragment的生命周期方法中执行成员注入。比如这样:

  • 2020-11-23 08:52:59

    asm.js 和 Emscripten 入门教程

    asm.js 就是为了解决这两个问题而设计的:它的变量一律都是静态类型,并且取消垃圾回收机制。除了这两点,它与 JavaScript 并无差异,也就是说,asm.js 是 JavaScript 的一个严格的子集,只能使用后者的一部分语法。

  • 2020-11-23 09:11:07

    爬虫——记一次破解前端加密详细过程

    从最初使用webdriver+selenium爬虫到现在利用http请求解析html,经历过各种各样的问题,webdriver+selenium这种办法虽然万能,而且可以用JS写解析脚本方便调试,