【问题标题】:Change css class on @click event of a table responsive table在表响应表的@click事件上更改css类
【发布时间】:2021-05-18 18:46:46
【问题描述】:

当我单击tr 时,我希望更改表格中一行的背景颜色。

这是表格的代码:

<div class="table-responsive">
        <table class="table table-hover">
            <thead>
                <tr>
                    <th><small class="text-muted">#</small></th>
                    <th>Name</th>
                    <th>Heading</th>
                    <th>Productor</th>
                </tr>
            </thead>
            <tbody>
                <tr
                    v-for="company in allCompanies"
                    :key="company.id"
                    role="button"
                    @click="selectCompany(company)"
                >
                    <td class="text-primary">
                        <small>#{{ company.id }}</small>
                    </td>
                    <td class="fw-bold">{{ company.name }}</td>
                    <td>{{ company.heading }}</td>
                    <td>
                        {{ company.productor }}
                    </td>
                </tr>
            </tbody>
        </table>
    </div>

这里是我在点击事件上调用的方法的代码:

 selectCompany(company) {
        this.storeSelectedCompamy(company);
        this.fetchShows(company.id);
        console.log("change css");
    }

任何帮助将不胜感激。 提前谢谢你

瓦莱里奥

【问题讨论】:

    标签: css vue.js bootstrap-vue


    【解决方案1】:

    看看这里:

    https://vuejs.org/v2/guide/class-and-style.html

    看起来每个&lt;tr&gt; 对应一个company,所以我的方法是将每一行的状态作为属性附加到其对应的company,然后在selectCompany 中切换该属性。比如:

    selectCompany(company) {
      allCompanies.forEach(_company => _company.isSelected = false);
      company.isSelected = true;
    }
    
    <tr
      v-for="company in allCompanies"
      :key="company.id"
      :class="company.isSelected ? 'selected' : ''"
      role="button"
      @click="selectCompany(company)"
    >
    

    【讨论】:

    • 我喜欢你的解决方案,但我发现 2 个问题:第一个,我失去了 :hover 颜色,第二个似乎不起作用..也许我犯了一些错误
    • 丢失:hover 颜色是由于style 属性覆盖了您的CSS。使用一个类应该可以解决这个问题。我已经更新了我的代码。至于它不起作用,可能有多种原因。你能提供一个代码 sn-p 来使用吗?
    • 抱歉,我的英语很差……你说的“code sn-p”到底是什么意思?如果粘贴 .vue 文件的完整代码就足够了?感谢您的帮助
    • 不,不是.vue 文件。在 StackOverflow 上,“sn-p”是 StackOverflow 将运行的一段代码。它使回答问题变得更加容易。这里有更多信息:stackoverflow.blog/2014/09/16/…
    猜你喜欢
    • 2021-10-14
    • 2013-08-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-19
    • 2010-12-29
    • 1970-01-01
    相关资源
    最近更新 更多