Vue ElementUI在el-table中使用el-popover,点击嵌套内容中的确定或取消来关闭el-popover。

阅读 (1)
官方的嵌套样例虽然可以正常关闭popover, 但如果popover在列表循环中,那对于el-popover 的 v-model 绑定的作用域什么出现问题,无法绑定

官方示例: 

<el-popover
  placement="top"
  width="160"
  v-model="visible">
  <p>这是一段内容这是一段内容确定删除吗?</p>
  <div style="text-align: right; margin: 0">
    <el-button size="mini" type="text" @click="visible = false">取消</el-button>
    <el-button type="primary" size="mini" @click="visible = false">确定</el-button>
  </div>
  <el-button slot="reference">删除</el-button>
</el-popover>

<script>
  export default {
    data() {
      return {
        visible: false,
      };
    }
  }
</script>

如果在el-table的循环中:

<el-table-column width="100" align="center" label="操作">
  <template scope="item">
    <el-popover
     placement="top"
     width="160"
     v-model="visible">
     <p>这是一段内容这是一段内容确定删除吗?</p>
      <div style="text-align: right; margin: 0">
       <el-button size="mini" type="text" @click="visible = false">取消</el-button>
       <el-button type="primary" size="mini" @click="visible = false">确定</el-button>
      </div>
      <el-button slot="reference">删除</el-button>
    </el-popover>
  </template>
</el-table-column>

即便定义了visible,也是无法正常工作的

完美解决方案:

<el-table-column width="100" align="center" label="操作">
  <template slot-scope="scope">
    <el-popover width="160" :ref="`popover-${scope.$index}`">
      <p>确定删除该项吗?</p>
      <div style="text-align: right; margin: 0">
        <el-button type="text" size="mini" @click="handleClose(scope.$index)">
          取消
        </el-button>
        <el-button type="danger" size="mini" >确定</el-button>
      </div>
      <el-button type="text" slot="reference">删除</el-button>
    </el-popover>
  </template>
</el-table-column>

<script>
 ...
   handleClose(index) {
     this.$refs[`popover-${index}`].doClose()
   }
 ...
</script>

或者:

<el-table-column width="100" align="center" label="操作">
  <template slot-scope="scope">
    <el-popover width="160" :ref="`popover-${scope.$index}`">
      <p>确定删除该项吗?</p>
      <div style="text-align: right; margin: 0">
        <el-button type="text" size="mini" @click="scope._self.$refs[`popover-${scope.$index}`].doClose()">
          取消
        </el-button>
        <el-button type="danger" size="mini" >确定</el-button>
      </div>
      <el-button type="text" slot="reference">删除</el-button>
    </el-popover>
  </template>
</el-table-column>

还有一种ref的处理方式,当逻辑上没有上面这种方法严谨:

<div ref="closepopover">
<el-table-column  label="操作">
 <template slot-scope="item">
  <el-popover
    placement="right"
    title="确定删除?"
    width="200"
    v-model="item.row.visible"
    trigger="click">
    <div style="text-align: right; margin: 0">
      <el-button size="mini" type="text" @click="updateVisible()">取消</el-button>
      <el-button type="primary" size="mini" @click="updateVisible()">确定</el-button>
    </div>
    <el-button slot="reference">删除</el-button>
  </el-popover>
</template >
</el-table-column>


//也可解决
updateVisible(){
    this.$refs.closepopover.click();
}

 

更新于:2020-01-02 15:29:28

留言

发表评论

标记为*的字段是必填项(邮箱地址不会被公开), 由于国内舆论控制, 评论需要管理员审核通过方可展示

返回顶部