如何在 vue.js 中创建用于创建和编辑功能的简单模式?

2024-05-15

我正在尝试创建一个模型以便能够编辑现有数据?我该怎么做呢?

我正在使用一个<el-form>要在本例中创建一个新条目,我正在创建问题,我想重复使用它进行编辑,并将条目中的数据添加到表单中。

这是我现在的表格

<el-dialog title="Nuevo" :loading="loading" :visible="dialogFormVisible" :visible.sync="dialogFormVisible">

    <el-form label-position="top" ref="form" :model="form" :rules="rules">
        <el-row>
            <el-form-item label="Pregunta" prop="question">
                <el-input v-model="form.question"></el-input>
            </el-form-item>
        </el-row>
        <el-row>
            <el-col :span="8">
                <el-form-item label="Seccion" prop="survey_section_id">
                    <el-select v-model="form.survey_section_id" placeholder="Selecionar">
                        <el-option v-for="section in survey_section" :key="section.id" :label="section.title"
                            :value="section.id">
                        </el-option>
                    </el-select>
                </el-form-item>
            </el-col>
            <el-col :span="8">
                <el-form-item label="Tipo de Respuesta" prop="response_type">
                    <el-select v-model="form.response_type_id" placeholder="Selecionar">
                        <el-option v-for="type in response_type" :key="type.id" :label="type.type" :value="type.id">
                        </el-option>
                    </el-select>
                </el-form-item>
            </el-col>
            <el-col :span="4">
                <el-form-item label="Opcional" prop="optional">
                    <el-switch v-model="form.optional"></el-switch>
                </el-form-item>
            </el-col>
        </el-row>
    </el-form>
    <span slot="footer">
        <el-button type="info" @click="dialogFormVisible = false">Cancelar
        </el-button>
        <el-button type="primary" :loading="loading" @click="submit('form')">Guardar
        </el-button>
    </span>
</el-dialog>

我需要做什么才能将其转换为模式并将其用于我的编辑?

有任何问题都可以问我,我可以提供我所需要的任何代码。 谢谢


这是一个常见问题。当然,其中很多事情都取决于个人喜好或意见,但这里有一些需要考虑的事情:

  1. 通常,在解决此问题时,您会以某种方式(即列表)显示所有现有模型,并带有“编辑”按钮。当我这样做时,我使用一个将模型 ID 映射到其相应模型对象的对象作为该列表的基础数据模型。这使得用更新版本替换模型变得容易(models[model.id] = model).

  2. 直接将模型绑定到表单(例如:model =“您要编辑的模型”)通常是一个糟糕的实现。这是因为编辑期间所做的任何更改都会立即写入模型。问题是,如果你的save()功能失败,那么你必须把一切都改回来。更好的实现是克隆模型,然后绑定that反对该表格。

  3. 您可以将计算属性用于模式的标题/标题。我通常有一个名为的数据属性mode,这将是“创建”或“编辑”,然后我有一个返回的计算属性mode + ' Model',其中“Model”是模型的名称 - 在您的例子中为“Pregunta”。

  4. 自从create and update函数通常使用不同的 API 端点(和 HTTP 方法),您的“保存/更新”按钮需要调用正确的端点。再次,我使用mode属性来处理这个问题(例如<button @click="mode == 'edit' ? update : save">)


下面的代码应该为您提供一个良好的起点,让您拥有一个可用于创建和编辑模型的模态,以及大多数模型的基本结构CRUD https://en.wikipedia.org/wiki/Create,_read,_update_and_delete成分。

<template>
  <div>
    <template v-for="model in modelsArray">
      ... <!-- Display your existing models however you want -->
      <a href="" @click.prevent="edit(model)">Edit</a>
    </template>
    <button @click="create">Create New Model</button>
    <el-dialog
      :title="modalTitle"
      :loading="loading"
      :visible.sync="dialogFormVisible"
    >
      <el-form :model="formModel">
        ...
      </el-form>
      <span slot="footer">
        <el-button type="info" @click="cancel">Cancelar</el-button>
        <el-button
          type="primary"
          :loading="loading"
          @click="mode == 'edit' ? update : save"
        >
          {{ mode }}
        </el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    mode: '',
    dialogFormVisible: false,
    loading: false,
    formModel: {},
    models: {            // an object mapping your existing model IDs
      1: { id: 1, ...},  // to their corresponding model objects
      2: { id: 2, ...},
      3: { id: 3, ...},
      ...
    }
  },
  ...,
  computed: {
    modelsArray() {
      return Object.values(this.models);
    },
    modalTitle() {
      return `${ this.mode } Model`;
    }
  },
  methods: {
    create() {
      this.mode = 'Create';
      this.dialogFormVisible = true;
    },
    edit(model) {
      this.mode = 'Edit';     
      this.formModel = _.cloneDeep(model); // See note below
      this.dialogFormVisible = true;
    },
    save() {
      // POST the new model (this.formModel) to your server using your ajax library of choice     
      // after the ajax call returns, do the following:
      this.models.push(the newly created model); // usually returned from the server as part of this ajax call
      this.reset();
    },
    update() {
      // PUT the new model (this.formModel) to your server using your ajax library of choice
      // After the ajax call returns, replace the updated model in the `models` object, and reset the form:
      this.models[this.formModel.id] = _.cloneDeep(this.formModel);
      this.reset();
    },
    reset() {
      this.dialogFormVisible = false;
      this.loading = false;
      this.formModel = {
        ... // put default form values here
      };
    }
  },
  mounted() {
    this.reset();
  }
}
</script

NOTE: _.cloneDeep https://lodash.com/docs#cloneDeep来自洛达什。如果您不使用 lodash,您可以使用其中之一这些方法 https://stackoverflow.com/a/728694/3109473克隆模型。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在 vue.js 中创建用于创建和编辑功能的简单模式? 的相关文章

随机推荐