【问题标题】:How to target specific vue table element?如何定位特定的 vue 表元素?
【发布时间】:2019-11-15 12:15:51
【问题描述】:

我正在设置一个从数据库中获取并显示订单列表的列表。 onclick 的按钮,我想仅在单击它的行中显示星级 div。如何使 vue 按钮仅在其所在的行中触发 div 的外观。

我尝试使用v-on:click= 函数在 js 中调用另一个函数,但似乎没有用。这里两个模板都包含在

    <div id="wrapper">
         <v-toolbar flat color="white">
              <v-toolbar-title style="font-family: 'Raleway', sans-serif;">Order 
               History</v-toolbar-title>
               <v-spacer></v-spacer>
               <d style="margin-right:20px;color:#a7a7a7;font-family: 'Raleway', 
               sans-serif;">click on an order for more information</d>
                         </v-toolbar>
                         <v-data-table
                            :headers="headers"
                            :items="orders"
                            :expand="expand"
                            item-key="number"
                            >
                            <template v-slot:items="props">
                               <tr @click="props.expanded = !props.expanded">
<!--trigger using this button-->   <td class="text-xs-left" style='font- 
                                    weight:400;'><v-btn v-on:click="greet" 
                                   class="dark">{{ props.item.review }}</v-btn> 
                                   </td>
                               </tr>
                            </template>
                            <template v-slot:expand="props" style="font- 
                               weight:500">
                               <v-data-table
                                  :items="[props.item]"
                                  :expand="expand"
                                  item-key="name"
                                  hide-actions
                                  >
                                  <template v-slot:headers="props">
                                  </template>
                                  <template v-slot:items="props">
                                     <td width="15%" class="text-xs-left" 
                                      style='font-weight:300;'>
<!--trigger this div  -->                    <div class="star-rating"></div>
                                     </td>
                                  </template>
                               </v-data-table>
                            </template>
                         </v-data-table>
                      </div>

//JS

var v = new Vue({
  el: '#app',
     methods: {
         greet: function () {
               $(".stars-rating").hide();
          }
      },
  data() {
    return {
      expand: true,
      headers: [
      {
        text: 'Order Number',
        align: 'left',
        sortable: false,
        value: 'number',
        width: '10%' },

      { text: 'Name/Message', value: 'name',
        align: 'left',
        width: '15%' },
      { text: 'Date/Start', value: 'date',
        align: 'left',
        width: '10%' },
      { text: 'Time/End', value: 'time',
        align: 'left',
        width: '10%' },
      { text: 'Provider/Receipts', value: 'provider',
        align: 'left',
        width: '10%' },
      { text: 'Amount/Labor', value: 'amount',
        align: 'left',
        width: '15%' },
      { text: 'Status/Tracking', value: 'status',
        align: 'left',
        width: '15%' },
      { text: 'Confirmation/Review', value: 'review',
        align: 'left',
        width: '15%' }],

      orders: [],

    };
  } });

【问题讨论】:

    标签: vue.js vue-component


    【解决方案1】:

    首先,您应该从&lt;tr @click="props.expanded = !props.expanded"&gt; 元素中删除@click

    在您的 greet 函数中,您必须更改 expand 标志,例如:

    greet: function () {
    	this.expand = false
    }

    由于您已经在使用 v-data-table expand 道具竞标 expand 状态,它应该可以工作

    <v-data-table
        :headers="headers"
        :items="orders"
        :expand="expand"
        item-key="number"
    >

    【讨论】:

    • 点击这些行以显示更多信息时已经展开。问题在于只有在单击该行中的按钮后才显示星星,而不是在单击该行本身之后,就像现在一样。
    • 如果您在 codepen 或小提琴中创建问题的重现,我将能够提供帮助,仅基于您的代码是困难的
    • 我正在尝试在按下“标记已完成”按钮以完成订单时显示评论星。 link
    【解决方案2】:

    你试过了吗

    <div class="star-rating" v-if="props.expanded"></div> 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-06
      • 1970-01-01
      • 2021-12-09
      • 2021-02-25
      相关资源
      最近更新 更多