【问题标题】:How to filter data with checkbox in Vuejs with API?如何使用 API 在 Vuejs 中使用复选框过滤数据?
【发布时间】:2021-06-03 20:08:48
【问题描述】:

我想使用复选框过滤表中的数据,输出数据将根据选中/未选中显示,例如,当我选中“显示全部”时,将输出所有数据,当我未选中时,则仅输出将显示一些数据。但是当我尝试这样做时,结果在我被检查时没有任何改变

这里是 vue html:

<div class="col-md-8">
  <input type="checkbox" id="checkboxOrder" v-model="checkedValue" value="Onloading Complete" >
  <label for="checkboxOrder">Show All</label>
</div>
<table class="table table-bordered">
  <thead>
    <tr>
      <th>#</th>
      <th>code</th>
      <th>price</th>
      <th>status</th>
      <th>transporter</th>
      <th>driver</th>
      <th>Action</th>
    </tr>
  </thead>
  <tbody>
      <tr v-for="(item, index) in showOrderDetail" :key="index">
        <td >{{Number(index)+1}}</td>
        <td>{{item.code}}</td>
        <td>{{formatNumber(item.invoice_price)}}</td>
        <td :class="statusBackground(item)">
          <template v-if="item.t_tour">
            <span v-if="!_.isEmpty(item.t_tour.t_tour_detail)"> 
                {{item.t_tour.t_tour_detail[0].status}}
            </span>
            <span v-else>Unproccess</span>
          </template>
          <span v-else>Unproccess</span>
        </td>
        <td class="bg-warning">{{item.m_transporter ? item.m_transporter.name : 'unproccess'}}</td>
        <td>{{driver(item)}}</td>
        <td><span class="btn btn-primary" @click="showModal(item.t_tour)"><i class="fas fa-search"></i></span></td>
      </tr>
  </tbody>
</table>

还有我的 vue.js:

import axios from "axios";
import accounting from "accounting";
import ShowTourDetail from "~/pages/transaction/order/ShowTourDetail";
export default {
  props: {
    rowData: {
      type: Object,
      required: true
    },
    rowIndex: {
      type: Number
    }
  },
  data() {
    return {
      t_order_detail: {},
      checkedValue:[]
    };
  },
  mounted() {
    this.fetchData();
  },
  computed:{
    showOrderDetail(){
      if(!this.checkedValue.length)
        return this.t_order_detail
      return this.t_order_detail.filter(o => this.checkedValue.includes(o.t_tour.t_tour_detail[0].status))
    }
  },
  methods: {
    async fetchData() {
      this.t_order_detail = {
        ...(
          await axios.get(`/api/order-details`, {
            params: { t_order_id: this.rowData.id }
          })
        ).data
      };
    }
};

【问题讨论】:

    标签: javascript api vue.js checkbox axios


    【解决方案1】:

    您可以应用更改方法,例如:

    <input type="checkbox" :value="mainCat.merchantId" id="mainCat.merchantId" v-model="checkedCategories" @change="check($event)">
    

    类似的方法:

     methods: {
        check: function(e) {
            if(e.target.checked){
              console.log("load all data using service");
              // service call here
            }else{
              console.log("load required data using service");
               // service call here
           }
            
        }
      }
    

    【讨论】:

    • 谢谢,它可以工作,但是当第一次加载表时没有显示任何值,我必须先选中/取消选中才能显示那里的值
    • 如果有用,请接受代码并标记为有用。谢谢@srxctalia
    猜你喜欢
    • 2017-10-11
    • 2021-06-19
    • 2011-11-20
    • 2020-02-17
    • 2013-06-06
    • 2019-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多