简体   繁体   English

从 HTML 到 VUE 的脚本 - 单击时更改背景颜色

[英]Script from HTML to VUE - change bg color on click

Currently I'm able to change color of a div, that from now on I will refer to as 'Cell', in a vanilla html file as follows:目前我可以更改 div 的颜色,从现在开始,我将在 vanilla html 文件中将其称为“单元格”,如下所示:

<script type=text/javascript>

    var type = -1;

    function colorCell() {
        if(type==0){
            event.target.style.backgroundColor = ' #fdcb6e ';
        }else if(type==1){
            event.target.style.backgroundColor = ' #fab1a0 ';
        }else if(type==2){
            event.target.style.backgroundColor = ' #fd79a8 ';
        }else if(type==-1){
            event.target.style.backgroundColor = ' azure ';
        }
    }

    function delay(){
        type=0;
    }
    function panne(){
        type=1;
    }
    function reserved(){
        type=2;
    }
    function clean(){
        type=-1;
    }
</script>

Cell can change to different colors according to the type parameter. Cell 可以根据 type 参数更改为不同的 colors。 Type changes when other buttons (not cells) are clicked.单击其他按钮(不是单元格)时,类型会发生变化。

Any orientation of how to get this done in VUE?关于如何在 VUE 中完成这项工作的任何方向? I can't get it working.我无法让它工作。

EDIT:编辑:

To add information to proposed problem, I need each cell to be colored only when clicked.要向提出的问题添加信息,我需要每个单元格仅在单击时才着色。 Cells are formed in a iterative way as follows:单元格以如下迭代方式形成:

    <div class="body">
        <div v-for="column in 25" :key="column.id" class="column">
          <div v-for="row in 10" :key="row.id" class="frame">
                <span v-if="column == 1">ROW-NAME</span>
                <div v-else class="selector"><b-button @click="colorCell($event)"  /></div>
          </div>
        </div>
    </div>

You could achieve this with a component that looks something like this:您可以使用如下所示的组件来实现此目的:

<template>
  <div>
    <div :style="{ backgroundColor }">Cell</div>
    <button @click="delay">delay</button>
    <button @click="panne">panne</button>
    <button @click="reserved">reserved</button>
    <button @click="clean">clean</button>
  </div>
</template>

<script>
export default {
  data() {
    return {
      type: -1,
    }
  },
  computed: {
    backgroundColor() {
      if (this.type == 0) {
        return '#fdcb6e';
      } else if (this.type == 1) {
        return '#fab1a0';
      } else if (this.type == 2) {
        return '#fd79a8';
      } else if (this.type == -1) {
        return 'azure';
      }
      return '';
    }
  },
  methods: {
    delay() {
      this.type = 0;
    },
    panne() {
      this.type = 1;
    },
    reserved() {
      this.type = 2;
    },
    clean() {
      this.type = -1;
    }
  }
}
</script>

UPDATE:更新:

In order to colour the cells individually you could do it like this (note: you were trying to use column.id and row.id but these properties do not exist - column and row are integers):为了单独为单元格着色,您可以这样做(注意:您尝试使用column.idrow.id但这些属性不存在 - columnrow是整数):

    <div class="body">
        <div v-for="column in 25" :key="column" class="column">
          <div v-for="row in 10" :key="row" class="frame" :ref="`${column}-${row}`">
                <span v-if="column == 1">ROW-NAME</span>
                <div v-else class="selector"><b-button @click="colorCell(column, row)"  /></div>
          </div>
        </div>
    </div>
colorCell(col, row) {
  const el = this.$refs[`${col}-${row}`][0];
  // Update the colour of the individual cell here e.g. el.style.backgroundcolor = '#fdcb6e'
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM