vue+ElementUI实现订单页动态添加产品效果

使用vue2.0(ElementUI基于vue2.0)+ElementUI(饿了么出品)实现的在订单页面动态添加产品的效果,并自动计算总价。代码直接保存为html文档,使用浏览器打开即可查看效果。
效果图:这里写图片描述
这里写图片描述

<html>
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
        <meta content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" name="viewport" />
        <!-- 引入element样式 -->
       <link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-default/index.css">
       <title>订单页面</title>
    </head>
    <body>
        <div id="orderTest">



            <el-dialog title="产品库" v-model="dialogTableVisible">
              <el-table :data="list">
                <el-table-column property="name" label="产品名称" width="150"></el-table-column>
                <el-table-column property="price" label="单价" width="200"></el-table-column>
                <el-table-column :context="_self" inline-template label="操作">
                  <div>
                    <el-button
                      size="small"
                      @click="choise(row)">
                      选择
                    </el-button>
                  </div>
                </el-table-column>
              </el-table>
            </el-dialog>

            <el-button type="info" icon="view" @click="dialogTableVisible = true">选择产品</el-button>
            <span v-show="checkedNames.length>0" style="font-family: Microsoft YaHei">总价:{{sumPrice}}</span>
            <el-table :data="checkedNames" v-show="checkedNames.length>0">
                <el-table-column property="name" label="产品名称" width="150"></el-table-column>
                <el-table-column property="price" label="单价" width="200"></el-table-column>
                <el-table-column inline-template label="数量" width="200">
                    <el-input-number  v-model="row.num" :min="1" :max="10"></el-input-number>
                </el-table-column>
                <el-table-column :context="_self" inline-template label="操作">
                    <i class="el-icon-circle-cross" @click="del(row)" title="删除"></i>
                </el-table-column>
            </el-table>


        </div>
    </body>
<!-- 引入vue -->
        <script src="https://unpkg.com/vue/dist/vue.js"></script>
        <!-- 引入element JS -->
        <script src="https://unpkg.com/element-ui/lib/index.js"></script>
        <script src="http://cdn.bootcss.com/jquery/3.1.1/jquery.js"></script>
        <script type="text/javascript">
            var order = new Vue({
                    el: '#orderTest',
                    data: {
                        dialogTableVisible:false,
                        checkedNames: [],
                        list:[
                                {name:"iphone7",price:5688,num:1},
                                {name:"荣耀8",price:2299,num:1},
                                {name:"Lumia830",price:1299,num:1}
                             ]
                    },
                    computed:{
                        sumPrice:function(){
                            var sum = 0 ;
                            for(var i=0;i< this.checkedNames.length;i++){
                                sum+=this.checkedNames[i].price*this.checkedNames[i].num;
                                }
                            return sum;
                            }
                        },
                    methods:{
                        choise:function(p){
                           order.checkedNames.push(p);
                        },
                        del:function(p){
                            order.checkedNames.splice($.inArray(p, order.checkedNames), 1);
                        },
                    }
                });
        </script>

</html>
  • 0
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
首先,你需要安装VueElementUI,可以通过以下命令进行安装: ```bash npm install vue element-ui --save ``` 然后在你的Vue项目中引入ElementUIVue Router,并创建一个顶部导航栏组件。这个组件将包含一个菜单和一个路由视图。 ```html <template> <div> <el-menu :default-active="$route.path" class="el-menu-demo" mode="horizontal" @select="handleSelect"> <el-menu-item index="/">首</el-menu-item> <el-submenu index="/products"> <template slot="title">产品</template> <el-menu-item index="/products/list">产品列表</el-menu-item> <el-menu-item index="/products/add">添加产品</el-menu-item> </el-submenu> <el-submenu index="/orders"> <template slot="title">订单</template> <el-menu-item index="/orders/list">订单列表</el-menu-item> <el-menu-item index="/orders/add">添加订单</el-menu-item> </el-submenu> </el-menu> <router-view></router-view> </div> </template> <script> import { mapState } from 'vuex'; export default { computed: { ...mapState(['user']) }, methods: { handleSelect(key, keyPath) { this.$router.push(key); } } }; </script> <style scoped> .el-menu-demo { margin-bottom: 20px; } </style> ``` 在这个组件中,我们使用了ElementUI的`el-menu`组件来创建菜单,并使用Vue Router的`$route.path`来设置默认选项。我们还使用了Vue Router的`router-view`组件来显示当前路由的内容。 接下来,我们需要定义路由和组件。我们可以在Vue Router的配置中定义路由,如下所示: ```javascript import Vue from 'vue'; import Router from 'vue-router'; Vue.use(Router); const HomePage = () => import('@/views/HomePage.vue'); const ProductListPage = () => import('@/views/ProductListPage.vue'); const ProductAddPage = () => import('@/views/ProductAddPage.vue'); const OrderListPage = () => import('@/views/OrderListPage.vue'); const OrderAddPage = () => import('@/views/OrderAddPage.vue'); export default new Router({ mode: 'history', routes: [ { path: '/', component: HomePage }, { path: '/products/list', component: ProductListPage }, { path: '/products/add', component: ProductAddPage }, { path: '/orders/list', component: OrderListPage }, { path: '/orders/add', component: OrderAddPage } ] }); ``` 在这个配置中,我们定义了五个路由和对应的组件。我们还使用了Vue Router的`mode: 'history'`来启用HTML5历史记录模式。 最后,我们需要在Vue项目中引入并使用Vue Router和顶部导航栏组件。我们可以在Vue实例中定义Vue Router,并在Vue模板中使用顶部导航栏组件,如下所示: ```javascript import Vue from 'vue'; import App from './App.vue'; import router from './router'; import store from './store'; Vue.config.productionTip = false; new Vue({ router, store, render: h => h(App) }).$mount('#app'); ``` ```html <template> <div id="app"> <top-nav /> </div> </template> <script> import TopNav from '@/components/TopNav.vue'; export default { components: { TopNav } }; </script> ``` 现在,你已经成功地使用VueElementUI创建了一个顶部导航栏,并实现动态路由。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值