2020-12-24 實(shí)訓(xùn)五

目標(biāo):創(chuàng)建班級(jí)管理模塊(學(xué)生和班級(jí)摘盆、學(xué)院檩禾、學(xué)校關(guān)聯(lián)起來(lái))

一、后臺(tái)三步驟:

1埃碱、打開(kāi)projectName文件嚎尤,在models目錄下創(chuàng)建student.js文件荔仁,接著文件操作:

const mongoose = require('mongoose')
const Schema = mongoose.Schema
const feld={
    name: String,
    age: Number,
    student_number:Number,
    gender:String,
    school : { type: Schema.Types.ObjectId, ref: 'School' },
    academy : { type: Schema.Types.ObjectId, ref: 'Academy' },
    classs : { type: Schema.Types.ObjectId, ref: 'Classs' }

}
//自動(dòng)添加更新時(shí)間創(chuàng)建時(shí)間:
let personSchema = new mongoose.Schema(feld, {timestamps: {createdAt: 'created', updatedAt: 'updated'}})
module.exports= mongoose.model('Student',personSchema)

2、找到projectName下的routes目錄芽死,創(chuàng)建student.js文件:

const router = require('koa-router')()
let Model = require("../db/models/student");
router.prefix('/student')

router.get('/', function (ctx, next) {
    ctx.body = 'this is a users response!'
})

router.post('/add', async function (ctx, next) {
    console.log(ctx.request.body)
    let model = new Model(ctx.request.body);
    model = await model.save();
    console.log('user',model)
    ctx.body = model
})

router.post('/find', async function (ctx, next) {
    let models = await Model.
    find({}).populate('classs').populate('academy').populate('school')
    ctx.body = models
})

router.post('/get', async function (ctx, next) {
    // let users = await User.
    // find({})
    console.log(ctx.request.body)
    let model = await Model.find(ctx.request.body)
    console.log(model)
    ctx.body = model
})

router.post('/update', async function (ctx, next) {
    console.log(ctx.request.body)
    let pbj = await Model.update({ _id: ctx.request.body._id }, ctx.request.body);
    ctx.body = pbj
})
router.post('/delete', async function (ctx, next) {
    console.log(ctx.request.body)
    await Model.remove({ _id: ctx.request.body._id });
    ctx.body = 'shibai '
})
module.exports = router

3.在app.js中掛載路由:

const classs= require('./routes/student')
app.use(student.routes(), student.allowedMethods())

二乏梁、前臺(tái)三步驟:

打開(kāi)vue-admin-template-master文件,在src/views目錄下創(chuàng)建一個(gè)student模塊关贵,并在student目錄下創(chuàng)建vue文件遇骑。

1.editor.vue為編輯文件,用于創(chuàng)建班級(jí)記錄揖曾;

image
<template>
  <div class="dashboard-container">
    <el-form ref="form" :model="form" label-width="80px">
      <el-form-item label="所屬學(xué)校">
        <el-select v-model="form.school" placeholder="請(qǐng)選擇" @change="schoolChange">
          <el-option
            v-for="item in schools"
            :key="item._id"
            :label="item.name"
            :value="item._id">
          </el-option>
        </el-select>
      </el-form-item>
      <!--      編輯框:學(xué)院選擇列表-->
      <el-form-item label="所屬學(xué)院">
        <el-select v-model="form.academy" placeholder="請(qǐng)選擇">
          <el-option
            v-for="item in academys"
            :key="item._id"
            :label="item.name"
            :value="item._id">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="所屬班級(jí)">
        <el-select v-model="form.classs" placeholder="請(qǐng)選擇">
          <el-option
            v-for="item in classs"
            :key="item._id"
            :label="item.name"
            :value="item._id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="用戶名">
        <el-input v-model="form.name"></el-input>
      </el-form-item>
      <el-form-item label="年齡">
        <el-input v-model="form.age"></el-input>
      </el-form-item>
      <el-form-item label="學(xué)號(hào)">
        <el-input v-model="form.student_number"></el-input>
      </el-form-item>
      <el-form-item label="性別">
        <el-input v-model="form.gender"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">立即創(chuàng)建</el-button>
        <el-button>取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
  import { mapGetters } from 'vuex'

  export default {
    name: 'student',
    computed: {
      ...mapGetters([
        'name'
      ])
    },
    data(){
      return{
        schools:[],
        academys:[],
        classs:[],
        options: [
        ],
        apiModel:'student',
        form:{}
      }
    },
    methods:{
      onSubmit(){
        if(this.form._id){
          this.$http.post(`/api/${this.apiModel}/update`,this.form).then(res => {
            console.log('bar:', res)
            this.$router.push({path:this.apiModel})
            this.form={}
          })
        }else
        {
          this.$http.post('/api/'+this.apiModel+'/add',this.form).then(res => {
            console.log('bar:', res)
            this.$router.push({path:this.apiModel})
            this.form={}
          })
        }
      },
      schoolChange(val1){
        //顯示學(xué)院選擇欄目
        this.$http.post('/api/academy/get',{school:val1}).then(res => {
          if(res&&res.length>0){
            this.academys = res
            console.log('res:', res)
          }
        })
      }
    },

    mounted() {
      if(this.$route.query._id){
        this.$http.post('/api/'+this.apiModel+'/get',{_id:this.$route.query._id}).then(res => {
          if(res&&res.length>0){
            this.form = res[0]
            this.schoolChange(this.form.school)
          }
        })
      }

      //顯示學(xué)校選擇欄目
      this.$http.post('/api/school/find').then(res => {
        if(res&&res.length>0){
          this.schools = res
          console.log('res:', res)
        }
      })
      //顯示班級(jí)欄目
      this.$http.post('/api/classs/find').then(res => {
        if(res&&res.length>0){
          this.classs = res
          console.log('res:', res)
        }
      })
    }
  }
</script>

<style lang="scss" scoped>
  .dashboard {
    &-container {
      margin: 30px;
    }
    &-text {
      font-size: 30px;
      line-height: 46px;
    }
  }
</style>

2.index.vue為目錄文件落萎,用于顯示結(jié)果;

<template>
  <div class="dashboard-container">
    <el-table
      :data="users"
      style="width: 100%"
      :row-class-name="tableRowClassName">
      <el-table-column
        prop="name"
        label="名字"
        width="180">
      </el-table-column>
      <el-table-column
        prop="age"
        label="年齡"
        width="180">
      </el-table-column>
      <el-table-column
        prop="student_number"
        label="學(xué)號(hào)">
      </el-table-column>
      <el-table-column
        prop="gender"
        label="性別">
      </el-table-column>
      <!--      列表添加項(xiàng)目
-->
      <el-table-column
        prop="school"
        label="學(xué)校名稱"
        width="180">
        <template slot-scope="scope" >
          <span class="" v-if="scope.row.school">
            <el-tag
              :type="scope.row.school.name === '深圳信息職業(yè)技術(shù)學(xué)院' ? 'primary' : 'success'"
              disable-transitions>{{scope.row.school.name}}</el-tag>
          </span>
        </template>
      </el-table-column>
      <el-table-column
        prop="academy"
        label="學(xué)院名稱"
        width="180">
        <template slot-scope="scope" >
          <span class="" v-if="scope.row.academy">
            <el-tag
              :type="scope.row.academy.name === '軟件學(xué)院' ? 'primary' : 'success'"
              disable-transitions>{{scope.row.academy.name}}</el-tag>
          </span>

        </template>
      </el-table-column>

      <el-table-column
        prop="classs"
        label="班級(jí)名稱"
        width="180">
        <template slot-scope="scope" >
          <span class="" v-if="scope.row.classs">
            <el-tag
              :type="scope.row.classs.name === '18軟工4-3' ? 'primary' : 'success'"
              disable-transitions>{{scope.row.classs.name}}</el-tag>
          </span>

        </template>
      </el-table-column>

      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            size="mini"
            @click="handleEdit(scope.$index, scope.row)">編輯
          </el-button>
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.$index, scope.row)">刪除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
  import { mapGetters } from 'vuex'

  export default {
    name: 'student',
    computed: {
      ...mapGetters([
        'name'
      ])
    },
    data() {
      return {
        apiModel:'student',
        users: {}
      }
    },
    methods: {
      onSubmit() {
        console.log(123434)
      },
      handleEdit(index, item) {
        this.$router.push({ path: '/'+this.apiModel+'/editor', query: {_id:item._id} })
      },
      handleDelete(index, item) {
        this.$http.post('/api/'+this.apiModel+'/delete', item).then(res => {
          console.log('res:', res)
          this.findUser()
        })

      },
      findUser(){
        this.$http.post('/api/'+this.apiModel+'/find', this.user).then(res => {
          console.log('res:', res)
          this.users = res
        })
      }
    },
    mounted() {
      this.findUser()
    }
  }
</script>

<style lang="scss" scoped>
  .dashboard {
    &-container {
      margin: 30px;
    }

    &-text {
      font-size: 30px;
      line-height: 46px;
    }
  }
</style>

3.在index.js中添加路由:

 {
    path: '/student',
    component: Layout,
    meta: { title: '學(xué)生管理', icon: 'example' },
    redirect: '/student',
    children: [{
      path: 'student',
      name: 'student',
      component: () => import('@/views/student/index'),
      meta: { title: '學(xué)生管理', icon: 'user' }
    },
      {
        path: 'editor',
        name: 'editor',
        component: () => import('@/views/student/editor'),
        meta: { title: '添加學(xué)生', icon: 'user' }
      }]
  },
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末炭剪,一起剝皮案震驚了整個(gè)濱河市练链,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌奴拦,老刑警劉巖媒鼓,帶你破解...
    沈念sama閱讀 222,183評(píng)論 6 516
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異粱坤,居然都是意外死亡隶糕,警方通過(guò)查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,850評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門站玄,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)枚驻,“玉大人,你說(shuō)我怎么就攤上這事株旷≡俚牵” “怎么了?”我有些...
    開(kāi)封第一講書人閱讀 168,766評(píng)論 0 361
  • 文/不壞的土叔 我叫張陵晾剖,是天一觀的道長(zhǎng)锉矢。 經(jīng)常有香客問(wèn)我,道長(zhǎng)齿尽,這世上最難降的妖魔是什么沽损? 我笑而不...
    開(kāi)封第一講書人閱讀 59,854評(píng)論 1 299
  • 正文 為了忘掉前任,我火速辦了婚禮循头,結(jié)果婚禮上绵估,老公的妹妹穿的比我還像新娘炎疆。我一直安慰自己,他們只是感情好国裳,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,871評(píng)論 6 398
  • 文/花漫 我一把揭開(kāi)白布形入。 她就那樣靜靜地躺著,像睡著了一般缝左。 火紅的嫁衣襯著肌膚如雪亿遂。 梳的紋絲不亂的頭發(fā)上,一...
    開(kāi)封第一講書人閱讀 52,457評(píng)論 1 311
  • 那天渺杉,我揣著相機(jī)與錄音蛇数,去河邊找鬼。 笑死少办,一個(gè)胖子當(dāng)著我的面吹牛苞慢,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播英妓,決...
    沈念sama閱讀 40,999評(píng)論 3 422
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼绍赛!你這毒婦竟也來(lái)了蔓纠?” 一聲冷哼從身側(cè)響起,我...
    開(kāi)封第一講書人閱讀 39,914評(píng)論 0 277
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤吗蚌,失蹤者是張志新(化名)和其女友劉穎腿倚,沒(méi)想到半個(gè)月后,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體蚯妇,經(jīng)...
    沈念sama閱讀 46,465評(píng)論 1 319
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡敷燎,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,543評(píng)論 3 342
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了箩言。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片硬贯。...
    茶點(diǎn)故事閱讀 40,675評(píng)論 1 353
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖陨收,靈堂內(nèi)的尸體忽然破棺而出饭豹,到底是詐尸還是另有隱情,我是刑警寧澤务漩,帶...
    沈念sama閱讀 36,354評(píng)論 5 351
  • 正文 年R本政府宣布拄衰,位于F島的核電站,受9級(jí)特大地震影響饵骨,放射性物質(zhì)發(fā)生泄漏翘悉。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 42,029評(píng)論 3 335
  • 文/蒙蒙 一居触、第九天 我趴在偏房一處隱蔽的房頂上張望妖混。 院中可真熱鬧包吝,春花似錦、人聲如沸源葫。這莊子的主人今日做“春日...
    開(kāi)封第一講書人閱讀 32,514評(píng)論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)息堂。三九已至嚷狞,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間荣堰,已是汗流浹背床未。 一陣腳步聲響...
    開(kāi)封第一講書人閱讀 33,616評(píng)論 1 274
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留振坚,地道東北人薇搁。 一個(gè)月前我還...
    沈念sama閱讀 49,091評(píng)論 3 378
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像渡八,于是被迫代替她去往敵國(guó)和親啃洋。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,685評(píng)論 2 360

推薦閱讀更多精彩內(nèi)容

  • 實(shí)訓(xùn)五學(xué)生管理一屎鳍、后臺(tái)三步驟:1宏娄、打開(kāi)projectName文件,在models目錄下創(chuàng)建student.js文件...
    commoo閱讀 126評(píng)論 0 0
  • 一逮壁、后臺(tái)三步驟: 1孵坚、打開(kāi)projectName文件,在models目錄下創(chuàng)建classs.js文件窥淆,接著文件操作...
    明年的牛肉干閱讀 137評(píng)論 0 0
  • 目標(biāo):創(chuàng)建班級(jí)管理模塊(班級(jí)和學(xué)院卖宠、學(xué)校關(guān)聯(lián)起來(lái)) 一、后臺(tái)三步驟: 1忧饭、打開(kāi)projectName文件扛伍,在mod...
    oy11閱讀 290評(píng)論 0 0
  • 目標(biāo):創(chuàng)建學(xué)生管理模塊(學(xué)生和班級(jí)、學(xué)院眷昆、學(xué)校關(guān)聯(lián)起來(lái))一蜒秤、后臺(tái)三步驟:1、打開(kāi)projectName文件亚斋,在mo...
    赴約閱讀 138評(píng)論 0 0
  • 實(shí)訓(xùn)一(校園管理系統(tǒng)1-部署環(huán)境作媚,云端本地?cái)?shù)據(jù)庫(kù)相連) 1.安裝 nodejs 2.安裝 git 官網(wǎng)下載安裝即可...
    凜冬_70ff閱讀 258評(píng)論 0 0