elementUI中的table表格不同数据结构

项目中需要使用的表格数据结构是下图所示

但是找了elementUI中的表格发现并没有自己需要的

数据结构并不相同,于是做了一下改造,因为后台并不会传一个单独的表格数据,所以methods中定义了一个init()方法,在init()中改造数据结构,然后created调用方法,

1.样式结构

<div style="width:550px;">
    <el-table
      :data="tableData"
      style="width: 100%"
      :border="true"
      :header-cell-style="{'text-align':'center','background-color':'#f5f5f6'}"
      :cell-style="{'text-align': 'center' }"
      size="mini"
    >
      <el-table-column prop="type" label="类别" width="80"></el-table-column>
      <el-table-column prop="single" label="单选题">
        <template slot-scope="scope">
          <span v-text="scope.row.single"></span>
        </template>
      </el-table-column>
      <el-table-column prop="multiple" label="多选题">
        <template slot-scope="scope">
          <span v-text="scope.row.multiple"></span>
        </template>
      </el-table-column>
      <el-table-column prop="judge" label="判断题">
        <template slot-scope="scope">
          <span v-text="scope.row.judge"></span>
        </template>
      </el-table-column>
      <el-table-column prop="blank" label="填空题">
        <template slot-scope="scope">
          <span v-text="scope.row.blank"></span>
        </template>
      </el-table-column>
      <el-table-column prop="question" label="问答题">
        <template slot-scope="scope">
          <span v-text="scope.row.question"></span>
        </template>
      </el-table-column>
    </el-table>
</div>

2.模拟数据

<script>
export default {
  data() {
    return {
      paper: {
        singleCount: "45",
        multipleCount: "30",
        judgeCount: "25",
        singleScore: "1",
        multipleScore: "1",
        judgeScore: "1",
        scores: "100"
      },
      tableData: [{ type: "题数" }, { type: "分/题" }, { type: "总分" }]
    };
  },
  methods: {
    init() {
      this.tableData[0].single = this.paper.singleCount
        ? this.paper.singleCount
        : "-";
      this.tableData[1].single = this.paper.singleScore
        ? this.paper.singleScore + "分/题"
        : "-";
      this.tableData[2].single = this.paper.singleScore
        ? this.paper.singleScore * this.paper.singleCount
        : "-";
      this.tableData[0].multiple = this.paper.multipleCount
        ? this.paper.multipleCount
        : "-";
      this.tableData[1].multiple = this.paper.multipleScore
        ? this.paper.multipleScore + "分/题"
        : "-";
      this.tableData[2].multiple = this.paper.multipleScore
        ? this.paper.multipleScore * this.paper.multipleCount
        : "-";
      this.tableData[0].judge = this.paper.judgeCount
        ? this.paper.judgeCount
        : "-";
      this.tableData[1].judge = this.paper.judgeScore
        ? this.paper.judgeScore + "分/题"
        : "-";
      this.tableData[2].judge = this.paper.judgeScore
        ? this.paper.judgeScore * this.paper.judgeCount
        : "-";
      this.tableData[0].blank = this.paper.blankCount
        ? this.paper.blankCount
        : "-";
      this.tableData[1].blank = this.paper.blankScore
        ? this.paper.blankScore + "分/题"
        : "-";
      this.tableData[2].blank = this.paper.blankScore
        ? this.paper.blankScore * this.paper.blankCount
        : "-";
      this.tableData[0].question = this.paper.questionCount
        ? this.paper.questionCount
        : "-";
      this.tableData[1].question = this.paper.questionScore
        ? this.paper.questionScore + "分/题"
        : "-";
      this.tableData[2].question = this.paper.questionScore
        ? this.paper.questionScore * this.paper.questionCount
        : "-";
     // console.log(this.tableData);
    }
  },
  created() {
    this.init();
  }
};

这样就可以了

<think>我们参考引用[1]和引用[3]中的代码示例,它们展示了在ElementUItable组件中动态添加行和删除行,并在行内使用输入框。用户的需求是在ElementUI表格中动态添加输入框,并实现数据的绑定和验证。步骤:1.在表格列中使用`<el-input>`组件代替普通文本。2.通过作用域插槽(scopedslot)获取当前行数据,并绑定到输入框。3.动态添加行时,为新行初始化数据对象,包含输入框所需的字段。4.如果需要验证,可以在数据对象中定义验证规则,并在输入框上应用这些规则(如引用[1]所示)。示例代码结构:模板部分:-使用`<el-table>`组件,绑定数据数组(如tableData)。-在需要输入框的列中使用`<templateslot-scope="scope">`,内部使用`<el-input>`绑定到scope.row的某个属性。-提供添加行和删除行的按钮。数据部分:-定义tableData数组,用于存储表格数据。-每个数据对象应包含输入框对应的属性(如name、age等)以及一个可能用于标记编辑状态的属性(如edit,但非必须)。方法部分:-addData:向tableData中添加一个新的数据对象(初始化各字段为空或默认值)。-deleteData:根据索引删除行。验证(可选):-如引用[1],可以在数据对象中定义验证规则(rules),并在输入框上使用`:rules`属性。-通过`this.$refs[formName].validate`进行验证。注意:如果整个表格在一个表单内,可以使用`<el-form>`包裹`<el-table>`,并为每一行使用动态的prop(如`:prop="'tableData.'+scope.$index+'.name'"`)和规则。下面给出一个简化示例(包含添加、删除、输入框绑定,以及简单的提交功能):</think>以下是在 ElementUI 表格中动态添加输入框的实现方法,包含完整代码示例: ### 实现思路 1. **数据驱动**:通过动态修改 `tableData` 数组控制表格内容 2. **作用域插槽**:使用 `scoped-slot` 在单元格内嵌入输入框 3. **状态管理**:通过 `edit` 字段控制单元格的编辑/查看状态切换 4. **表单验证**:结合 ElementUI 的 Form 组件实现校验 ### 完整代码示例 ```html <template> <div> <!-- 表格区域 --> <el-table :data="tableData" border style="width: 100%"> <!-- 姓名列(带输入框) --> <el-table-column prop="name" label="姓名"> <template slot-scope="scope"> <el-input v-if="scope.row.edit" v-model="scope.row.name" placeholder="请输入姓名" ></el-input> <span v-else>{{ scope.row.name }}</span> </template> </el-table-column> <!-- 年龄列(带输入框) --> <el-table-column prop="age" label="年龄"> <template slot-scope="scope"> <el-input v-if="scope.row.edit" v-model="scope.row.age" placeholder="请输入年龄" type="number" ></el-input> <span v-else>{{ scope.row.age }}</span> </template> </el-table-column> <!-- 操作列 --> <el-table-column label="操作"> <template slot-scope="scope"> <el-button v-if="scope.row.edit" type="success" size="small" @click="confirmEdit(scope.row)" >保存</el-button> <el-button v-else type="primary" size="small" @click="startEdit(scope.row)" >编辑</el-button> <el-button type="danger" size="small" @click="deleteRow(scope.$index)" >删除</el-button> </template> </el-table-column> </el-table> <!-- 添加按钮 --> <el-button type="primary" style="margin-top: 20px" @click="addNewRow" >添加新行</el-button> </div> </template> <script> export default { data() { return { tableData: [ // 初始数据示例(带编辑状态标记) { name: '张三', age: 25, edit: false }, { name: '李四', age: 30, edit: false } ] } }, methods: { // 添加新行 addNewRow() { this.tableData.push({ name: '', age: '', edit: true // 新添加的行默认为编辑状态 }) }, // 开始编辑 startEdit(row) { row.edit = true }, // 确认编辑 confirmEdit(row) { // 这里可添加验证逻辑 if (!row.name.trim()) { this.$message.error('姓名不能为空') return } row.edit = false }, // 删除行 deleteRow(index) { this.tableData.splice(index, 1) } } } </script> ``` ### 关键实现点说明 1. **动态输入框控制**: ```html <el-input v-if="scope.row.edit"></el-input> <span v-else>{{ scope.row.name }}</span> ``` 通过 `edit` 字段切换输入框和文本显示 2. **新行添加**: ```javascript addNewRow() { this.tableData.push({ name: '', age: '', edit: true }) } ``` 新行自动进入编辑状态 3. **状态切换**: ```javascript startEdit(row) { row.edit = true; // 进入编辑模式 } confirmEdit(row) { // 验证通过后... row.edit = false; // 退出编辑模式 } ``` ### 表单验证扩展 若需要更复杂的验证,可结合 ElementUI Form 组件: ```html <el-form :model="form" ref="form"> <el-table :data="tableData"> <el-table-column label="姓名"> <template slot-scope="scope"> <el-form-item :prop="'tableData.' + scope.$index + '.name'" :rules="{ required: true, message: '姓名不能为空' }" > <el-input v-model="scope.row.name"></el-input> </el-form-item> </template> </el-table-column> </el-table> </el-form> ``` ### 数据回显说明 当需要从接口加载数据时,需确保数据结构匹配: ```javascript // 从API获取数据后 api.getData().then(res => { this.tableData = res.data.map(item => ({ ...item, edit: false // 初始为查看状态 })) }) ``` ### 注意事项 1. 使用 `scope.row` 直接修改行数据可实现双向绑定 2. 动态添加行时注意初始化所有必要字段 3. 复杂场景建议使用 `Vue.set` 确保响应式更新 4. 大数据量时考虑使用虚拟滚动优化性能 [^1]: ElementUI 表格内嵌输入框实现方法 [^2]: 动态表格数据回显处理技巧 [^3]: 表格行编辑状态管理实践
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值