Search code examples
vuejs3v-data-table

Change of background color when checkbox clicked from v-data-table using vue 3


I am looking for a solution where background color of v-data-table rows can be applied whenever the checkbox of the table is toggled.

This is the vue 3 code that I am using for the problem,

<v-data-table
      v-if="makeFilelist"
      :headers="makeHeaders"
      :items="makeFilelist"
      :items-per-page="10"
      :items-per-page-options="items_per_page"
      :key="getCurrentPathNum"
      :single-select="false"
      item-key="name"
      show-select
      v-model="selectedItems"
      @update:options="updateOptions"
      @click:row="OnClick"
      mobile-breakpoint="0"
      items-per-page-text="表示件数:"
      class="elevation-0 filelist"
      :footer-props="footerProps"
      @contextmenu:row="show"
      @pagination="paginate"
      :loading="isGettingFileLists"
      return-object
    >
      <template v-slot:top="{ pagination, options, updateOptions }">
        <v-data-table-footer
          :pagination="pagination"
          :options="options"
          @update:options="updateOptions"
          :items-per-page-text="itemsPerPageText"
          :items-per-page-options="itemsPerPageOptions"
          :show-first-last-page="true"
          id="d-table-header"
        >
        </v-data-table-footer>
      </template>
      <template v-slot:[`item.icon`]="{ item }">
        <td><v-img :src="selectIcon(item)" contain width="50px"></v-img></td>
      </template>
      <template v-slot:[`item.name`]="{ item }">
        <td :style="switchNameStyle(item)">{{ item.name }}</td>
      </template>
      <template v-slot:[`item.fileSizeString`]="{ item }">
        <td :style="switchSizeStyle(item)">{{ item.fileSizeString }}</td>
      </template>
      <template v-slot:[`item.localUpdateTimeString`]="{ item }">
        <td :style="switchSizeStyle(item)">{{ item.localUpdateTimeString }}</td>
      </template>
      <template v-slot:[`item.generation`]="{ item }">
        <td>
          <!-- modified width for displaying generation icon correctly in the filelist -->
          <v-img
            :src="generationIcon(item)"
            contain
            width="40px"
            @click.stop="OnListGenerationClick(item)"
          ></v-img>
        </td>
      </template>
      <template v-slot:[`item.kayoishare`]="{ item }">
        <td>
          <!-- modified width for displaying share icon correctly in the filelist -->
          <v-img
            class="kayoiShareIcon"
            :src="kayoiShareIcon(item)"
            contain
            width="40px"
            @click.stop="OnListKayoiShare(item)"
          ></v-img>
        </td>
      </template>
      <template v-slot:[`item.download`]="{ item }">
        <td>
          <!-- modified width for displaying download icon correctly in the filelist -->
          <v-img
            class="downloadIcon"
            :src="downloadIcon(item)"
            contain
            width="40px"
            @click.stop="OnListDownload(item)"
          ></v-img>
        </td>
      </template>
    </v-data-table>

I need to modify the above code for modifying the background color

I have found out that for vuetify 2 v-data-table, whenever we toggle the checkbox, a new class is being added for the corresponding row. But the same is not being occurred for vuetify 3 v-data-table.


Solution

  • .v-data-table__tr:has(td:first-child input[type='checkbox']:checked) {
       backgroundColor: red // replace with any color you want
    }
    

    playground:

    https://play.vuetifyjs.com/#eNrNlc1q20AQx19l0MUpRLbSD9qqTiANpZTSEkhKoLEJY+3EWrzeFauVHRN8LoWm0PZceuqx79ZH6EqObVnCG+NeIoOlmdnR/Bj9d+f82kt11DpMkuYoIy/02oaGiUBDBx0J0B75DA36BnuCcgfAyB8qRmK/46UkKDLEOt4sEnKbm9pAcZ97c8MfocjIRiQOaR5IYzX2Z+/IHQftVrmYLd9ulVismUaaJ6bgoqtEaQOMLjETBq5nb8yzdx7MLQBNJtNyaduat8ghnHd3l+4C2PqWHihn5VdOHkLj76+br2C7JahRys8voSI0XEm76AzTmMu+UbK6KCbej41dEjT3qrEepnmFoBk8rYZGSmRFeRsMgkrmdMVai/0FXqK0Pwf3qyhDprQD+uF66Cdu6ErmhtCf4LXGhFIH9BuDYuJArlYuM9dCq8xbdvoznFnN6iGJugBK4EdWI7WvUQJ/vJb7kQu7EtyQ+QaOuSS8Q9inMXKBkjmoa2wLanezK/LZEPs7HMWkNXcq5DTTA7pXEvn9B96h7CuXrCXjLnXs1fbbgrl2sqwib9fpH3BiNI57ttu1VpawP5wcuhq9Xh31UKXR2x0g3+CYMIr/YxsGz9czP3MzV3bwhtr4CW/5mDuI39MYPhLesRHrB/LGZ7VD08upOZ09zILWaLcW8zmf1WZSzHCAZnmqX1wYHcaY7hgWXnKdGj+KuWDAZZKZczNJaL8RxRQNeuqq0Q2LR2KLkd7DaNDXKpPMj5RQOrQjnr1YAtwW9aa7MhOi+Ov+AzFmRH4=