0

請求書を作成するための動的な製品リストがあります。ここで、select->option list から製品を検索したいと思います。vuejs で Vue-select のような解決策を見つけましたが、既存のコードを変換して Vue-select を利用する方法がわかりませんでした。リストから一度に製品を検索できるように、「選択」にコードをどのように記述すればよいですか?

私の既存のコードは -

<td>
   <select id="orderproductId" ref="selectOrderProduct" class="form-control  input-sm" @change="setOrderProducts($event)">
     <option>Choose Product ...</option>
     <option :value="product.id + '_' + product.product_name" v-for="product in invProducts">@{{ product.product_name }}</option>
   </select>                      
</td>

そして、私はそれを次のように変換したい-

<v-select :options="options"></v-select>

そのため、商品がたくさんある場合でも商品を検索できます。そして私のスクリプトファイルは -

<script>
Vue.component('v-select', VueSelect.VueSelect);
var app = new Vue({
  el: '#poOrder',
  data: {

    orderEntry: {
        id: 1,
        product_name: '',
        quantity: 1,
        price: 0,
        total: 0,
    },
    orderDetail: [],
    grandTotal: 0,
    invProducts: [],
    invProducts: [
        @foreach ($productRecords as $invProduct)
            {
                id:{{ $invProduct['id'] }},
                product_name:'{{ $invProduct['product_name'] }}',

            },
        @endforeach
    ],

  },



  methods: {
    setOrderProducts: function(event) {
        //alert('fired');
        var self = this;
        var valueArr = event.target.value.split('_');
        var selectProductId = valueArr[0];
        var selectProductName = valueArr[1];

        self.orderEntry.id = selectProductId;
        self.orderEntry.product_name = selectProductName;

        $('#invQuantity').select();
    },
    addMoreOrderFields:function(orderEntry) {
        var self = this;
        if(orderEntry.product_name && orderEntry.quantity && orderEntry.price > 0) {

            self.orderDetail.push({
                id: orderEntry.id,
                product_name: orderEntry.product_name,
                quantity: orderEntry.quantity,
                price: orderEntry.price,
                total: orderEntry.total,
            });

            self.orderEntry = {
                id: 1,
                product_name:'',
                productId: 0,
                quantity: 1,
                price: 0,
                total: 0,
            }
            $('#orderproductId').focus();
            self.calculateGrandTotal();

        } else {
            $('#warningModal').modal();
        }
        this.$refs.selectOrderProduct.focus();

    },

    removeOrderField:function(removeOrderDetail) {
        var self = this;
        var index = self.orderDetail.indexOf(removeOrderDetail);
        self.orderDetail.splice(index, 1);
        self.calculateGrandTotal();
    },

    calculateGrandTotal:function() {

        var self = this;
        self.grandTotal = 0;
        self.totalPrice = 0;
        self.totalQuantity = 0;

        self.orderDetail.map(function(order){
            self.totalQuantity += parseInt(order.quantity);
            self.totalPrice += parseInt(order.price);
            self.grandTotal += parseInt(order.total);
        });
    },


    setTotalPrice:function(event){

        var self = this;
        //self.netTotalPrice();
        self.netTotalPrice;
    }   

  },

  computed: {
    netTotalPrice: function(){
        var self = this;

        var netTotalPriceValue = self.orderEntry.quantity * self.orderEntry.price;

        var netTotalPriceInDecimal = netTotalPriceValue.toFixed(2);

        self.orderEntry.total = netTotalPriceInDecimal;

        return netTotalPriceInDecimal;
    }

  }
});

4

1 に答える 1

0

invProductsが製品オブジェクトの配列であり、各製品オブジェクトにプロパティがあると仮定してproduct_name、このスニペットを試してください。

<v-select @input="selectChange()"   :label="product_name" :options="invProducts" v-model="selectedProduct">

</v-select>

という新しいデータ プロパティを作成し、selectedProductそれを vue-select コンポーネントにバインドします。そのため、vue-select の選択が変更されるたびに、 の値selectedProductも変更されます。これに加えて、@inputイベントを使用してコンポーネント内のメソッドをトリガーできます。そのメソッドで選択した製品を取得し、そのイベント リスナー内でさらにアクションを実行できます。

methods: {

 selectChange : function(){
 console.log(this.selectedProduct);
 //do futher processing

}
于 2018-04-19T04:23:23.757 に答える