Vue + ElementUI的電商管理系統實例15 訂單列表


1、創建order分支

創建分支:

git checkout -b order

推送到遠程:

git push -u origin order

2、通過路由渲染訂單列表頁面

新建order文件夾,Order.vue文件:

<template>
<div>
  <h3>訂單列表</h3>
</div>
</template>

<script>
export default {

}
</script>

<style lang="less" scoped>

</style>

添加路由:

import Order from '../components/order/Order.vue'

const routes = [
  { path: '/', redirect: '/login' }, // 重定向
  { path: '/login', component: Login },
  {
    path: '/home',
    component: Home,
    redirect: '/welcome', // 重定向
    children: [ // 子路由
      { path: '/welcome', component: Welcome },
      { path: '/users', component: Users }, // 用戶列表
      { path: '/rights', component: Rights }, // 權限列表
      { path: '/roles', component: Roles }, // 角色列表
      { path: '/categories', component: Cate }, // 商品分類
      { path: '/params', component: Params }, // 分類參數
      { path: '/goods', component: List }, // 商品列表
      { path: '/goods/add', component: Add }, // 添加商品
      { path: '/orders', component: Order } // 訂單列表
    ]
  }
]

基礎布局:

<template>
<div>
  <!--面包屑導航區域-->
  <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首頁</el-breadcrumb-item>
      <el-breadcrumb-item>訂單管理</el-breadcrumb-item>
      <el-breadcrumb-item>訂單列表</el-breadcrumb-item>
  </el-breadcrumb>
  <!--卡片視圖區域-->
  <el-card>
    <el-row>
      <el-col :span="8">
        <!--搜索區域-->
        <el-input placeholder="請輸入內容">
          <el-button slot="append" icon="el-icon-search"></el-button>
        </el-input>
      </el-col>
    </el-row>
  </el-card>
</div>
</template>

<script>
export default {

}
</script>

<style lang="less" scoped>

</style>

效果如圖:

3、根據分頁獲取訂單數據列表

調用api的訂單數據列表接口,請求路徑:orders,請求方法:get
請求參數:
query | 查詢參數 | 可以為空 |
pagenum | 當前頁碼 | 不能為空 |
pagesize | 每頁顯示條數 | 不能為空 |
user_id | 用戶 ID | 可以為空 |
pay_status | 支付狀態 | 可以為空 |
is_send | 是否發貨 | 可以為空 |
order_fapiao_title | ['個人','公司'] | 可以為空 |
order_fapiao_company | 公司名稱 | 可以為空 |
order_fapiao_content | 發票內容 | 可以為空 |
consignee_addr | 發貨地址 | 可以為空 |

添加代碼:

<script>
export default {
  data() {
    return {
      // 獲取訂單列表的參數對象
      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 10
      },
      orderList: [], // 訂單列表
      total: 0 // 訂單總數
    }
  },
  created() {
    this.getOrderList()
  },
  methods: {
    async getOrderList() {
      const { data: res } = await this.$http.get('orders', { params: this.queryInfo })
      if (res.meta.status !== 200) {
        return this.$message.error('獲取訂單列表失敗')
      }
      // console.log(res)
      this.orderList = res.data.goods
      this.total = res.data.total
    }
  }
}
</script>

4、渲染訂單table表格

添加表格代碼:

<!--訂單表格區域-->
<el-table :data="orderList" border stripe>
      <el-table-column type="index" label="#"></el-table-column>
      <el-table-column label="訂單編號" prop="order_number" width="300px"></el-table-column>
      <el-table-column label="訂單價格" prop="order_price"></el-table-column>
      <el-table-column label="是否付款" prop="pay_status">
        <template slot-scope="scope">
          <el-tag type="danger" v-if="scope.row.pay_status === '0'">未付款</el-tag>
          <el-tag type="success" v-else>已付款</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="是否發貨" prop="is_send"></el-table-column>
      <el-table-column label="下單時間" prop="create_time" width="140px">
        <template slot-scope="scope">
          <!--通過作用域插槽的形式 調用時間過濾器-->
          {{scope.row.create_time | dateFormat}}
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <!--修改按鈕-->
          <el-button type="primary" size="mini" icon="el-icon-edit"></el-button>
          <!--地址按鈕-->
          <el-button type="success" size="mini" icon="el-icon-location"></el-button>
        </template>
      </el-table-column>
</el-table>

此時效果圖:

5、實現分頁功能

添加分頁代碼:

<!--分頁區域-->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="queryInfo.pagenum"
      :page-sizes="[5, 10, 20]"
      :page-size="queryInfo.pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total" background
></el-pagination>

handleSizeChange 和 handleCurrentChange 函數:

// 監聽 pageSize 改變的事件
handleSizeChange(newSize) {
      this.queryInfo.pagesize = newSize
      this.getOrderList()
},
// 監聽 當前頁碼值 改變的事件
handleCurrentChange(newPage) {
      this.queryInfo.pagenum = newPage
      this.getOrderList()
}

效果圖:

6、實現省市區縣數據聯動效果

導入citydata.js文件

<script>
import cityData from './citydata.js'

export default {
  data() {
    return {
      cityData: cityData // 可以簡寫為cityData
    }
  }
}
</script>

給修改地址按鈕添加點擊事件:

<!--修改地址按鈕-->
<el-button type="primary" size="mini" icon="el-icon-edit"
              @click="showDialog"></el-button>

<script>
import cityData from './citydata.js'

export default {
  methods: {
    // 展示修改地址的對話框
    showDialog() {
      this.addressVisible = true
    }
  }
}
</script>

添加對話框代碼:

<!--修改地址對話框-->
<el-dialog title="修改地址" :visible.sync="addressVisible"
    width="50%" @close="addressDialogClosed">
    <el-form :model="addressForm" :rules="addressFormRules" ref="addressFormRef" label-width="100px">
      <el-form-item label="省市區/縣" prop="address1">
        <!--級聯選擇框 省市區縣-->
        <!-- options用來指定數據源  props用來指定配置對象  value/v-model選中項綁定值-->
        <el-cascader :options="cityData" v-model="addressForm.address1"></el-cascader>
      </el-form-item>
      <el-form-item label="詳細地址" prop="address2">
        <el-input v-model="addressForm.address2"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
        <el-button @click="addressVisible = false">取 消</el-button>
        <el-button type="primary" @click="addressVisible = false">確 定</el-button>
    </span>
</el-dialog>

<script>
import cityData from './citydata.js'

export default {
  methods: {
    // 修改地址對話框的關閉事件
    addressDialogClosed() {
      this.$refs.addressFormRef.resetFields() // 清空表單
      this.addressForm.address1 = []
    }
  }
}
</script>

7、展示物流進度對話框並獲取物流信息

給物流按鈕添加點擊事件:

<!--物流按鈕-->
<el-button type="success" size="mini" icon="el-icon-location" @click="showProgress"></el-button>

showProgress函數:

// 展示物流進度的對話框
showProgress() {
      this.progressVisible = true
}

添加物流進度的對話框:

<!--物流進度對話框-->
<el-dialog title="物流進度" :visible.sync="progressVisible" width="50%">
    <span>這是一段信息</span>
</el-dialog>

<script>
import cityData from './citydata.js'

export default {
  data() {
    return {
      progressVisible: false // 控制物流進度對話框是否顯示
    }
  }
}
</script>

然后要調用api的查看物流信息接口,請求路徑:/kuaidi/:id,請求方法:get

供測試的物流單號:1106975712662

添加代碼:

// 展示物流進度的對話框
async showProgress() {
      const { data: res } = await this.$http.get('/kuaidi/1106975712662')
      if (res.meta.status !== 200) {
        return this.$message.error('獲取物流信息失敗!')
      }
      console.log(res)
      this.progressInfo = res.data
      this.progressVisible = true
}

8、手動導入並使用Timeline組件

Timeline 時間線:可視化地呈現時間流信息。

Timeline 可拆分成多個按照時間戳正序或倒序排列的 activity,時間戳是其區分於其他控件的重要特征,使用時注意與 Steps 步驟條等區分。

我們發現 Timeline 組件是在element2.6.0版本才發布的,時間是:2019-03-01。

然后打開可視化工具--插件里的 vue-cli-plugin-element ,查看詳情發現它的最近發布時間是:2019-01-09。

這就說明我們安裝的插件 vue-cli-plugin-element 的發布時間,早於新版element增加 Timeline 功能的發布時間,所以需要手動導入。

把 timeline 和 timeline-item 文件夾復制粘貼到當前項目中的\src\plugins目錄下,然后打開element.js文件,添加代碼:

import Timeline from './timeline/index.js'
import TimelineItem from './timeline-item/index.js'

Vue.use(Timeline)
Vue.use(TimelineItem)

添加代碼:

<!--timeline時間線-->
<el-timeline>
      <el-timeline-item
        v-for="(activity, index) in progressInfo"
        :key="index"
        :timestamp="activity.time">
        {{activity.context}}
      </el-timeline-item>
</el-timeline>

<script>
import cityData from './citydata.js'

export default {
  data() {
    return {
      progressInfo: [] // 物流信息列表
    }
  }
}
</script>

此時效果圖:

導入 timeline 的樣式:

<style lang="less" scoped>
@import '../../plugins/timeline/timeline.css';
@import '../../plugins/timeline-item/timeline-item.css';
.el-cascader{width: 100%}
</style>

刷新后效果圖:

提交代碼並合並分支:

git add .
git commit -m "完成訂單功能的開發"
git push

git checkout master
git merge order
git push

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM