scope.row的含义及应用

一、scope.row的概念

在Vue.js的组件中,我们经常会使用到scope.row,其实scope.row代表组件中的一行数据。scope.row通常用于循环表格中的表格行,可以在table或el-table等组件中使用。

在该组件内部,每一行都会生成一个可绑定的对象,该对象就是每一行的数据。scope.row会自动为我们绑定该对象,在组件内部可以通过该对象来获取行的具体数据。

二、scope.row的用法

使用scope.row可以获取到当前行的数据对象,从而再进行对该行数据的一系列操作。

1. 获取当前行数据

<el-table :data="tableData">
  <el-table-column prop="name">
    <template scope="scope">
      {{ scope.row.name }}
    </template>
  </el-table-column>
  <el-table-column>
    <template scope="scope">
      <el-button @click="showRow(scope.row)">查看详情</el-button>
    </template>
  </el-table-column>
</el-table>

在上述代码实例中,我们使用了scope.row.name获取当前行数据中的name属性,并在模板中进行了展示。同时,我们可以通过onClick事件,借助该按钮查看当前行的具体详情。

2. 对当前行数据的修改

<el-table :data="tableData">
  <el-table-column prop="name">
    <template scope="scope">
      <el-input v-model="scope.row.name"></el-input>
    </template>
  </el-table-column>
  <el-table-column>
    <template scope="scope">
      <el-button @click="editRow(scope.row)">保存</el-button>
    </template>
  </el-table-column>
</el-table>

在上述代码实例中,我们使用了一个带有v-model指令的el-input,该指令会自动绑定当前行的name属性。同时,在点击保存按钮时,便可以触发对当前行数据的修改。

3. 对当前行数据的删除

<el-table :data="tableData">
  <el-table-column>
    <template scope="scope">
      <el-button @click="deleteRow(scope.row)">删除</el-button>
    </template>
  </el-table-column>
</el-table>

在上述代码实例中,我们使用了一个带有onClick事件的el-button,该事件会触发对当前行数据的删除操作。

三、scope.row的优缺点

1. 优点:

scope.row可以很方便的获取当前行的数据,并且可通过该数据进行一系列操作,如修改、删除等。

当配合vue.js的计算属性使用时,会使得组件的数据操作更加简洁、直观。

2. 缺点:

scope.row可能会让组件模板中的代码显得冗长,同时还需要手动引入el-table等组件,使代码的组织结构不够清晰。

四、总结

scope.row是Vue.js组件中经常用到的概念,其代表了我们要处理的一行数据。通过scope.row,我们可以方便的获取到行数据,并对该数据进行一系列操作,使组件的数据处理更加简洁、直观。


原文链接:scope.row的含义及应用_笔记大全_设计学院 (python100.com)

如若内容造成侵权,请联系邮箱:2375622971@qq.com

<div v-else-if="per.dict_value === 'file_name'"> <el-tooltip placement="top" effect="dark" v-if="scope.row.download_time || scope.row.repair_download_time"> <div slot="content" > <span v-if="scope.row.repair_download_time">{{ parseTime(scope.row.repair_download_time,'{y}-{m}-{d} {h}:{i}:{s}') }}</span> <span v-else-if="scope.row.download_time">{{ parseTime(scope.row.download_time,'{y}-{m}-{d} {h}:{i}:{s}') }}</span> </div> <el-link v-if="scope.row.repair_file_id" :key="scope.row.repair_file_id" style="display: block;color: #67C23A" icon="el-icon-paperclip" @click="downloadOrderExcel(scope.row.repair_file_id, scope.row.order_no, scope.row.repair_file_name)" >{{ scope.row.repair_file_name }}</el-link> <el-link v-else-if="scope.row.file_id" :key="scope.row.file_id" style="display: block;color: #67C23A" icon="el-icon-paperclip" @click="downloadOrderExcel(scope.row.file_id, scope.row.order_no, scope.row.file_name)" >{{ scope.row[per.dict_value] }}</el-link> </el-tooltip> <div v-else> <el-link v-if="scope.row.repair_file_id" :key="scope.row.repair_file_id" style="display: block;" icon="el-icon-paperclip" @click="downloadOrderExcel(scope.row.repair_file_id, scope.row.order_no, scope.row.repair_file_name)" >{{ scope.row.repair_file_name }}</el-link> <el-link v-else-if="scope.row.file_id" :key="scope.row.file_id" style="display: block;" icon="el-icon-paperclip" @click="downloadOrderExcel(scope.row.file_id, scope.row.order_no, scope.row.file_name)" >{{ scope.row[per.dict_value] }}</el-link> </div> </div>优化这段代码用vue2.0
06-07
首先,将指令 `v-if` 和 `v-else-if` 改为计算属性,以减少模板中的逻辑判断。 ```javascript computed: { hasDownloadTime() { return this.scope.row.download_time || this.scope.row.repair_download_time } }, ``` 然后,将模板中的 `v-if` 和 `v-else-if` 替换为计算属性的使用。 ```html <div> <el-tooltip placement="top" effect="dark" v-if="hasDownloadTime"> <div slot="content"> <span v-if="scope.row.repair_download_time">{{ parseTime(scope.row.repair_download_time,'{y}-{m}-{d} {h}:{i}:{s}') }}</span> <span v-else-if="scope.row.download_time">{{ parseTime(scope.row.download_time,'{y}-{m}-{d} {h}:{i}:{s}') }}</span> </div> <el-link v-if="scope.row.repair_file_id" :key="scope.row.repair_file_id" style="display: block;color: #67C23A" icon="el-icon-paperclip" @click="downloadOrderExcel(scope.row.repair_file_id, scope.row.order_no, scope.row.repair_file_name)" >{{ scope.row.repair_file_name }}</el-link> <el-link v-else-if="scope.row.file_id" :key="scope.row.file_id" style="display: block;color: #67C23A" icon="el-icon-paperclip" @click="downloadOrderExcel(scope.row.file_id, scope.row.order_no, scope.row.file_name)" >{{ scope.row.file_name }}</el-link> </el-tooltip> <div v-else> <el-link v-if="scope.row.repair_file_id" :key="scope.row.repair_file_id" style="display: block;" icon="el-icon-paperclip" @click="downloadOrderExcel(scope.row.repair_file_id, scope.row.order_no, scope.row.repair_file_name)" >{{ scope.row.repair_file_name }}</el-link> <el-link v-else-if="scope.row.file_id" :key="scope.row.file_id" style="display: block;" icon="el-icon-paperclip" @click="downloadOrderExcel(scope.row.file_id, scope.row.order_no, scope.row.file_name)" >{{ scope.row.file_name }}</el-link> </div> </div> ``` 最后,将 `{{ scope.row[per.dict_value] }}` 替换为 `{{ scope.row.file_name }}`。完整的代码如下: ```html <div> <el-tooltip placement="top" effect="dark" :visible="hasDownloadTime"> <div slot="content"> <span v-if="scope.row.repair_download_time">{{ parseTime(scope.row.repair_download_time,'{y}-{m}-{d} {h}:{i}:{s}') }}</span> <span v-else-if="scope.row.download_time">{{ parseTime(scope.row.download_time,'{y}-{m}-{d} {h}:{i}:{s}') }}</span> </div> <el-link v-if="scope.row.repair_file_id" :key="scope.row.repair_file_id" style="display: block;color: #67C23A" icon="el-icon-paperclip" @click="downloadOrderExcel(scope.row.repair_file_id, scope.row.order_no, scope.row.repair_file_name)" >{{ scope.row.repair_file_name }}</el-link> <el-link v-else-if="scope.row.file_id" :key="scope.row.file_id" style="display: block;color: #67C23A" icon="el-icon-paperclip" @click="downloadOrderExcel(scope.row.file_id, scope.row.order_no, scope.row.file_name)" >{{ scope.row.file_name }}</el-link> </el-tooltip> <div v-else> <el-link v-if="scope.row.repair_file_id" :key="scope.row.repair_file_id" style="display: block;" icon="el-icon-paperclip" @click="downloadOrderExcel(scope.row.repair_file_id, scope.row.order_no, scope.row.repair_file_name)" >{{ scope.row.repair_file_name }}</el-link> <el-link v-else-if="scope.row.file_id" :key="scope.row.file_id" style="display: block;" icon="el-icon-paperclip" @click="downloadOrderExcel(scope.row.file_id, scope.row.order_no, scope.row.file_name)" >{{ scope.row.file_name }}</el-link> </div> </div> ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值