|
| 1 | +React 动态生成栅格布局 |
| 2 | + |
| 3 | + |
| 4 | +>作为 TerminalMACS 的一个子进程模块 - React Web管理端,使用Ant Design Pro作为框架。 |
| 5 | +
|
| 6 | +## 本文应用到的知识 |
| 7 | +- 1.样式文件less中方法的使用 |
| 8 | +- 2.for循环创建按钮和栅格布局(flex布局) |
| 9 | + |
| 10 | + |
| 11 | +## 1. 最终效果及源码链接 |
| 12 | + |
| 13 | +作为前端新手的我,做下面这个简单功能花了好几天时间,问了不少前端大佬(大佬们应该要不到半个小时,惭愧惭愧),现在回想问的问题都很基础(有点丢人,哈哈),多谢了哦。 |
| 14 | + |
| 15 | +先看看效果 |
| 16 | + |
| 17 | +<p align="center"> |
| 18 | + <img width="800px" src="https://dotnet9.com/wp-content/uploads/2020/05/dynamic_create_grid.gif"> |
| 19 | +</p> |
| 20 | + |
| 21 | +点击浏览源码:[开源项目对应源码](https://github.com/dotnet9/TerminalMACS/tree/master/src/TerminalMACS.Manager/TerminalMACS.ManagerForReact/src/pages/Grid/DynamicGridPage) |
| 22 | + |
| 23 | +## 2. 代码简单讲解 |
| 24 | + |
| 25 | +### 2.1 TypeScript代码 |
| 26 | + |
| 27 | +./src/pages/Grid/DynamicGridPage/index.tsx |
| 28 | +```TypeScript |
| 29 | +import React from 'react'; |
| 30 | +import styles from './index.less'; |
| 31 | +import { Button, Card } from 'antd'; |
| 32 | + |
| 33 | +interface IVideoPanelProps {} |
| 34 | + |
| 35 | +interface IVideoPanelSate { |
| 36 | + cardCount: number; |
| 37 | +} |
| 38 | + |
| 39 | +class VideoPanel extends React.Component<IVideoPanelProps, IVideoPanelSate> { |
| 40 | + constructor(props: Readonly<{}>) { |
| 41 | + super(props); |
| 42 | + this.state = { |
| 43 | + cardCount: 1, |
| 44 | + }; |
| 45 | + } |
| 46 | + |
| 47 | + // 动态生成Grid |
| 48 | + createCard() { |
| 49 | + var res = []; |
| 50 | + for (var i = 0; i < this.state.cardCount * this.state.cardCount; i++) { |
| 51 | + res.push(<Card className={styles['video_panel' + this.state.cardCount]} />); |
| 52 | + } |
| 53 | + return res; |
| 54 | + } |
| 55 | + |
| 56 | + // 动态生成控制按钮 |
| 57 | + createControlButon() { |
| 58 | + var res = []; |
| 59 | + const btnCount = 4; |
| 60 | + for (let i = 1; i <= btnCount; i++) { |
| 61 | + res.push( |
| 62 | + <Button |
| 63 | + key={i} |
| 64 | + className={styles['control_Button']} |
| 65 | + type="primary" |
| 66 | + onClick={() => { |
| 67 | + this.changeCardCount(i); |
| 68 | + }} |
| 69 | + > |
| 70 | + {i + '*' + i} |
| 71 | + </Button>, |
| 72 | + ); |
| 73 | + } |
| 74 | + |
| 75 | + return res; |
| 76 | + } |
| 77 | + |
| 78 | + // 修改显示的格子数 |
| 79 | + changeCardCount(count: any) { |
| 80 | + this.setState({ |
| 81 | + cardCount: count, |
| 82 | + }); |
| 83 | + } |
| 84 | + |
| 85 | + render() { |
| 86 | + return ( |
| 87 | + <div className={styles.main}> |
| 88 | + <div className={styles.main_child}> |
| 89 | + <div className={styles.left}> |
| 90 | + <div className={styles.left_top}></div> |
| 91 | + <div className={styles.left_bottom}></div> |
| 92 | + </div> |
| 93 | + <div className={styles.right}> |
| 94 | + <div className={styles.right_top}>{this.createCard()}</div> |
| 95 | + <div className={styles.right_bottom}>{this.createControlButon()}</div> |
| 96 | + </div> |
| 97 | + </div> |
| 98 | + </div> |
| 99 | + ); |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +export default VideoPanel; |
| 104 | +``` |
| 105 | +
|
| 106 | +代码不多,for循环生成控制按钮及栅格(未使用antd的Grid布局,简单使用div布局,flex很香),简化了很多代码。 |
| 107 | +
|
| 108 | +### 2.2 less样式文件 |
| 109 | +
|
| 110 | +./src/pages/Grid/DynamicGridPage/index.less |
| 111 | +```less |
| 112 | +@import '~antd/es/style/themes/default.less'; |
| 113 | + |
| 114 | +.main { |
| 115 | + position: absolute; |
| 116 | + width: 100%; |
| 117 | + height: 100%; |
| 118 | + background: red; |
| 119 | +} |
| 120 | + |
| 121 | +@ptcWidth: 250px; |
| 122 | +@btnAreaHeight: 50px; |
| 123 | +@videoPanelMargin: 5px; |
| 124 | + |
| 125 | +.main_child { |
| 126 | + display: flex; |
| 127 | + height: 100%; |
| 128 | +} |
| 129 | + |
| 130 | +.left { |
| 131 | + width: @ptcWidth; |
| 132 | + height: 100%; |
| 133 | + background: green; |
| 134 | +} |
| 135 | + |
| 136 | +.left_top { |
| 137 | + width: 100%; |
| 138 | + height: @ptcWidth; |
| 139 | + background: orange; |
| 140 | +} |
| 141 | + |
| 142 | +.left_bottom { |
| 143 | + width: 100%; |
| 144 | + height: calc(100% - @ptcWidth); |
| 145 | + background: #ccc; |
| 146 | +} |
| 147 | + |
| 148 | +.right { |
| 149 | + width: calc(100% - @ptcWidth); |
| 150 | + height: 100%; |
| 151 | + background: #f60; |
| 152 | +} |
| 153 | + |
| 154 | +.right_top { |
| 155 | + width: 100%; |
| 156 | + height: calc(100% - @btnAreaHeight); |
| 157 | + background: blue; |
| 158 | + display: flex; |
| 159 | + flex-wrap: wrap; |
| 160 | +} |
| 161 | + |
| 162 | +.right_bottom { |
| 163 | + width: 100%; |
| 164 | + height: @btnAreaHeight; |
| 165 | + background: #f90; |
| 166 | +} |
| 167 | + |
| 168 | +.control_Button { |
| 169 | + margin: 2px; |
| 170 | +} |
| 171 | + |
| 172 | +.video_panel(@count) { |
| 173 | + width: calc(100% / @count - @videoPanelMargin * 2); |
| 174 | + height: calc(100% / @count - @videoPanelMargin * 2); |
| 175 | + margin: @videoPanelMargin; |
| 176 | +} |
| 177 | + |
| 178 | +// 1*1 |
| 179 | +.video_panel1 { |
| 180 | + .video_panel(1); |
| 181 | +} |
| 182 | + |
| 183 | +// 2*2 |
| 184 | +.video_panel2 { |
| 185 | + .video_panel(2); |
| 186 | +} |
| 187 | + |
| 188 | +// 3*3 |
| 189 | +.video_panel3 { |
| 190 | + .video_panel(3); |
| 191 | +} |
| 192 | + |
| 193 | +// 4*4 |
| 194 | +.video_panel4 { |
| 195 | + .video_panel(4); |
| 196 | +} |
| 197 | +``` |
| 198 | +
|
| 199 | +1 * 1、2 * 2、3 * 3、4 * 4 生成的每个栅格的样式格式类似,只是长、宽比例区别,定义了方法(.video_panel(@count)),这是less的语法,极大的扩展了css功能,非常方便。 |
| 200 | +
|
| 201 | +## 3. 关于TerminalMACS及本React Web管理端 |
| 202 | +
|
| 203 | +### 2.1. TermainMACS |
| 204 | +
|
| 205 | +多终端资源管理与检测系统(后续可能转而写成博客系统),包含多个子进程模块,目前开发了Xamarin.Forms客户端、WPF管理端、React Web管理端、.NET Core WEB API服务端,下一步继续完善WPF管理端(IdentityServer 4研究中)。 |
| 206 | +
|
| 207 | +### 2.2. React Web管理端 |
| 208 | +
|
| 209 | +作为TerminalMACS系统的一个子进程模块,目前使用 [Ant Design Pro](https://pro.ant.design/index-cn) 搭建了框架,在平时工作中,添加一些测试Demo。 |
| 210 | +
|
| 211 | +## 3. 关于项目开源 |
| 212 | +
|
| 213 | +- 1. 开源项目地址:https://github.com/dotnet9/TerminalMACS |
| 214 | +- 2. 官方网站:https://dotnet9.com |
0 commit comments