3

Can someone show me how to remove an object in the newly created array? parameter "index" does not work well in this case as it is not really the index of the newly created array.

I am creating the new array in the onchange event.

Here I am including the full code. Just in case someone has got an idea.

import * as React from 'react';
import styles from './ListItems.module.scss';
import { IListItemsProps } from './IListItemsProps';
import { escape } from '@microsoft/sp-lodash-subset';
import  {DropdownBasicExample} from './DropdownExample'
import { IDropdownOption, DropdownMenuItemType } from 'office-ui-fabric-react';
import { DropdownBasicExample2 } from './Dropdown2.Basic.Example';

export interface IListItemsState{
  selectedItems:IDropdownOption[];
  selectedSite:IDropdownOption;
}

export default class ListItems extends React.Component<IListItemsProps, IListItemsState> {
  private myWeb:IDropdownOption;

  constructor(props){
    super(props);
      this.state = {
        selectedItems:[],
        selectedSite:null
      }
  }

  private sites = [
    { key: 'Header', text: 'Actions', itemType: DropdownMenuItemType.Header },
    { key: 'A', text: 'Site a', title: 'I am Site a.' },
    { key: 'B', text: 'Site b' },
    { key: 'C', text: 'Site c' },
    { key: 'D', text: 'Site d' },
    { key: 'E', text: 'Site e' },
     { key: 'F', text: 'Site f' },
    { key: 'G', text: 'Site g' },
    { key: 'H', text: 'Site h' },
    { key: 'I', text: 'Site i' },
    { key: 'J', text: 'Site j' }
  ];

  private loadSites= (): Promise<IDropdownOption[]> => {
    return new Promise<IDropdownOption[]>((resolve: (sites: IDropdownOption[]) => void, reject: (error: any) => void) => {
      setTimeout(() => {
        resolve(this.sites);
      }, 1000);
    });
  }

  private onChanged = (item: IDropdownOption, index?: number): void => {
    let mySelectedItems = [...this.state.selectedItems];

  if (item.selected) {
    mySelectedItems.push(item);
  } else {
      mySelectedItems = mySelectedItems.filter(selectedItem => selectedItem !== item);
  }

  this.setState({
    selectedItems: mySelectedItems
  });


    console.log(mySelectedItems);


  }


  public render(): React.ReactElement<IListItemsProps> {
    const {selectedSite} = this.state;
    return (
      <div className={styles.listItems}>


      <DropdownBasicExample loadOptions={this.loadSites} onChanged={this.onChanged}   />       

              <div id="showItems"></div>
              <ul>{
                this.state.selectedItems.map((site:IDropdownOption)=> {
                return <li>{site.text}</li>
              })
              }

              </ul>
              <div>selected Site {
              selectedSite ? selectedSite.key: "is empty"                
                }</div>
      </div>
    );
  }

}

DropdownBasicExample2

import * as React from 'react';
import { PrimaryButton } from 'office-ui-fabric-react/lib/Button';
import { Dropdown, IDropdown, DropdownMenuItemType, IDropdownOption } from 'office-ui-fabric-react/lib/Dropdown';

import './Dropdown.Basic.Example.scss';
import { BaseComponent, createRef, IBaseProps } from 'office-ui-fabric-react';

export interface IDropdownBasicExample2State{
    selectedItem?:  IDropdownOption;
    selectedItems: IDropdownOption[];
    options: IDropdownOption[];
}


export interface IDropdownBasicExample2Props  extends IBaseProps{
  onChanged?: (option: IDropdownOption, index?: number) => void;
  Options: IDropdownOption[];
}
export class DropdownBasicExample2 extends BaseComponent<IDropdownBasicExample2Props,IDropdownBasicExample2State> {
  private _basicDropdown = createRef<IDropdown>();
  private alphas:IDropdownOption[]; 
  private array2: Array<IDropdownOption>;
  constructor(props: IDropdownBasicExample2Props) {
    super(props);
    this.state = {
      selectedItem: null,
      selectedItems: [],
      options:[]
    };
  }

componentDidMount(){

}

  public render() {
    const { selectedItem, selectedItems } = this.state;

    return (
      <div className="docs-DropdownExample">


        <Dropdown
          placeHolder="Select options"
          label="Multi-Select controlled example:"
          selectedKey={selectedItem ? selectedItem.key : undefined}
          key={selectedItem ? selectedItem.key : undefined}
          onChanged={this.onChangeMultiSelect}
          multiSelect
          options={this.props.Options}
        />

      </div>
    );
  }

  public onChangeMultiSelect = (item: IDropdownOption,index:number): void => {

    this.props.onChanged(item,index);
  };

}

2 Answers 2

2

You could filter out the item from the array.

private onChanged = (item: IDropdownOption, index?: number): void => {
  let mySelectedItems = [...this.state.selectedItems];

  if (item.selected) {
    mySelectedItems.push(item);
  } else {
    mySelectedItems = mySelectedItems.filter(
      selectedItem => selectedItem !== item
    );
  }

  this.setState({
    selectedItems: mySelectedItems
  });
};
Sign up to request clarification or add additional context in comments.

6 Comments

I used your code, but it does not remove the items from the newly created array. they are still in the array.
@BurreIfort That's frustrating. Could you include your state and how you call onChanged in your code? It will be hard to say what could be wrong otherwise.
I will include above my entire code. Perhaps that will be easier.
@BurreIfort Try to include a Minimal, Complete, and Verifiable example.
I have included it. it is just an example. However, using the index parameter I am able to remove them, but the only thing is that "index" returns the index of the original array and is not zero based. So, I was trying to get the index of the object in the array. It is very confusing in React.
|
1

Give this a try. Remove items based on a property. In this case I am using the key.

 private onChanged = (item: IDropdownOption, index?: number): void => {
    let mySelectedItems = [...this.state.selectedItems];

  if (item.selected) {
    mySelectedItems.push(item);
  } else {
      mySelectedItems = mySelectedItems.filter(selectedItem => selectedItem.key !== item.key);
  }

  this.setState({
    selectedItems: mySelectedItems
  });

  }

Comments

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.