简体   繁体   English

如何存储/访问表(el-table)中的特定行数据并在单击该行中的链接时在对话框中显示(不是警报)?

[英]How to store/access the specific row data in a table (el-table) and display that in an dialog box (not alert) when clicked on a link in that row?

I have created a table using el-table (elements - ui) in Vue.js. I have multiple columns in that table (id, username, total_number).我在 Vue.js 中使用 el-table (elements - ui) 创建了一个表。我在该表中有多个列(id、用户名、total_number)。 The column total_number consists of buttons. total_number 列由按钮组成。 When I click on the button, it should open up a dilaog box, and call a function called fetchData (in javascript) which sends the row object. The code that I have written is giving me some issues accessing that specific row data.当我单击该按钮时,它应该打开一个对话框,并调用一个名为 fetchData 的 function(在 javascript 中),它发送行 object。我编写的代码在访问该特定行数据时出现了一些问题。

                <el-table-column  prop="count"
                   label="Total">

                   <template slot-scope="scope">
                      <el-button type="text" @click="dialogVisible = true">{{scope.row.count}}
                      </el-button>

                      <el-dialog
                        :visible.sync="dialogVisible"
                        :before-close="handleClose">
                           {{fetchData(scope.row)}}
                      </el-dialog>
                   </template>
                </el-table-column>

javascript code javascript 代码

fetchData(row){
   console.log(row.id +" "+row.username);
}

If I clicked on the button in the 1st row, and made a call to fetchData, I expect it to pass the object of the first row for scope.row is passed to the function fetchData.如果我单击第一行的按钮,并调用 fetchData,我希望它传递第一行的 object 为 scope.row 传递给 function fetchData。 And so on for any row clicked.对于单击的任何行,依此类推。 Instead, it somehow ends up printing the values (id and username) in the last row in the table.相反,它最终以某种方式在表的最后一行打印值(id 和用户名)。

There are total 7 rows in the table and 3 columns.表中共有 7 行 3 列。 The 3rd column has buttons in it.第三列中有按钮。 When I click on the button in the 1st row of the 3rd column, I want to call the function fetchData which passes the 1st row.当我点击第 3 列第 1 行的按钮时,我想调用传递第 1 行的 function fetchData。 That is I want to pass the row object of the 1st row.那就是我想传递第一行的 object 行。 What actually happens in the above code is that it sends the last (7th) row's object (scope.row).上面代码中实际发生的是它发送最后(第 7)行的 object(scope.row)。 And I want to call the fetchData function and pass the current row object when the dialog box opens up.我想调用 fetchData function 并在对话框打开时传递当前行 object。 What changes should I make in the above code to make in order access the current row when the button in that specific row is clicked?我应该在上面的代码中进行哪些更改才能在单击特定行中的按钮时访问当前行?

I would really appreciate if someone pointed out my mistake here.如果有人在这里指出我的错误,我将不胜感激。

Thank you.谢谢你。

 var Main = { data() { return { dialogVisible: false, rowData: null, tableData: [{ id: 1, username: 'Tom', count: 10 }, { id: 2, username: 'Jack', count: 20 }, { id: 3, username: 'Leo', count: 30 }] } }, methods: { showDialogHandle(row) { this.rowData = Object.assign({}, row) this.dialogVisible = true }, handleCloseDialog() { this.rowData = null this.dialogVisible = false } } } var Ctor = Vue.extend(Main) new Ctor().$mount('#app')
 @import url("//unpkg.com/element-ui@2.12.0/lib/theme-chalk/index.css");
 <script src="//unpkg.com/vue/dist/vue.min.js"></script> <script src="//unpkg.com/element-ui@2.12.0/lib/index.js"></script> <div id="app"> <el-table:data="tableData" style="width: 100%"> <el-table-column prop="id" label="ID" min-width="180"></el-table-column> <el-table-column prop="username" label="Name" min-width="180"></el-table-column> <el-table-column prop="count" label="Total" min-width="180"> <template slot-scope="scope"> <el-button type="text" @click="showDialogHandle(scope.row)">{{scope.row.count}} </el-button> </template> </el-table-column> </el-table> <el-dialog:visible.sync="dialogVisible":before-close="handleCloseDialog"> {{rowData}} </el-dialog> </div>

Each time the row data is clicked, store the row data in a variable and then displayed in the dialog.每次单击行数据时,将行数据存储在变量中,然后显示在对话框中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM