欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

VUE+Element實(shí)現(xiàn)增刪改查的示例源碼

 更新時間:2020年11月23日 09:14:43   作者:古有風(fēng)情·月下談之  
這篇文章主要介紹了VUE+Element實(shí)現(xiàn)增刪改查的示例源碼,幫助大家更好的理解和使用vue,感興趣的朋友可以了解下

前言

&最近因?yàn)橐恍┰?,沒有更博客,昨天老師布置了一個作業(yè),用vue實(shí)現(xiàn)增刪改查功能,想想這也不難,就做一下試試吧。
因?yàn)樽约簩懙臉邮經(jīng)]有別人做的好,因此我想用現(xiàn)成的UI框架,一直也沒用過Element,就干脆趁機(jī)學(xué)一下吧。

實(shí)驗(yàn)步驟

首先引入一下element的css以及js

<!-- 引入樣式 -->
<link rel="stylesheet"  rel="external nofollow" rel="external nofollow" >
<!-- 引入組件庫 -->
<script src="https://unpkg.com/element-ui/lib/index.js"></script>

然后引入需要用到的vue相關(guān)的js文件

<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>

下面說一下HTML結(jié)構(gòu)

<div id="app">
  <h1>職位的增刪改查</h1>
  <div class="head">
    <el-row :gutter="20">
      <el-col :span="6">
        <el-input v-model="userInfo.name" placeholder="請輸入你的公司名"></el-input>
      </el-col>
      <el-col :span="6">
        <el-input v-model="userInfo.position" placeholder="請輸入你的職位"></el-input>
      </el-col>
      <el-col :span="6">
        <el-input v-model="userInfo.major" placeholder="請輸入你的專業(yè)"></el-input>
      </el-col>
      <el-col :span="6">
        <el-input v-model="userInfo.number" placeholder="請輸入數(shù)量"></el-input>
      </el-col>
    </el-row>
    <el-button type="primary" @click="addUser" class="add-btn" plain>添加信息</el-button>
  </div>
  <!-- 主體內(nèi)容 -->
  <div class="body">
    <template>
      <el-table :data="tableData" style="width: 100%">
        <el-table-column label="序號" width="180"><template slot-scope="scope"> {{scope.$index + 1 }} </template></el-table-column>
        <el-table-column prop="name" label="公司名" width="180"></el-table-column>
        <el-table-column prop="position" label="職位"></el-table-column>
        <el-table-column prop="major" label="專業(yè)"></el-table-column>
        <el-table-column prop="number" label="數(shù)量"></el-table-column>
        <el-table-column prop="birthday" label="操作">
          <template slot-scope="scope">
            <el-button type="primary" icon="el-icon-edit" @click="editUser(scope.row,scope.$index)" circle></el-button>
            <el-button type="danger" icon="el-icon-delete" @click="delUser(scope.$index)" circle></el-button>
          </template>
        </el-table-column>
      </el-table>
    </template>
  </div>
  <!-- 編輯框 -->
  <el-dialog title="編輯用戶信息" :visible.sync="dialogVisible" width="30%" :before-close="handleClose">
    <div>
      <el-form ref="form" :model="editObj" label-width="80px">
        <el-form-item label="公司名"><el-input v-model="editObj.name"></el-input></el-form-item>
        <el-form-item label="職位"><el-input v-model="editObj.position"></el-input></el-form-item>
        <el-form-item label="專業(yè)"><el-input v-model="editObj.major"></el-input></el-form-item>
        <el-form-item label="數(shù)量"><el-input v-model="editObj.number"></el-input></el-form-item>
      </el-form>
    </div>
    <span slot="footer" class="dialog-footer">
      <el-button @click="dialogVisible = false">取 消</el-button>
      <el-button type="primary" @click="confirm">確 定</el-button>
    </span>
  </el-dialog>
</div>

這一段是element的表單以及編輯等樣式 ,其中添加了一些click操作 后面需要用到

加上基礎(chǔ)的樣式

 <style>
    #app{
      width:1024px;
      margin: 0 auto; 
    }
    .add-btn{
      margin-top: 20px;
      width: 100%;
    }
    .body{
      margin-top:20px;
    }
  </style>

現(xiàn)在頁面的基本樣式就做好了,如下圖所示:

下面開始寫vue代碼,對各個功能進(jìn)行處理操作
了解過vuejs的應(yīng)該知道這樣的結(jié)構(gòu) data里面填寫我們獲取的數(shù)據(jù) 一些規(guī)則,定義一些變量 ,在methods進(jìn)行我們的操作。

new Vue({
  el: '#app',
    data:{},
    methods:{}
})
data: function(){
        return{
          userInfo:{ 
            name:'',
            position: '',
            major: '',
            number: '',
          },
          tableData: [{
            name:'互聯(lián)網(wǎng)+學(xué)院',
            position: '專職教師',
            major: '對外貿(mào)易',
            number: '2',
          },{
            name:'徐州重工',
            position: '工廠車研發(fā)部工程師',
            major: '精密機(jī)械制造',
            number: '12',
          },{
            name:'北京青碼科技',
            position: '前端開發(fā)工程師',
            major: 'Vue、React',
            number: '4',
          }
          ],
          dialogVisible: false, 
          editObj:{
            name:'',
            position: '',
            major: '',
            number: '',
          },
          userIndex:0,
        }
      },

接下來我們添加methods

  •     addUser() 是添加數(shù)據(jù)
  •     delUser()是刪除數(shù)據(jù)
  •     editUser()是編輯數(shù)據(jù)
  •     handleClose()是是否彈出編輯框
  •     confirm()是確認(rèn)信息并且傳數(shù)據(jù)到表格中

在增加模塊中,我做了信息判斷,如果是信息是空就會彈出提示框,顯示信息不能為空,
在刪除模塊中,點(diǎn)擊可以刪除一行信息
在修改模塊中,會先將原本的信息拿到,然后再修改你需要修改的信息。

 methods:{
       //添加
        addUser(){
          if(!this.userInfo.name){
            this.$message({
              message: '請輸入你的公司名!',
              
            });
            return;
          }
          if(!this.userInfo.position){
            this.$message({
              message: '請輸入你的職位!',
              type: 'warning'
            });
            return;
          }
          if (!this.userInfo.major) {
            this.$message({
              message: '請輸入你的專業(yè)!',
              type: 'warning'
            });
            return;
          }
          if (!this.userInfo.number) {
            this.$message({
              message: '請輸入數(shù)量!',
              type: 'warning'
            });
            return;
          }
          this.tableData.push(this.userInfo);
          this.userInfo = { 
            name:'',
            position: '',
            major: '',
            number: '',
          };
        },

        //刪除
        delUser(idx){
          this.$confirm('確認(rèn)刪除此用戶信息?')
            .then(_ => {
              this.tableData.splice(idx, 1);
            })
            .catch(_ => {});
        },
        //編輯
        editUser(item,idx){
          this.userIndex = idx;
          this.editObj = {
            name: item.name,
            position: item.position,
            major: item.major,
            number: item.number,
          };
          this.dialogVisible = true;
        },

        handleClose(){
          this.dialogVisible = false;
        },

        confirm(){
          this.dialogVisible = false;
          Vue.set(this.tableData, this.userIndex, this.editObj);
            }
          },
        })

總結(jié):

    通過這次練習(xí),讓我知道了Element框架是怎么使用的,Element框架寫代碼做樣式的確方便,以后有什么要求低的作業(yè)可以拿來使用,目前的我畢竟還是一個學(xué)生,我還是需要多鍛煉寫代碼,手寫樣式的能力。

    最后: 附整個項(xiàng)目的源代碼,本項(xiàng)目僅供學(xué)習(xí)交流。

源代碼

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <link rel="stylesheet"  rel="external nofollow" rel="external nofollow" >
  <title>Vue增刪改查</title>
  <style>
    #app{
      width:1024px;
      margin: 0 auto; 
    }
    .add-btn{
      margin-top: 20px;
      width: 100%;
    }
    .body{
      margin-top:20px;
    }
  </style>
  
</head>
<body>
  <div id="app">
    <h1>職位的增刪改查</h1>
    <div class="head">
      <el-row :gutter="20">
        <el-col :span="6">
          <el-input v-model="userInfo.name" placeholder="請輸入你的公司名"></el-input>
        </el-col>
        <el-col :span="6">
          <el-input v-model="userInfo.position" placeholder="請輸入你的職位"></el-input>
        </el-col>
        <el-col :span="6">
          <el-input v-model="userInfo.major" placeholder="請輸入你的專業(yè)"></el-input>
        </el-col>
        <el-col :span="6">
          <el-input v-model="userInfo.number" placeholder="請輸入數(shù)量"></el-input>
        </el-col>
      </el-row>
      <el-button type="primary" @click="addUser" class="add-btn" plain>添加信息</el-button>
    </div>
    <!-- 主體內(nèi)容 -->
    <div class="body">
      <template>
        <el-table :data="tableData" style="width: 100%">
          <el-table-column label="序號" width="180"><template slot-scope="scope"> {{scope.$index + 1 }} </template></el-table-column>
          <el-table-column prop="name" label="公司名" width="180"></el-table-column>
          <el-table-column prop="position" label="職位"></el-table-column>
          <el-table-column prop="major" label="專業(yè)"></el-table-column>
          <el-table-column prop="number" label="數(shù)量"></el-table-column>
          <el-table-column prop="birthday" label="操作">
            <template slot-scope="scope">
              <el-button type="primary" icon="el-icon-edit" @click="editUser(scope.row,scope.$index)" circle></el-button>
              <el-button type="danger" icon="el-icon-delete" @click="delUser(scope.$index)" circle></el-button>
            </template>
          </el-table-column>
        </el-table>
      </template>
    </div>
    <!-- 編輯框 -->
    <el-dialog title="編輯用戶信息" :visible.sync="dialogVisible" width="30%" :before-close="handleClose">
      <div>
        <el-form ref="form" :model="editObj" label-width="80px">
          <el-form-item label="公司名"><el-input v-model="editObj.name"></el-input></el-form-item>
          <el-form-item label="職位"><el-input v-model="editObj.position"></el-input></el-form-item>
          <el-form-item label="專業(yè)"><el-input v-model="editObj.major"></el-input></el-form-item>
          <el-form-item label="數(shù)量"><el-input v-model="editObj.number"></el-input></el-form-item>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="confirm">確 定</el-button>
      </span>
    </el-dialog>
  </div>
  <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
  <script src="https://unpkg.com/element-ui/lib/index.js"></script>

  <script>
    
    new Vue({
      el:'#app',
      data: function(){
        return{
          userInfo:{ 
            name:'',
            position: '',
            major: '',
            number: '',
          },
          tableData: [{
            name:'互聯(lián)網(wǎng)+學(xué)院',
            position: '專職教師',
            major: '對外貿(mào)易',
            number: '2',
          },{
            name:'徐州重工',
            position: '工廠車研發(fā)部工程師',
            major: '精密機(jī)械制造',
            number: '12',
          },{
            name:'北京青碼科技',
            position: '前端開發(fā)工程師',
            major: 'Vue、React',
            number: '4',
          }
          ],
          dialogVisible: false, 
          editObj:{
            name:'',
            position: '',
            major: '',
            number: '',
          },
          userIndex:0,
        }
      },
      methods:{
        //添加
        addUser(){
          if(!this.userInfo.name){
            this.$message({
              message: '請輸入你的公司名!',
              
            });
            return;
          }
          if(!this.userInfo.position){
            this.$message({
              message: '請輸入你的職位!',
              type: 'warning'
            });
            return;
          }
          if (!this.userInfo.major) {
            this.$message({
              message: '請輸入你的專業(yè)!',
              type: 'warning'
            });
            return;
          }
          if (!this.userInfo.number) {
            this.$message({
              message: '請輸入數(shù)量!',
              type: 'warning'
            });
            return;
          }
          this.tableData.push(this.userInfo);
          this.userInfo = { 
            name:'',
            position: '',
            major: '',
            number: '',
          };
        },

        //刪除
        delUser(idx){
          this.$confirm('確認(rèn)刪除此用戶信息?')
            .then(_ => {
              this.tableData.splice(idx, 1);
            })
            .catch(_ => {});
        },
        //編輯
        editUser(item,idx){
          this.userIndex = idx;
          this.editObj = {
            name: item.name,
            position: item.position,
            major: item.major,
            number: item.number,
          };
          this.dialogVisible = true;
        },

        handleClose(){
          this.dialogVisible = false;
        },

        confirm(){
          this.dialogVisible = false;
          Vue.set(this.tableData, this.userIndex, this.editObj);
            }
          },
        })
  </script>

</body>
</html>

以上就是VUE+Element實(shí)現(xiàn)增刪改查的示例源碼的詳細(xì)內(nèi)容,更多關(guān)于VUE+Element實(shí)現(xiàn)增刪改查的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評論