我想将列表中的检查值存储在数组中,但不能给我正确的结果

时间:2018-09-06 08:28:09

标签: javascript arrays jsx

我无法在selectedDepartment数组中存储多个值,因为它会删除旧值并为最新值更新数组。

onClickHandler = (e, DeptId, DeptName) => {
  let selectedDepartment = [];
  selectedDepartment.push(DeptName);
  console.log("selectedDepartment----", selectedDepartment);
  this.setState({
    isChecked: !this.state.isChecked
  });
};

departmentData() {
let departmentList = this.props.departmentData.departments || [];
return departmentList.map((departmentName, index) => {
  return (
    <li className="row" key={index}>
      <div className="col-12 dep-list">
        <div className="row">
          <div className="col-md-9">
            <div
              className="user-name"
              title={departmentName.departmentName}
            >
              {departmentName.departmentName}
            </div>
          </div>
          <div className="col-md-3 mt-rd-btn">
             <RadioButton
                // checked={this.state.isChecked}
                id={departmentName.dObjectId}
                type="radio"
                name={departmentName.departmentName}
                value={departmentName.departmentName}
                onClick={e =>
                  this.onClickHandler(
                    e,
                    departmentName.dObjectId,
                    departmentName.departmentName
                  )
                }
              />
            </div>
          </div>
        </div>
     </li>
    );
  });
}

先谢谢您。帮帮我

2 个答案:

答案 0 :(得分:0)

我还没有完全检查代码,但是您的主要功能onClickHandler遇到了基本的基本问题。

您将在每个函数调用上分配一个新的临时变量,并将所选对象推入该临时变量。因此,从本质上讲,您每次都在创建一个新实例,并将所选值放入该变量中。

您实际需要做的是,获取变量中的旧值,然后推送所选值。 ..

onClickHandler(..., DeptName) {
     let currentList = this.departmentList;
     currentList.push(DeptName);
     this.departmentList = currentList;
}

那应该可以解决您的问题

答案 1 :(得分:0)

@Entity
@Table(name = "game")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
@Document(indexName = "game")
public class Game implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "sequenceGenerator")
    @SequenceGenerator(name = "sequenceGenerator")
    private Long id;

    @Column(name = "name")
    private String name;

    @Column(name = "active")
    private Boolean active;

    @Lob
    @Column(name = "description")
    private String description;

    @Column(name = "cover_image")
    private String coverImage;

    @Column(name = "logo")
    private String logo;

    @Column(name = "background_image")
    private String backgroundImage;

    @Column(name = "featured_image")
    private String featuredImage;

    @Column(name = "character_image")
    private String characterImage;

    @Column(name = "small_cover_image")
    private String smallCoverImage;

    @ManyToMany
    @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
    @JoinTable(name = "game_genres",
               joinColumns = @JoinColumn(name="games_id", referencedColumnName="id"),
               inverseJoinColumns = @JoinColumn(name="genres_id", referencedColumnName="id"))
    private Set<GameGenre> genres = new HashSet<>();