如果有重复的值

发布于 2025-01-31 20:47:11 字数 2240 浏览 0 评论 0原文

  1. 如果分数等于100,我想突出显示得分。

,我可以通过使用(item.score == 100)返回来做到这一点 'style-1';

  1. 如果名称具有相同的名字,我想突出显示名称。

您可以看到在Json甜点中,它们的名称相同,“冷冻” 两次。我想突出两者。

new Vue({
  el: '#app',
  vuetify: new Vuetify(),
  methods: {
    customRowClass(item) {
    
      if (item.score == 100) return 'style-1';
      
      // How to highlight duplicate names? 
      // if (item.name == item.name) return 'style-2';
    },
  },
  data () {
    return  {
      headers: [],
    desserts: [
      {
        name: "Frozen",
        score: 66,
      },
      {
        name: "Tom",
        score: 100,
      },
      {
        name: "Eclair",
        score: 100,
      },
      {
        name: "Frozen",
        score: 89,
      },
    ]
    }
  },
})
.style-1 {
  background-color: rgb(215, 215, 44) !important;
}
.style-2 {
  background-color: rgb(114, 114, 67) !important;
}
<link rel='stylesheet' href='https://cdn.jsdelivr.net/npm/[email protected]/dist/vuetify.min.css'>
<script src='https://cdn.jsdelivr.net/npm/[email protected]/dist/vue.js'></script>
<script src='https://cdn.jsdelivr.net/npm/[email protected]/dist/vuetify.min.js'></script>



<div id="app">
    <v-data-table
      :headers="headers"
      :items="desserts"
      :item-class="customRowClass"
    >
      <template #item="{ item }">
        <tr :class="customRowClass(item)">
          <td>{{ item.name }}</td>
          <td>{{ item.score }}</td>
        </tr>
      </template>

    </v-data-table>
</div>

  1. I would like to highlight the score if it is equal to 100.

By this I can do this by using if (item.score == 100) return
'style-1';

  1. I would like to highlight the names if they have the same name.

You can see that in json desserts, they have the same name "Frozen"
twice. I want to highlight both of them.

new Vue({
  el: '#app',
  vuetify: new Vuetify(),
  methods: {
    customRowClass(item) {
    
      if (item.score == 100) return 'style-1';
      
      // How to highlight duplicate names? 
      // if (item.name == item.name) return 'style-2';
    },
  },
  data () {
    return  {
      headers: [],
    desserts: [
      {
        name: "Frozen",
        score: 66,
      },
      {
        name: "Tom",
        score: 100,
      },
      {
        name: "Eclair",
        score: 100,
      },
      {
        name: "Frozen",
        score: 89,
      },
    ]
    }
  },
})
.style-1 {
  background-color: rgb(215, 215, 44) !important;
}
.style-2 {
  background-color: rgb(114, 114, 67) !important;
}
<link rel='stylesheet' href='https://cdn.jsdelivr.net/npm/[email protected]/dist/vuetify.min.css'>
<script src='https://cdn.jsdelivr.net/npm/[email protected]/dist/vue.js'></script>
<script src='https://cdn.jsdelivr.net/npm/[email protected]/dist/vuetify.min.js'></script>



<div id="app">
    <v-data-table
      :headers="headers"
      :items="desserts"
      :item-class="customRowClass"
    >
      <template #item="{ item }">
        <tr :class="customRowClass(item)">
          <td>{{ item.name }}</td>
          <td>{{ item.score }}</td>
        </tr>
      </template>

    </v-data-table>
</div>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

寒冷纷飞旳雪 2025-02-07 20:47:11

您可以使用 >功能并检查是否有1个以上的项目检查条件。

this.desserts.filter(x => x.name === item.name).length > 1

最后,您可以订购如果将最重要的Hilight放在功能的顶部

new Vue({
  el: '#app',
  vuetify: new Vuetify(),
  methods: {
    customRowClass(item) {
      if (item.score == 100) return 'style-1';

      if (this.desserts.filter(x => x.name === item.name).length > 1) return "style-2"
    },
  },
  data () {
    return  {
      headers: [],
    desserts: [
      {
        name: "Frozen",
        score: 66,
      },
      {
        name: "Tom",
        score: 100,
      },
      {
        name: "Eclair",
        score: 100,
      },
      {
        name: "Frozen",
        score: 89,
      },
    ]
    }
  },
})
.style-1 {
  background-color: rgb(215, 215, 44) !important;
}
.style-2 {
  background-color: rgb(114, 114, 67) !important;
}
<link rel='stylesheet' href='https://cdn.jsdelivr.net/npm/[email protected]/dist/vuetify.min.css'>
<script src='https://cdn.jsdelivr.net/npm/[email protected]/dist/vue.js'></script>
<script src='https://cdn.jsdelivr.net/npm/[email protected]/dist/vuetify.min.js'></script>



<div id="app">
    <v-data-table
      :headers="headers"
      :items="desserts"
      :item-class="customRowClass"
    >
      <template #item="{ item }">
        <tr :class="customRowClass(item)">
          <td>{{ item.name }}</td>
          <td>{{ item.score }}</td>
        </tr>
      </template>

    </v-data-table>
</div>

You can use the Array#Filter function and check if there is more than 1 items that check the condition.

this.desserts.filter(x => x.name === item.name).length > 1

Finally, you can order your if to put the most important hilight at the top of the function

new Vue({
  el: '#app',
  vuetify: new Vuetify(),
  methods: {
    customRowClass(item) {
      if (item.score == 100) return 'style-1';

      if (this.desserts.filter(x => x.name === item.name).length > 1) return "style-2"
    },
  },
  data () {
    return  {
      headers: [],
    desserts: [
      {
        name: "Frozen",
        score: 66,
      },
      {
        name: "Tom",
        score: 100,
      },
      {
        name: "Eclair",
        score: 100,
      },
      {
        name: "Frozen",
        score: 89,
      },
    ]
    }
  },
})
.style-1 {
  background-color: rgb(215, 215, 44) !important;
}
.style-2 {
  background-color: rgb(114, 114, 67) !important;
}
<link rel='stylesheet' href='https://cdn.jsdelivr.net/npm/[email protected]/dist/vuetify.min.css'>
<script src='https://cdn.jsdelivr.net/npm/[email protected]/dist/vue.js'></script>
<script src='https://cdn.jsdelivr.net/npm/[email protected]/dist/vuetify.min.js'></script>



<div id="app">
    <v-data-table
      :headers="headers"
      :items="desserts"
      :item-class="customRowClass"
    >
      <template #item="{ item }">
        <tr :class="customRowClass(item)">
          <td>{{ item.name }}</td>
          <td>{{ item.score }}</td>
        </tr>
      </template>

    </v-data-table>
</div>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文