Vueの電気の供給、バックエンド管理システムプロジェクト2日目 - ホーム&テーブルを追加し、動的描画データのページング

0x01の。Githubのは、ジェスチャーを使用することを学ぶために

昨日、今日の内容に基づいて、我々はいくつかの単一ファイルのアセンブリを追加する必要があり、ルーティングのファイルが記録に使用され、今日のフォーカスを対応する増加を行う必要がある要素-UIをしてスプレッドシートコンポーネントの動的レンダリングプロセスとデータの実現を達成するために、ページング機能を実装プロセスのを。

言っても無駄、プロジェクトはGithubににアップロードし、知りたいことができてきたGithubの詳細の実装プロセスのあらゆる段階コミットの過去のバージョンに基づいて詳細な検討のために提出履歴ビューのバージョンを。

最初のステップ:

 

 

ステップ2:

 

第三段階:

分割ボタンの右側をクリックして、あなたが行われていたものの修正版でこの機能を見ることができ、緑色の部分が変更/追加が、バージョンにクローン最も古いバージョンのクローンを作成するために注意を払うための時間です作るコンテンツです、あなたはそれはクールだったので、私は、ステップバイステップでは、アイデアと一緒に行くためにどのような機能を実現するためのコードを追加したものを知っているので。

 

[OK]を、すぐにメインイベントを始めました!

 

0x02.Element-UI申込書

1.まず、テーブルのテーブルを見つけ、アカウントにポイントを次の表の多くの異なる種類があり、これをコントロールしていない、我々は、目的の項目を見つける国境フォームで。

 

 

2.ビューソースコードは、ソース・コードは常に注意深く(リードスルーとテーブルの下に解釈されるべきで視聴するたびに属性ノートで説明属性テーブル)にソースコードをコピーuser.vue成分、及びそれを修正します。

 

3.ソースのコンテンツは、静的データ構造の形態を達成するために変更されます。

<テンプレート>
  <DIV CLASS = "ユーザー">
    <! - ユーザーのページ・テーブルを追加します - >
    <el-table :data="tableData" border style="width: 100%">
      <el-table-column prop="date" label="日期" width="180"></el-table-column>
      <el-table-column prop="name" label="姓名" width="180"></el-table-column>
      <el-table-column prop="address" label="地址"></el-table-column>
    </el-table>
  </div>
</template>

<script>
export default {
  // data是一个函数,返回一个对象
  data () {
    return {
      tableData: [
        {
          date: '2016-05-02',
          name: '王小虎',
          address: '上海市普陀区金沙江路 1518 弄'
        },
        {
          date: '2016-05-04',
          name: '王小虎',
          address: '上海市普陀区金沙江路 1517 弄'
        },
        {
          date: '2016-05-01',
          name: '王小虎',
          address: '上海市普陀区金沙江路 1519 弄'
        },
        {
          date: '2016-05-03',
          name: '王小虎',
          address: '上海市普陀区金沙江路 1516 弄'
        }
      ]
    }
  }
}
</script>

<style lang="less" scoped>
</style>
写死数据

 

4.添加右侧编辑,删除,分配角色按钮,增加索引

找到表格选项中的自定义列模版,从操作部分开始复制源码,然后增加合适的button按钮,在首列中加上<el-table-column type="index" width="50"></el-table-column>代码实现索引显示;

注意:在template中嵌套template这种写法可以叫插槽,官方叫自定义列模版,通过scope.row这个属性可以拿到当前行的数据,如果怀疑可以自己打印一下scope。

 

5.使用Tooltip 组件进行按钮的文字提示

复制源码,将源码里面的button按钮替换成你的编辑、删除、分配角色按钮;

    <el-tooltip class="item" effect="dark" content="Top Center 提示文字" placement="top">
      <el-button>上边</el-button>
    </el-tooltip>

 

6.利用switch开关组件实现用户状态切换

复制源码并新建一个插槽结构(自定义列模版),然后把代码粘贴进去,在data返回对象中添加value:true就完事了。

 

表格静态数据结构

<template>
  <div class="user">
    <!-- 添加user页面的表格 -->
    <el-table :data="tableData" border style="width: 100%">
      <!-- 使用index属性增加索引 -->
      <el-table-column type="index" width="50"></el-table-column>
      <!--  -->
      <el-table-column prop="date" label="日期" width="180"></el-table-column>
      <el-table-column prop="name" label="姓名" width="180"></el-table-column>
      <el-table-column prop="address" label="地址"></el-table-column>

      <el-table-column label="用户状态">
        <template slot-scope="scope">
          <el-switch v-model="value" active-color="#13ce66" inactive-color="#ff4949"></el-switch>
        </template>
      </el-table-column>

      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-tooltip class="item" effect="dark" content="编辑用户" placement="top">
            <el-button type="primary" icon="el-icon-edit" @click="handleEdit(scope.row)"></el-button>
          </el-tooltip>

          <el-tooltip class="item" effect="dark" content="删除用户" placement="top">
            <el-button type="danger" icon="el-icon-delete"></el-button>
          </el-tooltip>

          <el-tooltip class="item" effect="dark" content="分配角色" placement="top">
            <el-button type="success" icon="el-icon-share"></el-button>
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
export default {
  // data是一个函数,返回一个对象
  data () {
    return {
      value: true,
      tableData: [
        {
          date: '2016-05-02',
          name: '王小虎',
          address: '上海市普陀区金沙江路 1518 弄'
        },
        {
          date: '2016-05-04',
          name: '王小虎',
          address: '上海市普陀区金沙江路 1517 弄'
        },
        {
          date: '2016-05-01',
          name: '王小虎',
          address: '上海市普陀区金沙江路 1519 弄'
        },
        {
          date: '2016-05-03',
          name: '王小虎',
          address: '上海市普陀区金沙江路 1516 弄'
        }
      ]
    }
  },
  methods: {
    handleEdit (obj) {
      console.log(obj)
    }
  }
}
</script>

<style lang="less" scoped>
</style>
表格静态数据结构

 

7.调用接口方法请求数据实现表格动态数据渲染

查看对应的接口文档得知具体的请求路径、请求方式、传递的数据。

本文的内容只是演示,和github上的项目有差异,具体实现看github项目代码。

用户数据列表接口:

 

 实现代码:

<template>
  <div class="user">
    <!-- 添加user页面的表格 -->
    <el-table :data="tableData" border style="width: 100%">
      <!-- 使用index属性增加索引 -->
      <el-table-column type="index" width="50"></el-table-column>
      <!--  -->
      <el-table-column prop="username" label="用户名" width="180"></el-table-column>
      <el-table-column prop="email" label="邮箱" width="180"></el-table-column>
      <el-table-column prop="mobile" label="手机号"></el-table-column>

      <el-table-column label="用户状态">
        <template slot-scope="scope">
          <el-switch v-model="value" active-color="#13ce66" inactive-color="#ff4949"></el-switch>
        </template>
      </el-table-column>

      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-tooltip class="item" effect="dark" content="编辑用户" placement="top">
            <el-button type="primary" icon="el-icon-edit" @click="handleEdit(scope.row)"></el-button>
          </el-tooltip>

          <el-tooltip class="item" effect="dark" content="删除用户" placement="top">
            <el-button type="danger" icon="el-icon-delete"></el-button>
          </el-tooltip>

          <el-tooltip class="item" effect="dark" content="分配角色" placement="top">
            <el-button type="success" icon="el-icon-share"></el-button>
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import { getAllList } from '@/api/users.js'
export default {
  // data是一个函数,返回一个对象
  data () {
    return {
      query: '',
      pagenum: 1,
      pagesize: 7,
      value: true,
      tableData: []
    }
  },
  methods: {
    handleEdit (obj) {
      console.log(obj)
    }
  },
  mounted () {
    getAllList({
      query: this.query,
      pagenum: this.pagenum,
      pagesize: this.pagesize
    })
      .then(result => {
        // 成功后将获取到的数据进行覆盖
        console.log(result)
        this.tableData = result.data.data.users
      })
      .catch(err => {
        console.log(err)
      })
  }
}
</script>

<style lang="less" scoped>
</style>
最终代码

 

最终效果:

 

0x03.Element-UI分页功能应用

注意看文档中分页各个属性的意思!

代码: 

<template>
  <div class="user">
    <!-- 添加user页面的表格 -->
    <el-table :data="tableData" border style="width: 100%">
      <!-- 使用index属性增加索引 -->
      <el-table-column type="index" width="50"></el-table-column>
      <!--  -->
      <el-table-column prop="username" label="用户名" width="180"></el-table-column>
      <el-table-column prop="email" label="邮箱" width="180"></el-table-column>
      <el-table-column prop="mobile" label="手机号"></el-table-column>

      <el-table-column label="用户状态">
        <template slot-scope="scope">
          <el-switch v-model="value" active-color="#13ce66" inactive-color="#ff4949"></el-switch>
        </template>
      </el-table-column>

      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-tooltip class="item" effect="dark" content="编辑用户" placement="top">
            <el-button type="primary" icon="el-icon-edit" @click="handleEdit(scope.row)"></el-button>
          </el-tooltip>

          <el-tooltip class="item" effect="dark" content="删除用户" placement="top">
            <el-button type="danger" icon="el-icon-delete"></el-button>
          </el-tooltip>

          <el-tooltip class="item" effect="dark" content="分配角色" placement="top">
            <el-button type="success" icon="el-icon-share"></el-button>
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>

    <!-- 实现分页 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="pagenum"
      :page-sizes="[1, 2, 3, 4]"
      :page-size="pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total-0"
    ></el-pagination>
  </div>
</template>

<script>
import { getAllList } from '@/api/users.js'
export default {
  // data是一个函数,返回一个对象
  data () {
    return {
      total: '',
      query: '',
      // 当前页码
      pagenum: 1,
      // 每页显示记录数
      pagesize: 3,
      value: true,
      tableData: []
    }
  },
  methods: {
    handleEdit (obj) {
      console.log(obj)
    },
    handleSizeChange (val) {
      console.log(`每页 ${val} 条`)
      this.pagesize = val
      this.init()
    },
    handleCurrentChange (val) {
      console.log(`当前页: ${val}`)
      this.pagenum = val
      this.init()
    },
    init () {
      getAllList({
        query: this.query,
        pagenum: this.pagenum,
        pagesize: this.pagesize
      })
        .then(result => {
        // 成功后将获取到的数据进行覆盖
          console.log(result)
          this.tableData = result.data.data.users
          this.total = result.data.data.total
        })
        .catch(err => {
          console.log(err)
        })
    }
  },
  mounted () {
    this.init()
  }
}
</script>

<style lang="less" scoped>
</style>
分页功能

 

效果:

 

 

 

 

 

 

 

< template >
< div class= "user" >
<!-- 添加user页面的表格 -->
< el-table : data=" tableData" border style= "width: 100%" >
<!-- 使用index属性增加索引 -->
< el-table-column type= "index" width= "50" ></ el-table-column >
<!-- -->
< el-table-column prop= "username" label= "用户名" width= "180" ></ el-table-column >
< el-table-column prop= "email" label= "邮箱" width= "180" ></ el-table-column >
< el-table-column prop= "mobile" label= "手机号" ></ el-table-column >

< el-table-column label= "用户状态" >
< template slot-scope= "scope" >
< el-switch v-model=" value" active-color= "#13ce66" inactive-color= "#ff4949" ></ el-switch >
</ template >
</ el-table-column >

< el-table-column label= "操作" >
< template slot-scope= "scope" >
< el-tooltip class= "item" effect= "dark" content= "编辑用户" placement= "top" >
< el-button type= "primary" icon= "el-icon-edit" @ click=" handleEdit( scope. row)" ></ el-button >
</ el-tooltip >

< el-tooltip class= "item" effect= "dark" content= "删除用户" placement= "top" >
< el-button type= "danger" icon= "el-icon-delete" ></ el-button >
</ el-tooltip >

< el-tooltip class= "item" effect= "dark" content= "分配角色" placement= "top" >
< el-button type= "success" icon= "el-icon-share" ></ el-button >
</ el-tooltip >
</ template >
</ el-table-column >
</ el-table >

<!-- 实现分页 -->
< el-pagination
@ size-change=" handleSizeChange"
@ current-change=" handleCurrentChange"
: current-page=" pagenum"
: page-sizes="[ 1, 2, 3, 4]"
: page-size=" pagesize"
layout= "total, sizes, prev, pager, next, jumper"
: total=" total- 0"
></ el-pagination >
</ div >
</ template >

< script >
import { getAllList } from '@/api/users.js'
export default {
// data是一个函数,返回一个对象
data () {
return {
total: '',
query: '',
// 当前页码
pagenum: 1,
// 每页显示记录数
pagesize: 3,
value: true,
tableData: []
}
},
methods: {
handleEdit ( obj) {
console. log( obj)
},
handleSizeChange ( val) {
console. log( `每页 ${ val } 条`)
this. pagesize = val
this. init()
},
handleCurrentChange ( val) {
console. log( `当前页: ${ val } `)
this. pagenum = val
this. init()
},
init () {
getAllList({
query: this. query,
pagenum: this. pagenum,
pagesize: this. pagesize
})
. then( result => {
// 成功后将获取到的数据进行覆盖
console. log( result)
this. tableData = result. data. data. users
this. total = result. data. data. total
})
. catch( err => {
console. log( err)
})
}
},
mounted () {
this. init()
}
}
</ script >

< style lang= "less" scoped >
</ style >

おすすめ

転載: www.cnblogs.com/replaceroot/p/11086746.html