index.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. <template>
  2. <d2-container>
  3. <template slot="header"
  4. style="padding: 5px;">
  5. <el-form :model="dutyDetail"
  6. label-width="100px">
  7. <el-row :gutter="20">
  8. <el-col :span="8">
  9. <el-form-item label="标题"
  10. label-width="120px">
  11. {{dutyDetail.Title}}
  12. </el-form-item>
  13. </el-col>
  14. <el-col :span="8">
  15. <el-form-item label="学年"
  16. label-width="120px">
  17. {{dutyDetail.Year}}年
  18. </el-form-item>
  19. </el-col>
  20. <el-col :span="8">
  21. <el-form-item label="学期"
  22. label-width="120px">
  23. {{this.termName}}
  24. </el-form-item>
  25. </el-col>
  26. <el-col :span="8">
  27. <el-form-item label="值班人员"
  28. size="mini"
  29. label-width="120px">
  30. <el-tag @click="personClick(item.ItemValue)"
  31. v-for="item in PeopleList"
  32. :key="item.ItemValue"
  33. :label="item.ItemValue">{{item.ItemName}}</el-tag>
  34. </el-form-item>
  35. </el-col>
  36. <el-col :span="8">
  37. <el-form-item label="地点"
  38. size="mini"
  39. label-width="120px">
  40. <el-checkbox-group v-model="selectLocal">
  41. <el-checkbox v-for="item in LocalList"
  42. :label="item.ItemValue"
  43. :key="item.ItemValue">{{item.ItemName}}</el-checkbox>
  44. </el-checkbox-group>
  45. </el-form-item>
  46. </el-col>
  47. <el-col :span="8">
  48. <el-form-item label="值班时间"
  49. size="mini"
  50. label-width="120px">
  51. <el-checkbox-group v-model="selectTime">
  52. <el-checkbox v-for="item in TimeList"
  53. :label="item.ItemValue"
  54. :key="item.ItemValue">{{item.ItemName}}</el-checkbox>
  55. </el-checkbox-group>
  56. </el-form-item>
  57. </el-col>
  58. </el-row>
  59. <el-button size="mini"
  60. @click="addList()"
  61. type="success"
  62. style="margin-right:6px">生成表格</el-button>
  63. <el-button size="mini"
  64. type="primary"
  65. style="margin-left:10px"
  66. @click="addDuytDetail()">保存</el-button>
  67. <el-button size="mini"
  68. style="margin-right:6px"
  69. @click="closeWindow()">关闭</el-button>
  70. </el-form>
  71. </template>
  72. <el-table :data="list"
  73. border
  74. fit
  75. tooltip-effect="dark"
  76. style="width: 100%"
  77. height="100%"
  78. @cell-click="cellclick"
  79. @header-click="headclick"
  80. :cell-class-name="cellBg"
  81. :key="refresh">
  82. <el-table-column label="操作"
  83. width="180px"
  84. align="center"
  85. fixed='right'>
  86. <template slot-scope="scope">
  87. <el-button size="mini"
  88. type="danger"
  89. title="删除"
  90. @click="deleteRow(scope.row.Id)"
  91. style="margin-left:5px;"
  92. icon="el-icon-delete"
  93. circle></el-button>
  94. </template>
  95. </el-table-column>
  96. <el-table-column fit
  97. prop="Local"
  98. min-width="160px"
  99. label="地点"
  100. align="center"
  101. show-overflow-tooltip
  102. :formatter="formatLocal"></el-table-column>
  103. <el-table-column prop="Time"
  104. label="时间段"
  105. align="center"
  106. min-width="120px"
  107. show-overflow-tooltip
  108. :formatter="formatTime"></el-table-column>
  109. <el-table-column prop="Monday"
  110. label="周一"
  111. align="center"
  112. min-width="120px"
  113. show-overflow-tooltip
  114. :formatter="formatPerson"></el-table-column>
  115. <el-table-column prop="Tuesday"
  116. label="周二"
  117. align="center"
  118. min-width="120px"
  119. show-overflow-tooltip
  120. :formatter="formatPerson"></el-table-column>
  121. <el-table-column prop="Wednesday"
  122. label="周三"
  123. align="center"
  124. min-width="120px"
  125. show-overflow-tooltip
  126. :formatter="formatPerson"></el-table-column>
  127. <el-table-column prop="Thursday"
  128. label="周四"
  129. align="center"
  130. min-width="120px"
  131. show-overflow-tooltip
  132. :formatter="formatPerson"></el-table-column>
  133. <el-table-column prop="Friday"
  134. label="周五"
  135. align="center"
  136. min-width="120px"
  137. show-overflow-tooltip
  138. :formatter="formatPerson"></el-table-column>
  139. <el-table-column prop="Saturday"
  140. label="周六"
  141. align="center"
  142. min-width="120px"
  143. show-overflow-tooltip
  144. :formatter="formatPerson"></el-table-column>
  145. <el-table-column prop="Sunday"
  146. label="周日"
  147. align="center"
  148. min-width="120px"
  149. show-overflow-tooltip
  150. :formatter="formatPerson"></el-table-column>
  151. </el-table>
  152. </d2-container>
  153. </template>
  154. <script>
  155. // 总列数
  156. const columnNum = 9
  157. // 固定列数
  158. const fixRowHeadNum = 2
  159. // todo 欠优化 列属性对应
  160. const columnProperty = [
  161. 'Local',
  162. 'Time',
  163. 'Monday',
  164. 'Tuesday',
  165. 'Wednesday',
  166. 'Thursday',
  167. 'Friday',
  168. 'Saturday',
  169. 'Sunday'
  170. ]
  171. import DutyApi from '@/api/duty'
  172. import itemDetailApi from '@/api/sysadmin/itemdetail'
  173. export default {
  174. name: 'dutyEdit',
  175. data () {
  176. return {
  177. LocalList: [],
  178. PeopleList: [],
  179. TimeList: [],
  180. termList: [],
  181. termName: '',
  182. selectLocal: [],
  183. selectTime: [],
  184. dutyDetail: {
  185. Id: -1,
  186. Year: -1,
  187. Term: -1,
  188. },
  189. year: '',
  190. years: [],
  191. dialogvisible: false,
  192. // 刷新标志 刷新表格样式
  193. refresh: 123,
  194. // 已选列表
  195. selectcell: new Map(),
  196. // 数据列表
  197. list: []
  198. }
  199. },
  200. created () {
  201. this.init()
  202. },
  203. mounted () {
  204. this.getLocal()
  205. this.getPeople()
  206. this.getDutyTime()
  207. let _this = this
  208. _this.dutyDetail = _this.$route.query.duty
  209. _this.termList = _this.$route.query.term
  210. this.getTerm()
  211. this.getDetailData()
  212. },
  213. methods: {
  214. // 获取值班子表表格
  215. getDetailData () {
  216. let _this = this
  217. DutyApi.getDetailByDutyId({
  218. DutyId: _this.dutyDetail.Id
  219. }).then(res => {
  220. if (res.length > 0) {
  221. _this.list = res
  222. }
  223. })
  224. },
  225. // 新增表格
  226. addList () {
  227. if (this.selectLocal == 0 && this.selectTime.length == 0) {
  228. return
  229. }
  230. this.list = []
  231. this.selectcell = new Map()
  232. let _this = this
  233. this.selectLocal.forEach(function (value, key) {
  234. _this.selectTime.forEach(function (valuee, keyy) {
  235. _this.list.push({
  236. DutyId: _this.dutyDetail.Id,
  237. Local: value,
  238. Time: valuee,
  239. Monday: 0,
  240. Tuesday: 0,
  241. Wednesday: 0,
  242. Thursday: 0,
  243. Friday: 0,
  244. Saturday: 0,
  245. Sunday: 0
  246. })
  247. })
  248. })
  249. },
  250. // 保存值班子表信息
  251. addDuytDetail () {
  252. let _this = this
  253. if (this.dutyDetail.Id) {
  254. DutyApi.Saves({ DataList: this.list, DutyId: this.dutyDetail.Id })
  255. .then(res => {
  256. })
  257. .catch(err => {
  258. // handle error
  259. console.error(err)
  260. })
  261. } else {
  262. console.log('error submit!!')
  263. return false
  264. }
  265. },
  266. // 删除一行
  267. deleteRow () {
  268. let val = this.selectcell
  269. if (val) {
  270. val.forEach((val, index) => {
  271. this.list.forEach((v, i) => {
  272. if (val.index === v.index) {
  273. this.list.splice(i, 1)
  274. }
  275. })
  276. })
  277. }
  278. },
  279. // 获取字典表地点
  280. getLocal () {
  281. let _this = this
  282. itemDetailApi.getItemDetailByItemCode({ ItemCode: 'Local' })
  283. .then(res => {
  284. _this.LocalList = res
  285. })
  286. .catch(err => {
  287. console.error(err)
  288. })
  289. },
  290. // 获取字典表值班人员
  291. getPeople () {
  292. let _this = this
  293. itemDetailApi.getItemDetailByItemCode({ ItemCode: 'People' })
  294. .then(res => {
  295. _this.PeopleList = res
  296. })
  297. .catch(err => {
  298. console.error(err)
  299. })
  300. },
  301. // 获取字典表课程时间段
  302. getDutyTime () {
  303. let _this = this
  304. itemDetailApi.getItemDetailByItemCode({ ItemCode: 'DutyTime' })
  305. .then(res => {
  306. _this.TimeList = res
  307. })
  308. .catch(err => {
  309. console.error(err)
  310. })
  311. },
  312. // 获取本年年份
  313. init () {
  314. var myDate = new Date()
  315. var year = myDate.getFullYear()// 获取当前年
  316. this.initSelectYear(year)
  317. },
  318. // 循环遍历年份
  319. initSelectYear (year) {
  320. this.years = []
  321. for (let i = 0; i < 5; i++) {
  322. this.years.push({ value: (year - i), label: (year - i) + '年' })
  323. }
  324. },
  325. // 单元格、行选中
  326. cellclick (row, column, cell, event) {
  327. // 第3列开始可以选中
  328. // 单击单元格选中
  329. if (cell.cellIndex >= fixRowHeadNum) {
  330. if (this.selectcell.get(row.Local + '_' + row.Time + '_' + column.property)) {
  331. this.selectcell.set(row.Local + '_' + row.Time + '_' + column.property, false)
  332. } else {
  333. this.selectcell.set(row.Local + '_' + row.Time + '_' + column.property, this.list[row.index])
  334. }
  335. }
  336. // 第三列之前选中整行
  337. // 行选中
  338. var currentcolumnindex = column.index
  339. if (cell.cellIndex < fixRowHeadNum) {
  340. for (var i = cell.cellIndex; i < columnNum - 1; i++) {
  341. currentcolumnindex = currentcolumnindex + 1
  342. var nextSibling = cell.nextSibling
  343. cell = nextSibling
  344. if (i >= fixRowHeadNum - 1) {
  345. if (this.selectcell.get(row.Local + '_' + row.Time + '_' + columnProperty[currentcolumnindex])) {
  346. this.selectcell.set(row.Local + '_' + row.Time + '_' + columnProperty[currentcolumnindex], false)
  347. } else {
  348. this.selectcell.set(row.Local + '_' + row.Time + '_' + columnProperty[currentcolumnindex], this.list[row.index])
  349. }
  350. }
  351. }
  352. }
  353. this.refresh = Math.random()
  354. },
  355. // 列选中
  356. headclick (column, event) {
  357. // 判断是否为可选列
  358. if (column.index > fixRowHeadNum - 1) {
  359. for (var i = 0; i < this.list.length; i++) {
  360. if (this.selectcell.get(this.list[i].Local + '_' + this.list[i].Time + '_' + column.property)) {
  361. this.selectcell.set(this.list[i].Local + '_' + this.list[i].Time + '_' + column.property, false)
  362. } else {
  363. this.selectcell.set(this.list[i].Local + '_' + this.list[i].Time + '_' + column.property, this.list[i])
  364. }
  365. }
  366. }
  367. this.refresh = Math.random()
  368. },
  369. // 选择值班人员
  370. personClick (person) {
  371. this.selectcell.forEach(function (value, key) {
  372. if (value) {
  373. var property = key.split('_')
  374. value[property[2]] = person
  375. }
  376. })
  377. this.selectcell = new Map()
  378. },
  379. // 更新背景
  380. cellBg ({ row, column, rowIndex, columnIndex }) {
  381. row.index = rowIndex
  382. column.index = columnIndex
  383. let _this = this
  384. // 注意这里是解构
  385. // 利用单元格的 className 的回调方法,给行列索引赋值
  386. if (_this.selectcell.get(row.Local + '_' + row.Time + '_' + column.property)) {
  387. return 'selectedCell'
  388. }
  389. },
  390. // 匹配学期
  391. formatTerm (row, column) {
  392. for (var i = 0; i < this.termList.length; i++) {
  393. if (parseInt(this.termList[i].ItemValue) === parseInt(row.Term)) {
  394. return this.termList[i].ItemName
  395. }
  396. }
  397. },
  398. // 获取学期名称
  399. getTerm () {
  400. let termName = ''
  401. let _this = this
  402. _this.termList.forEach(function (value, key) {
  403. if (_this.dutyDetail.Term == value.ItemValue) {
  404. _this.termName = value.ItemName
  405. }
  406. })
  407. },
  408. formatPerson (row, column, cellValue, index) {
  409. let label = '休息'
  410. for (var i = 0; i < this.PeopleList.length; i++) {
  411. if (this.PeopleList[i].ItemValue == cellValue) {
  412. return this.PeopleList[i].ItemName
  413. }
  414. }
  415. return label
  416. }, formatLocal (row, column, cellValue, index) {
  417. for (var i = 0; i < this.LocalList.length; i++) {
  418. if (this.LocalList[i].ItemValue == cellValue) {
  419. return this.LocalList[i].ItemName
  420. }
  421. }
  422. }, formatTime (row, column, cellValue, index) {
  423. for (var i = 0; i < this.TimeList.length; i++) {
  424. if (this.TimeList[i].ItemValue == cellValue) {
  425. return this.TimeList[i].ItemName
  426. }
  427. }
  428. },
  429. // 关闭当前页
  430. closeWindow () {
  431. window.location.href = "./#/duty";
  432. }
  433. }
  434. }
  435. </script>
  436. <style>
  437. .selectedCell {
  438. background-color: green !important;
  439. color: white;
  440. }
  441. </style>