如何在Quasar中以编程方式正确选择Qtable中的行



我想在Qtable中只选择一行关键字字段(而不是整行数据(到目前为止,我可以在使用行键时切换复选框this.selected=[{"name":"Eclair"}]但我预计整个行数据会填充到所选对象中,但事实并非如此。转到下面的代码笔,首先尝试单击复选框,并注意屏幕底部的字符串数据

然后

单击按钮,注意字符串化的数据并不是整行。代码笔https://codepen.io/jniumata/pen/YzVWoQO

因此,我的问题是,如何通过编程只使用行键字段选择一行,并将整行数据放入数组中。(注意:我不想传递整个对象,只需要传递行键(

new Vue({
el: '#q-app',
methods: {
onChangeSelect (details) {
console.log('details: ', details)
// parse details and make a request.... 
},
testMethod () {
if(this.selected.length > 0) {
this.selected = [];
} else {
this.selected = [{"name": "Eclair"}];  
}

}
},
data () {
return {
selected: [],
columns: [
{
name: 'desc',
required: true,
label: 'Dessert (100g serving)',
align: 'left',
field: row => row.name,
format: val => `${val}`,
sortable: true
},
{ name: 'calories', align: 'center', label: 'Calories', field: 'calories', sortable: true },
{ name: 'fat', label: 'Fat (g)', field: 'fat', sortable: true },
{ name: 'carbs', label: 'Carbs (g)', field: 'carbs' },
{ name: 'protein', label: 'Protein (g)', field: 'protein' },
{ name: 'sodium', label: 'Sodium (mg)', field: 'sodium' },
{ name: 'calcium', label: 'Calcium (%)', field: 'calcium', sortable: true, sort: (a, b) => parseInt(a, 10) - parseInt(b, 10) },
{ name: 'iron', label: 'Iron (%)', field: 'iron', sortable: true, sort: (a, b) => parseInt(a, 10) - parseInt(b, 10) }
],
data: [
{
name: 'Frozen Yogurt',
calories: 159,
fat: 6.0,
carbs: 24,
protein: 4.0,
sodium: 87,
calcium: '14%',
iron: '1%'
},
{
name: 'Ice cream sandwich',
calories: 237,
fat: 9.0,
carbs: 37,
protein: 4.3,
sodium: 129,
calcium: '8%',
iron: '1%'
},
{
name: 'Eclair',
calories: 262,
fat: 16.0,
carbs: 23,
protein: 6.0,
sodium: 337,
calcium: '6%',
iron: '7%'
},
{
name: 'Cupcake',
calories: 305,
fat: 3.7,
carbs: 67,
protein: 4.3,
sodium: 413,
calcium: '3%',
iron: '8%'
},
{
name: 'Gingerbread',
calories: 356,
fat: 16.0,
carbs: 49,
protein: 3.9,
sodium: 327,
calcium: '7%',
iron: '16%'
},
{
name: 'Jelly bean',
calories: 375,
fat: 0.0,
carbs: 94,
protein: 0.0,
sodium: 50,
calcium: '0%',
iron: '0%'
},
{
name: 'Lollipop',
calories: 392,
fat: 0.2,
carbs: 98,
protein: 0,
sodium: 38,
calcium: '0%',
iron: '2%'
},
{
name: 'Honeycomb',
calories: 408,
fat: 3.2,
carbs: 87,
protein: 6.5,
sodium: 562,
calcium: '0%',
iron: '45%'
},
{
name: 'Donut',
calories: 452,
fat: 25.0,
carbs: 51,
protein: 4.9,
sodium: 326,
calcium: '2%',
iron: '22%'
},
{
name: 'KitKat',
calories: 518,
fat: 26.0,
carbs: 65,
protein: 7,
sodium: 54,
calcium: '12%',
iron: '6%'
}
]
}
}
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.4.1/vue.js"></script>
<link href="https://cdn.jsdelivr.net/npm/quasar@1.14.3/dist/quasar.min.css" rel="stylesheet"/>
<script src="https://cdn.jsdelivr.net/npm/quasar@1.14.3/dist/quasar.umd.min.js"></script>

<!--
Forked from:
https://quasar.dev/vue-components/table#Example--Single-selection
-->
<div id="q-app">
<div class="q-pa-md">
<q-table
title="Programmatically select row (click button below)"
:data="data"
:columns="columns"
row-key="name"
selection="single"
:selected.sync="selected"
@selection="(details) => onChangeSelect(details)"
></q-table>
<div class="row justify-start">

<q-btn  label="I like eclairs" @click="testMethod" color="primary" class="q-mt-sm" />
</div>
<div class="q-mt-md">
Selected: {{ JSON.stringify(selected) }}
</div>
</div>
</div>

有点晚了,但你可以试试这个

this.selected = this.data.filter(row => row.name == "Eclair")

相关内容

  • 没有找到相关文章

最新更新