【问题标题】:Change color of <td> independetly in react js在反应js中独立改变<td>的颜色
【发布时间】:2019-05-16 14:36:17
【问题描述】:

我在一张桌子上有三个&lt;td&gt;。我想独立更改&lt;td&gt; 的颜色。 onClick ,弹出一个 Modal ,您可以选择颜色。现在,当我将颜色设置为状态时,所有&lt;td&gt; 都会更改它们的背景颜色。我想独立更改每个&lt;td&gt; 的颜色。所以一个&lt;td&gt; 可能是红色,另一个&lt;td&gt; 是绿色,另一个&lt;td&gt; 是黄色。

 state = { visible: false, color: "", text: ""  };
     showModal = () => {
    this.setState({
      visible: true
    });
  };
     boxes1 = (value, text) => {
        console.log("dssdf", value);
        this.setState(
          {
            color: value,
            text: text
          },
          () => console.log("this is wt", this.state.color)
        );
      };

      boxes2 = (value, text) => {
        console.log("vf", value);
        this.setState(
          {
            color: value,
            text: text
          },
          () => console.log("this is wt", this.state.color)
        );
      };

      boxes3 = (value, text) => {
        console.log("sds", value);
        this.setState(
          {
            color: value,
            text: text
          },
          () => console.log("this is wt", this.state.color)
        );
      };

     render() {
        const yellow = "yellow";
        const blue = "blue";
        const reenter code hered = "red";
        const text = "colors";

        let s = [1, 2, 3];

        let d = s.map(tables => (
          <div>
            <table
              style={{
                border: "1px solid black",
                width: "70px",
                background: `${this.state.color}`
              }}
            >
              <thead>
                <tr>
                  <td onClick={this.showModal}>{this.state.text}Change 
                   colors
                  </td>
                </tr>
              </thead>
            </table>
          </div>
        ));
     return (
          <div>
            {d}
            <Modal
              title="Basic Modal"
              visible={this.state.visible}
              onOk={this.handleOk}
              onCancel={this.handleCancel}
            >
              <div className="boxes" onClick={()=>this.boxes1(yellow,text)}>
               YELLOW
              </div>
              <div className="boxes" onClick={() => this.boxes2(red,text)}>
               RED
              </div>
              <div className="boxes" onClick={() => this.boxes3(blue,text)}>
               BLUE
              </div>
            </Modal>
          </div>
        );
      }
    }

预期:当单击单个 &lt;td&gt; 时,背景颜色应仅针对该 &lt;td&gt; 更新进行更新。

实际:点击后,所有&lt;td&gt;的背景颜色都会改变

【问题讨论】:

  • 我可以在代码中看到单个 td 但点击是从不同的 div 触发的,所以你想用类名框或单个 td 更改 div 的颜色?
  • 单个 td 被映射 3 次,所以这会创建三个 td [boxes],我想要做的是,单击单个 td,我只需要更改该 td 的背景颜色.
  • 一个table里面有3个td应该是:每三个table里面都有td

标签: javascript arrays reactjs


【解决方案1】:

我们必须有多个项目来表示多个 div 元素,否则它将像您一样失败,即更改所有 div 颜色。
以下是代码:

state = { box1: {visible: false, color: "", text: ""},
box2: {visible: false, color: "", text: ""},
box3: {visible: false, color: "", text: ""},  };

     showModal = () => {
    this.setState({
      visible: true
    });
  };
   boxesChange = (value, text, id) => {
    const box={
            color: value,
            text: text
          };        
    this.setState(
          `${id}`:box,
          () => console.log("this is wt", this.state.color)
        );
      };

     render() {
        const yellow = "yellow";
        const blue = "blue";
        const reenter code hered = "red";
        const text = "colors";

        let s = [1, 2, 3];

        let d = s.map(tables => (
          <div>
            <table
              style={{
                border: "1px solid black",
                width: "70px",
                background: `${this.state.color}`
              }}
            >
              <thead>
                <tr>
                  <td onClick={this.showModal}>{this.state.text}Change 
                   colors
                  </td>
                </tr>
              </thead>
            </table>
          </div>
        ));
     return (
          <div>
            {d}
            <Modal
              title="Basic Modal"
              visible={this.state.visible}
              onOk={this.handleOk}
              onCancel={this.handleCancel}
            >
              <div id="box1" className="boxes" onClick={()=>this.boxes1(yellow,text,id)}>
               YELLOW
              </div>
              <div id="box2" className="boxes" onClick={() => this.boxes2(red,text,id)}>
               RED
              </div>
              <div id="box3" className="boxes" onClick={() => this.boxes3(blue,text,id)}>
               BLUE
              </div>
            </Modal>
          </div>
        );
      }
    }

【讨论】:

  • 谢谢@imrajshah。所以我需要多个项目来更改不同的元素。这产生了另一个问题。我需要创建大约 80 个这样的 td,这会使状态组件变得巨大。所以我认为我们必须遍历状态。对吗?
  • 所以你可以在状态中创建盒子数组,然后在其中添加你所有的 。它会解决你的问题
  • 这里出现错误:this.setState(${id}:box, () => console.log("this is wt", this.state.color) ); }; setState 中的 id 属性给了我一个错误
【解决方案2】:

基于元素索引的动态样式渲染建议:

假设您在下面渲染了多个元素,这只是一个示例,根据单击的元素的索引将动态样式应用于元素,可以更改特定元素的样式:

伪代码:

const myStyleBackgroundColor = {};

allElementArray.map((item,index)=>
<div onClick={(e)=>onClickHandle(index)} style={{backgroundColor : myStyleBackgroundColor[index] ? myStyleBackgroundColor[index] : "#FFF" }}>
This is the div which will change color once clicked based on index
</div>
);

onClickHandle(index){
myStyleBackgroundColor [index] = "color";
// apply rest of index a default color
}

注意:上面的常量也可以是局部状态的变量数组。

【讨论】:

    猜你喜欢
    相关资源
    最近更新 更多
    热门标签