Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
168 views
in Technique[技术] by (71.8m points)

javascript - Computed data in table not updating after changing radio value

I have the following problem with the data table I'm trying to implement in my VueJS frontend. When I try to change the supplier, the price needs to be updated in the cost column on the far right column of the table, and same for the total cost given by a computed property. I don't understand why the data is not being updated, I've tried this.$forceUpdate() and nothing works..

Thanks for your help.

enter image description here

    <template>
   <div>
      <v-row>
         <v-col
             cols="2">
            <v-date-picker
                v-model="dateRange"
                range show-adjacent-months
                @input="fetchWeeklyIngredients"/>

            <h1>Total cost: ${{Math.round(totalCost)}}</h1>
         </v-col>
         <v-col>
            <v-simple-table>
               <template v-slot:default>
                  <thead>
                  <tr>
                     <th class="text-left">
                        Ingredient
                     </th>
                     <th class="text-left">
                        Weight
                     </th>
                     <th class="text-left">
                        Status
                     </th>
                     <th class="text-left">
                        Suppliers
                     </th>
                     <th class="text-left">
                        Cost
                     </th>
                  </tr>
                  </thead>
                  <tbody>
                  <tr
                      v-for="ingredient in ingredients"
                      :key="ingredient.id">
                     <td>{{ ingredient.name }}</td>
                     <td>{{ Math.round(ingredient.weight*100)/100 }}</td>
                     <td>{{ ingredient.status }}</td>
                     <td>
                        <v-radio-group
                            v-if="ingredient.suppliers.length"
                            v-model="ingredient.selectedSupplier">
                           <v-radio
                               v-for="supplier in ingredient.suppliers"
                               :key="supplier.id"
                               :value="supplier"
                               :label="supplier.name+' | Priority: '+supplier.priority+' | Price: '+supplier.cost"/>
                        </v-radio-group>
                     </td>

                     <td v-if="ingredient.selectedSupplier">
                        {{Math.round(ingredient.weight*ingredient.selectedSupplier.cost)}}
                     </td>
                  </tr>
                  </tbody>
               </template>
            </v-simple-table>
         </v-col>
      </v-row>
   </div>
</template>



<script>

import axios from 'axios'
export default {
   name: "WeeklyIngredients",
   data(){
      return{
         dateRange:[],
         ingredients: [],
         headers:[
            {
               text: 'Ingredient',
               align: 'start',
               sortable: true,
               value: 'name',
            },
            {
               text: 'Weight',
               value: 'weight',
               sortable: true
            },
            {
               text: 'Status',
               value: 'status',
               sortable: true
            },
            {
               text: 'Suppliers',
               value: 'suppliers',
               sortable: false
            },
            {
               text: 'Cost',
               value: 'cost',
               sortable: true
            }
         ],
         loading: false,
         itemsPerPage: 10
      }
   },
   computed:{
      sortedDateRange(){
         return this.dateRange.sort()
      },
      totalCost(){
        return this.ingredients
             .filter(ingredient => ingredient.selectedSupplier)
             .reduce((totalCost, ingredient) =>
                 totalCost + ingredient.weight*ingredient.selectedSupplier.cost,
                 0)
      }
   },
   methods:{
      fetchWeeklyIngredients(){
         if(this.sortedDateRange.length<2){return;}

         this.loading = true;

         axios.get("/kitchenReport/ingredients/"+this.sortedDateRange[0]+"/"+this.sortedDateRange[1])
             .then(({data}) => {
                this.ingredients = data.ingredients;
                this.ingredients.map((ingredient,index) => {
                   if(ingredient.suppliers.length>0){
                      this.ingredients[index].selectedSupplier = this.ingredients[index].suppliers[0];
                   }
                })
                this.loading=false;
             })
      },
      selectedSupplierCost(suppliers){
         if(!suppliers.length>0){return 0;}

         const selectedSupplier = suppliers.find(supplier => supplier.selected)
         return (selectedSupplier ? selectedSupplier.cost : 0)
      }
   }
}
</script>

EDIT: This is the structure of the ingredients array of objects:

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...