4

let's we have a data variable and i want to use this data variable for my style.

data() 
{
 return{
        selected:{
         index: 2
        }
     }
}
<style>
.parent-table >>> .table-row:nth-child(/* here i want to pass selected.index */) {
   background: red;
}
</style>


My use case is that i have a table component used in my landing page . I want to change background of selected row of table from my landing page.

6
  • 1
    I think you should make a method that applies a css property with the index with this.selected.index. You have various class name .parent-table and .table-row, I don't realy understand, but you can apply css property with the css selector :nth-child(index) Commented Sep 9, 2022 at 18:08
  • 1
    @RyukShi actually, .table-row is the class of the table component's row and I want to override their nth-child(index) Commented Sep 9, 2022 at 18:14
  • 1
    ok ok I had this idea, that might look like this, in the method : document.querySelector('css_selector').style.backgroundColor('red'); Commented Sep 9, 2022 at 19:26
  • 1
    @RyukShi I have already tried this one but it's not working. Commented Sep 10, 2022 at 2:50
  • 1
    @NileshMishra Can you please confirm, are you creating HTML table or vuetify v-data-table ? Also, do you want to change the row color on click on the row ? Commented Sep 14, 2022 at 13:53

3 Answers 3

6
+50

I don't think we have any solution as per your requirement in Vue 2. In Vue 3.2, They introduced this feature which enables component state-driven dynamic CSS values in <style> tags. You can read that out here.

After understand your exact requirement and post spending few hours on this requirement, Here I am coming up with the solution in JavaScript way as we can't use dynamic variables in CSS nth-child selectors. You can have a look in this SO post : Is it possible to use CSS vars in CSS3 selectors?

Looks like you have to do update the style of nth-child in pure JS way.

Working Demo :

new Vue({
  el: '#app',
  data: {
    selected: {
      index: 2
    }
  },
  mounted() {
    let listSelector = document.querySelector(`li:nth-child(${this.selected.index})`);
    listSelector.style.backgroundColor = 'lightGreen';
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
  <ul>
    <li>First list item</li>
    <li>Second list item</li>
    <li>Third list item</li>
    <li>Fourth list item</li>
    <li>Fifth list item</li>
  </ul>
</div>

Sign up to request clarification or add additional context in comments.

7 Comments

I can't change the component I have to do something in the parent vue file without disturbing Table.vue file.
@NileshMishra I understood your requirement now. I don't think we can pass vue variables into a <styles> tag as it is static. But we can find some way to achieve this by using CSS variables .. I need to spend some more time on this requirement you have. Will hopefully update you by tomorrow EOD.
@NileshMishra I updated the answer as per your requirement. Hope it will work as per your expectation.
I have already tried this one,but it's not working.
But it's working in the above code snippet. So it should work locally as well .. did you tried with /deep/ css selector ?
|
5

I don't know how to explain it. but here's how to pass variables to style scope

PROPS:

props: {
    bgColor: {
       type: String,
       default: "#ff0000" //RED
   }
 }, 

COMPUTED (variables that can be used as arguments):

computed: {
    tableRowColor() {
      return {
        '--bg-color': this.bgColor,
        '--index': this.selected.index //from your data
      }
   }
}

Example of accessing the variables inside style scoped:

<style scoped>

     table, td, th {
        border: 1px solid;
     }

     table {
        width: 100%;
        background: var(--bg-color); /*here is how to access the variable */
    }
</style>

note: You don't need to make the props if you only want to get the index from your data

6 Comments

I have to pass dynamic parameter in nth-child()
This is indeed the way to go in Vue2.
@kissu This is not what author wants. By this way we can pass the dynamic variables as a value but author wants to access dynamic variable in the selector which is not possible directly by using Vue 2.
@RohìtJíndal for me, it achieves the same goal even if not exactly written the way OP expected. But yeah, maybe it's different.
@kissu It will not achieve the same goal my friend. where that --index variable is using in his answer ? Can you pass dynamic values as a selector ? You can not pass dynamic value in :nth-child directly just using CSS. I did lot of research and then at the end I came up with this solution.
|
2

this is how you pass a data property to a css property.

<script>
    export default {
        data: () => ({
            color: 'red',
        }),
    };
</script>
<style scoped>
.card-text-color {
    color: v-bind(color)
}

1 Comment

This is not available in Vue2.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.