class.vue 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. <template>
  2. <div class="pb_content">
  3. <div class="tou">
  4. <span>班级管理</span>
  5. </div>
  6. <div class="pb_content_body">
  7. <div class="student_head">
  8. <div class="student_search">
  9. <span>
  10. <el-input
  11. placeholder="请输入班级名称"
  12. v-model="sClassName"
  13. clearable
  14. >
  15. </el-input>
  16. </span>
  17. <el-button type="primary" @click="searchClass">查询</el-button>
  18. </div>
  19. <div class="student_button">
  20. <el-button type="primary" @click="dialogVisible = true"
  21. >添加班级</el-button
  22. >
  23. </div>
  24. </div>
  25. <div class="student_table">
  26. <el-table
  27. ref="table"
  28. :data="tableData"
  29. border
  30. :height="tableHeight"
  31. :fit="true"
  32. v-loading="isLoading"
  33. style="width: 100%; height: 60%"
  34. :header-cell-style="{ background: '#f1f1f1' }"
  35. :row-class-name="tableRowClassName"
  36. >
  37. <el-table-column
  38. prop="school"
  39. label="学校名称"
  40. min-width="40%"
  41. align="center"
  42. >
  43. </el-table-column>
  44. <el-table-column
  45. prop="name"
  46. label="班级名称"
  47. min-width="40%"
  48. align="center"
  49. >
  50. </el-table-column>
  51. <el-table-column label="操作" min-width="20%">
  52. <template slot-scope="scope">
  53. <el-button
  54. @click="deleteClass(scope.row)"
  55. type="text"
  56. size="small"
  57. >删除</el-button
  58. >
  59. </template>
  60. </el-table-column>
  61. </el-table>
  62. </div>
  63. <div class="student_page">
  64. <el-pagination
  65. background
  66. layout="prev, pager, next"
  67. :page-size="10"
  68. :total="total"
  69. v-if="!isListAjax && page"
  70. @current-change="handleCurrentChange"
  71. >
  72. </el-pagination>
  73. </div>
  74. </div>
  75. <el-dialog
  76. title="添加班级"
  77. :visible.sync="dialogVisible"
  78. :append-to-body="true"
  79. width="25%"
  80. :before-close="handleClose"
  81. class="dialog_diy"
  82. >
  83. <el-form>
  84. <el-form-item label="所属学校" :label-width="formLabelWidth">
  85. <el-select v-model="oid" disabled>
  86. <el-option
  87. v-for="item in schoolArray"
  88. :key="item.id"
  89. :label="item.name"
  90. :value="item.id"
  91. ></el-option>
  92. </el-select>
  93. </el-form-item>
  94. <el-form-item label="班级名称" :label-width="formLabelWidth">
  95. <el-input
  96. v-model="className"
  97. auto-complete="off"
  98. placeholder="请输入班级..."
  99. ></el-input>
  100. </el-form-item>
  101. </el-form>
  102. <span slot="footer" class="dialog-footer">
  103. <el-button @click="dialogVisible = false">取 消</el-button>
  104. <el-button type="primary" @click="insertClass">确 定</el-button>
  105. </span>
  106. </el-dialog>
  107. </div>
  108. </template>
  109. <script>
  110. import $ from "jquery";
  111. export default {
  112. data() {
  113. return {
  114. isLoading: false,
  115. tableHeight: 50,
  116. sClassName: "",
  117. className: "",
  118. formLabelWidth: "80px",
  119. dialogVisible: false,
  120. tableData: [],
  121. total: null,
  122. page: null,
  123. oid: "",
  124. isListAjax: false,
  125. schoolArray:[]
  126. };
  127. },
  128. mounted() {
  129. this.$nextTick(function () {
  130. this.tableHeight =
  131. window.innerHeight - this.$refs.table.$el.offsetTop - 200;
  132. if (this.tableHeight <= 530) {
  133. this.tableHeight = 530;
  134. }
  135. // 监听窗口大小变化
  136. let self = this;
  137. window.onresize = function () {
  138. self.tableHeight =
  139. window.innerHeight - self.$refs.table.$el.offsetTop - 200;
  140. if (self.tableHeight <= 530) {
  141. self.tableHeight = 530;
  142. }
  143. };
  144. });
  145. //this.$refs.table.$el.offsetTop:表格距离浏览器的高度 //200表示你想要调整的表格距离底部的高度(你可以自己随意调整),因为我们一般都有放分页组件的,所以需要给它留一个高度
  146. },
  147. created() {
  148. this.page = 1;
  149. this.getClass();
  150. this.getOrg();
  151. this.oid = this.$store.state.userInfo.organizeid;
  152. },
  153. methods: {
  154. tableRowClassName({ row, rowIndex }) {
  155. if ((rowIndex + 1) % 2 === 0) {
  156. return "even_row";
  157. } else {
  158. return "";
  159. }
  160. },
  161. handleCurrentChange(val) {
  162. // console.log(`当前页: ${val}`);
  163. this.page = val;
  164. this.getClass();
  165. },
  166. handleClose(done) {
  167. done();
  168. // this.$confirm("确认关闭?")
  169. // .then((_) => {
  170. // done();
  171. // })
  172. // .catch((_) => {});
  173. },
  174. //查询学校
  175. getOrg() {
  176. this.ajax
  177. .get(this.$store.state.api + "getAllOrg", "")
  178. .then((res) => {
  179. this.schoolArray = res.data[0];
  180. console.log(res.data[0]);
  181. })
  182. .catch((err) => {
  183. console.error(err);
  184. });
  185. },
  186. //搜索班级
  187. searchClass() {
  188. this.page = 1;
  189. this.isListAjax = true;
  190. this.getClass();
  191. },
  192. //获取班级
  193. getClass() {
  194. this.isLoading = true;
  195. let params = {
  196. username: this.$store.state.userInfo.organizeid,
  197. cn: this.sClassName,
  198. page: this.page,
  199. };
  200. this.ajax
  201. .get(this.$store.state.api + "getClass", params)
  202. .then((res) => {
  203. this.isListAjax = false;
  204. this.isLoading = false;
  205. this.total = res.data[0].length > 0 ? res.data[0][0].num : 0;
  206. this.tableData = res.data[0];
  207. })
  208. .catch((err) => {
  209. this.isLoading = false;
  210. console.error(err);
  211. });
  212. },
  213. time() {
  214. if (!this.now) {
  215. this.now = new Date().getTime();
  216. return true;
  217. } else {
  218. let time = new Date().getTime();
  219. if (time - this.now > 3000) {
  220. this.now = time;
  221. return true;
  222. } else {
  223. return false;
  224. }
  225. }
  226. },
  227. //添加班级
  228. insertClass() {
  229. let _this = this
  230. console.log(this.$store.state.userInfo);
  231. if (this.oid === "") {
  232. this.$message.error("请选择学校");
  233. return;
  234. }
  235. if (this.className === "") {
  236. this.$message.error("班级名字不能为空");
  237. return;
  238. }
  239. let params = [{ oid: this.oid, n: this.className, cuid: this.$store.state.userInfo.userid }];
  240. let params2 = { pid: this.oid, n: this.className };
  241. let a = 1
  242. $.ajax({
  243. url: this.$store.state.api + "getClassRepeat", //url路径
  244. type: "GET", //GET
  245. async: false, //或false,是否异步
  246. data: params2,
  247. timeout: 5000, //超时时间
  248. dataType: "json", //返回的数据格式:
  249. beforeSend: function (xhr) {},
  250. success: function (res, textStatus, jqXHR) {
  251. if (res[0][0].num > 0) {
  252. _this.$message.error(
  253. "此学校已存在此班级!"
  254. );
  255. a = 2;
  256. }
  257. },
  258. error: function (xhr, textStatus) {
  259. console.log(textStatus);
  260. },
  261. complete: function () {},
  262. });
  263. if (this.time() && a == 1) {
  264. this.ajax
  265. .post(this.$store.state.api + "insertClass", params)
  266. .then((res) => {
  267. this.$message({
  268. message: "添加成功",
  269. type: "success",
  270. });
  271. this.sClassName = "";
  272. this.className = "";
  273. this.page = 1;
  274. this.getClass();
  275. this.dialogVisible = false;
  276. })
  277. .catch((err) => {
  278. this.$message.error("添加失败");
  279. console.error(err);
  280. });
  281. }
  282. },
  283. //删除班级
  284. deleteClass(row) {
  285. let params = [{ id: row.id }];
  286. this.$confirm("确定删除此班级吗?", "提示", {
  287. confirmButtonText: "确定",
  288. cancelButtonText: "取消",
  289. type: "warning",
  290. })
  291. .then(() => {
  292. this.ajax
  293. .post(this.$store.state.api + "deleteClass", params)
  294. .then((res) => {
  295. this.$message({
  296. message: "删除成功",
  297. type: "success",
  298. });
  299. this.className = "";
  300. if (this.page != 1 && this.tableData.length == 1) {
  301. this.page--;
  302. }
  303. this.getClass();
  304. this.dialogVisible = false;
  305. })
  306. .catch((err) => {
  307. this.$message.error("删除失败");
  308. console.error(err);
  309. });
  310. })
  311. .catch(() => {});
  312. },
  313. },
  314. };
  315. </script>
  316. <style scoped>
  317. .dialog_diy >>> .el-dialog__header {
  318. background: #3d67bc !important;
  319. padding: 15px 20px;
  320. }
  321. .dialog_diy >>> .el-dialog__title {
  322. color: #fff;
  323. }
  324. .dialog_diy >>> .el-dialog__headerbtn {
  325. top: 19px;
  326. }
  327. .dialog_diy >>> .el-dialog__headerbtn .el-dialog__close {
  328. color: #fff;
  329. }
  330. .dialog_diy >>> .el-dialog__headerbtn .el-dialog__close:hover {
  331. color: #fff;
  332. }
  333. .student_head >>> .el-button--primary {
  334. background-color: #2268bc;
  335. }
  336. .pb_content {
  337. box-sizing: border-box;
  338. padding: 20px;
  339. }
  340. .pb_content_body {
  341. margin-top: 10px;
  342. }
  343. .xls_button {
  344. font-size: 14px;
  345. cursor: pointer;
  346. text-decoration: underline;
  347. color: rgb(34, 104, 188);
  348. }
  349. .student_head {
  350. display: flex;
  351. justify-content: space-between;
  352. }
  353. .student_search {
  354. display: flex;
  355. width: 300px;
  356. }
  357. .student_search span {
  358. margin: 0 10px 0 0;
  359. }
  360. .student_button {
  361. display: flex;
  362. overflow: hidden;
  363. height: 40px;
  364. }
  365. .student_button .el-button--primary {
  366. /* margin-right: 10px; */
  367. }
  368. .upload-demo {
  369. display: flex;
  370. flex-direction: column;
  371. align-items: end;
  372. /* position: relative; */
  373. width: 100px;
  374. overflow: hidden;
  375. }
  376. .student_table {
  377. margin: 20px 0;
  378. }
  379. .el-table >>> .even_row {
  380. background-color: #f1f1f1;
  381. }
  382. .tou {
  383. border-bottom: 1px solid #c9c9c9;
  384. height: 50px;
  385. font-size: 30px;
  386. }
  387. </style>