【问题标题】:Conditional Component variable value increment Vue/Laravel条件组件变量值递增Vue/Laravel
【发布时间】:2021-03-26 05:33:39
【问题描述】:

[1]所以我有一个 laravel 项目正在进行,我想增加变量 Deliver_later_num 的值,这取决于我输出的 items[] 数组中同一组件中存在的“deliver_today”模板文件,我不知道该怎么做,我不知道我是否可以在模板端或组件端增加值。这是组件代码:

 cartContent = new Vue({
        el: '#cartList',
        data: {
            items: [], //array containing all the items
            deliver_later_num: 0, //value to increment 
        },
        methods: {
            remove: function (product_id) {
                removeProductIfFromCart(product_id);
            },
            incQuantity: function (product_id){
                incCart(product_id)
            },
            decQuantity: function (product_id){
                decCart(product_id)
            },
           
        }
    })

这是模板文件:

<div id="cartList">
                <div v-for="item in items" class="items col-xs-12 col-sm-12 col-md-12 col-lg-12 clearfix">
                    <div class="info-block block-info clearfix" v-cloak>
                        <div class="square-box pull-left">
                            <img :src="item.attributes.image"  class="productImage" width="100" height="105" alt="">
                        </div>
                        <h6 class="product-item_title">@{{ item.name }}</h6>
                        <p class="product-item_quantity">@{{ item.quantity }} x @{{ item.attributes.friendly_price }}</p>
                        <ul class="pagination">
                            <li class="page-item">
                                <button v-on:click="decQuantity(item.id)" :value="item.id" class="page-link" tabindex="-1">
                                    <i class="fa fa-minus"></i>
                                </button>
                            </li>
                            <li class="page-item">
                                <button v-on:click="incQuantity(item.id)" :value="item.id" class="page-link" >
                                    <i class="fa fa-plus"></i>
                                </button>
                            </li>

                            <li class="page-item">
                                <button v-on:click="remove(item.id)"  :value="item.id" class="page-link" >
                                    <i class="fa fa-trash"></i>
                                </button>
                            </li>

                            <input hidden  class="delivers_today_state" type="text" :value=" item.attributes.delivers_today "> // if this equals 0 i want to increment the deliver_later_num value
                       

                        </ul>
                   </div>
                </div>
            </div>

laravel 控制器代码:

  public function add(Request $request){
    $item = Items::find($request->id);
    $restID=$item->category->restorant->id;

    //Check if added item is from the same restorant as previus items in cart
    $canAdd = false;
    if(Cart::getContent()->isEmpty()){
        $canAdd = true;
    }else{
        $canAdd = true;
        foreach (Cart::getContent() as $key => $cartItem) {
            if($cartItem->attributes->restorant_id."" != $restID.""){
                $canAdd = false;
                break;
            }
        }
    }

    //TODO - check if cart contains, if so, check if restorant is same as pervious one

   // Cart::clear();
    if($item && $canAdd){

        //are there any extras
        $cartItemPrice=$item->price;
        $cartItemName=$item->name;
        $theElement="";

        //Is there a varaint
        //variantID
        if($request->variantID){
            //Get the variant
            $variant=Variants::findOrFail($request->variantID);

            $cartItemPrice=$variant->price;
            $cartItemName=$item->name." ".$variant->optionsList;
            //$theElement.=$value." -- ".$item->extras()->findOrFail($value)->name."  --> ". $cartItemPrice." ->- ";
        }


        foreach ($request->extras as $key => $value) {

            $cartItemName.="\n+ ".$item->extras()->findOrFail($value)->name;
            $cartItemPrice+=$item->extras()->findOrFail($value)->price;
            $theElement.=$value." -- ".$item->extras()->findOrFail($value)->name."  --> ". $cartItemPrice." ->- ";
        }


        Cart::add((new \DateTime())->getTimestamp(), $cartItemName, $cartItemPrice, $request->quantity, array('id'=>$item->id,'variant'=>$request->variantID, 'extras'=>$request->extras,'restorant_id'=>$restID,'image'=>$item->icon,'friendly_price'=>  Money($cartItemPrice, env('CASHIER_CURRENCY','usd'),true)->format(),'delivers_today' => $item->deliverstoday ));


        return response()->json([
            'status' => true,
            'errMsg' => $theElement
        ]);
    }else{
        return response()->json([
            'status' => false,
            'errMsg' => __("You can't add items from other restaurant!")
        ]);
        //], 401);
    }
}

public function getContent(){
    //Cart::clear();
    return response()->json([
        'data' => Cart::getContent(),
        'total' => Cart::getSubTotal(),
        'status' => true,
        'errMsg' => ''
    ]);
}

链接到项目数组 vue 开发工具截图 [1]:https://i.stack.imgur.com/smLRV.png

感谢您宝贵的帮助和时间。

【问题讨论】:

    标签: laravel vue.js


    【解决方案1】:

    如果deliver_later_num 仅依赖于items 数组元素上deliver_today 的存在/不存在,则可以使用计算属性

     cartContent = new Vue({
        el: '#cartList',
        data: {
            items: {}
        },
    
        computed: {
            deliver_later_num() {
                let num = 0;
                Object.keys(this.items).forEach(key => {
                    let item = this.items[key];
                    Object.keys(item).forEach(k => {
                        if(k === 'deliver_today' && item[k]) {
                            num++;
                        }
                    });
                });
    
                return num;
             },
        }
    

    【讨论】:

    • 它似乎不起作用,当我添加您提供的代码时,商品将不再添加到购物车中,并且在 Deliver_later_num 的插值字段中没有真正显示:/
    • items 数组是如何填充的?
    • @HatemTEMIMI 已更新答案以使用计算属性而不是观察者,请检查并告诉我
    • 和以前一样,它阻止我将商品添加到购物车,并出现以下错误:“vue.js:437 TypeError: this.items.forEach is not a function”。我尝试删除迭代块并使用 num = 5 初始化 num 变量,它起作用了,所以它一定是我刚刚指出的错误。
    • 当您将商品添加到购物车时,devtools 会为商品显示什么 - 在任何时候都是商品而不是数组。您是否在任何方法中将其设置为 null 或任何其他类型
    猜你喜欢
    • 2015-11-03
    • 2020-07-26
    • 2019-11-02
    • 1970-01-01
    • 2020-12-21
    • 2021-02-04
    • 2021-11-06
    • 2021-02-27
    • 2017-05-22
    相关资源
    最近更新 更多