更改ElementUi Vue中的输入后,“选择框”不会立即更改

时间:2019-11-14 14:27:15

标签: laravel vue.js laravel-6 element-ui

我用Laravue制作了一个基于CRUD的管理面板。现在我的问题是,每当我在更新页面上时,都无法立即更改类别。我总是必须在其他字段中进行更改才能查看更改的类别。当所有字段都为空时,它可以在添加卡页面上正常工作。

这是我问题的视频 https://thisisntmyid.tumblr.com/post/189075383873/my-issue-with-elementui-and-laravue-and-vue

我已经为清单页面以及添加/更新页面添加了代码。

将导致您进入编辑页面的列表页面代码

  <div class="app-container">
    <h1>My Cards</h1>
    <Pagination
      :total="totalCards"
      layout="total, prev, pager, next"
      :limit="10"
      :page.sync="currentPage"
      @pagination="loadNewPage"
    ></Pagination>
    <el-table v-loading="loadingCardsList" :data="cards" stripe style="width: 100%">
      <el-table-column prop="name" sortable label="Product Name"></el-table-column>
      <el-table-column prop="description" label="Description" width="200px"></el-table-column>
      <el-table-column prop="price" label="Price"></el-table-column>
      <el-table-column prop="cardcategory" sortable label="Category"></el-table-column>
      <el-table-column label="Operations" width="300px">
        <template slot-scope="scope">
          <el-button size="mini" type="primary" @click="handleView(scope.$index, scope.row)">View</el-button>
          <el-button size="mini" @click="handleEdit(scope.$index, scope.row)">Edit</el-button>
          <el-button size="mini" type="danger" @click="handleDelete(scope.$index, scope.row)">Delete</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog :visible.sync="viewCard" width="30%">
      <el-card class="box-card">
        <h1>{{currentViewedCard.name}}</h1>
        <p>{{currentViewedCard.description}}</p>
        <p>{{currentViewedCard.price}}</p>
        <el-tag>{{currentViewedCard.cardcategory}}</el-tag>
      </el-card>
    </el-dialog>
  </div>
</template>

<script>
import Resource from '@/api/resource';
import Pagination from '@/components/Pagination/index.vue';
const cardcategoryResource = new Resource('cardcategories');
const cardResource = new Resource('cards');

export default {
  name: 'Cards',
  components: {
    Pagination,
  },
  data() {
    return {
      cards: [],
      categories: [],
      currentPage: '',
      totalCards: '',
      loadingCardsList: true,
      currentViewedCard: '',
      viewCard: false,
    };
  },
  created() {
    this.getCardCategories();
    this.getCardList({ page: 1 });
  },
  methods: {
    async getCardList(query) {
      this.loadingCardsList = true;
      const data = await cardResource.list(query);
      this.cards = data.data;
      for (const card of this.cards) {
        card['cardcategory'] = this.getCategoryName(card.cardCategory_id);
      }
      console.log(this.cards);
      this.totalCards = data.total;
      this.loadingCardsList = false;
    },
    async getCardCategories() {
      this.categories = await cardcategoryResource.list({});
      console.log(this.categories);
    },
    loadNewPage(val) {
      this.getCardList({ page: val.page });
    },
    getCategoryName(id) {
      return this.categories[id - 1].name;
    },
    handleView(index, info) {
      this.viewCard = true;
      this.currentViewedCard = info;
    },
    handleEdit(index, info) {
      this.$router.push('/cards/edit/' + info.id);
    },
    closeDialog() {
      this.viewProduct = false;
      this.currentProductInfo = null;
    },
    handleDelete(index, info) {
      cardResource.destroy(info.id).then(response => {
        this.$message({
          message: 'Card Deleted Successfully',
          type: 'success',
          duration: 3000,
        });
        this.getCardList({ page: this.currentPage });
      });
    },
  },
};
</script>

<style lang="scss" scoped>
</style>

添加/更新页面的代码

<template>
  <div class="app-container">
    <el-form ref="form" :model="formData" label-width="120px">
      <el-form-item label="Name">
        <el-input v-model="formData.name"></el-input>
      </el-form-item>
      <el-form-item label="Description">
        <el-input type="textarea" v-model="formData.description"></el-input>
      </el-form-item>
      <el-form-item label="Price">
        <el-input v-model="formData.price"></el-input>
      </el-form-item>
      <el-form-item label="Category">
        <!-- DO something here like binding category id to category name sort of meh... -->
        <el-select v-model="formData.cardcategory" placeholder="please select category">
          <el-option v-for="item in categories" :key="item" :label="item" :value="item"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="editable ? updateProduct() : createProduct()">Save</el-button>
        <el-button @click="editable ? populateFormData($route.params.id) : formDataReset()">Reset</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import Resource from '@/api/resource';
const cardcategoryResource = new Resource('cardcategories');
const cardResource = new Resource('cards');

export default {
  data() {
    return {
      formData: {
        name: '',
        description: '',
        price: '',
        cardcategory: '',
      },
      categories: [],
      editable: '',
    };
  },
  created() {
    this.getCategories();
    if (this.$route.params.id) {
      this.editable = true;
      this.populateFormData(this.$route.params.id);
    } else {
      this.editable = false;
    }
  },
  methods: {
    async getCategories() {
      this.categories = (await cardcategoryResource.list({})).map(
        cat => cat.name
      );
      console.log(this.categories);
    },
    formDataReset() {
      this.formData = {
        name: '',
        description: '',
        price: '',
        cardcategory: '',
      };
    },
    populateFormData(id) {
      cardResource.get(id).then(response => {
        this.formData = Object.assign({}, response);
        this.formData.price = this.formData.price.toString();
        this.formData.cardcategory = this.categories[
          this.formData.cardCategory_id - 1
        ];
        delete this.formData.cardCategory_id;
      });
    },
    filterFormData(formData) {
      const cardData = Object.assign({}, formData);
      cardData['cardCategory_id'] =
        this.categories.indexOf(cardData.cardcategory) + 1;
      delete cardData.cardcategory;
      return cardData;
    },
    createProduct() {
      const cardData = this.filterFormData(this.formData);
      cardResource
        .store(cardData)
        .then(response => {
          this.$message({
            message: 'New Card Added',
            type: 'success',
            duration: 3000,
          });
          this.formDataReset();
        })
        .catch(response => {
          alert(response);
        });
    },
    updateProduct() {
      const cardData = this.filterFormData(this.formData);
      cardResource.update(this.$route.params.id, cardData).then(response => {
        this.$message({
          message: 'Card Updated Successfully',
          type: 'success',
          duration: 3000,
        });
        this.populateFormData(this.$route.params.id);
      });
    },
  },
};
</script>

<style>
</style>

更新 这是我的项目的git hub回购 https://github.com/ThisIsntMyId/laravue-admin-pannel-demo

0 个答案:

没有答案