-2

任何人都可以发送有关如何在功能组件(https://developer.microsoft.com/en-us/fluentui#/controls/web/detailslist/basic )中实现流畅的 UI详细信息列表以及如何从 API 获取数据到详细信息的代码列表

4

1 回答 1

1

这是一个开始,您需要通过这种方式“重构”这段代码,这是一个非常好的做法:

import * as React from "react";
import { Announced } from "office-ui-fabric-react/lib/Announced";
import {
  TextField,
  ITextFieldStyles
} from "office-ui-fabric-react/lib/TextField";
import {
  DetailsList,
  DetailsListLayoutMode,
  Selection,
  IColumn
} from "office-ui-fabric-react/lib/DetailsList";
import { MarqueeSelection } from "office-ui-fabric-react/lib/MarqueeSelection";
import { Fabric } from "office-ui-fabric-react/lib/Fabric";
import { mergeStyles } from "office-ui-fabric-react/lib/Styling";
import { Text } from "office-ui-fabric-react/lib/Text";

const exampleChildClass = mergeStyles({
  display: "block",
  marginBottom: "10px"
});

const textFieldStyles: Partial<ITextFieldStyles> = {
  root: { maxWidth: "300px" }
};

export interface IDetailsListBasicExampleItem {
  key: number;
  name: string;
  value: number;
}

export interface IDetailsListBasicExampleState {
  items: IDetailsListBasicExampleItem[];
  selectionDetails: string;
}

export const DetailsListBasicExampleFunction: React.FunctionComponent<
  {} | IDetailsListBasicExampleState
> = () => {
  const _allItems: IDetailsListBasicExampleItem[] = [];
  const [selection, setSelection] = React.useState<Selection | undefined>();
  function _getSelectionDetails(): string {
    const selectionCount = selection ? selection.getSelectedCount() : 0;

    switch (selectionCount) {
      case 0:
        return "No items selected";
      case 1:
        return (
          "1 item selected: " +
          (selection.getSelection()[0] as IDetailsListBasicExampleItem).name
        );
      default:
        return `${selectionCount} items selected`;
    }
  }
  const [state, setState] = React.useState({
    items: _allItems,
    selectionDetails: _getSelectionDetails()
  });
  React.useEffect(() => {
    const _selection: Selection = new Selection({
      onSelectionChanged: () =>
        setState((prev) => {
          return { ...prev, selectionDetails: _getSelectionDetails() };
        })
    });
    setSelection(_selection);
    for (let i = 0; i < 200; i++) {
      _allItems.push({
        key: i,
        name: "Item " + i,
        value: i
      });
    }
    setState((prev) => {
      return { ...prev, items: _allItems };
    });
  }, []);
  const _columns: IColumn[] = [
    {
      key: "column1",
      name: "Name",
      fieldName: "name",
      minWidth: 100,
      maxWidth: 200,
      isResizable: true
    },
    {
      key: "column2",
      name: "Value",
      fieldName: "value",
      minWidth: 100,
      maxWidth: 200,
      isResizable: true
    }
  ];

  // Populate with items for demos.

  const _onFilter = (
    ev: React.FormEvent<HTMLInputElement | HTMLTextAreaElement>,
    text: string
  ): void => {
    console.log(text);
    setState((prev) => {
      return {
        ...prev,
        items: text
          ? _allItems.filter((i) => i.name.toLowerCase().indexOf(text) > -1)
          : _allItems
      };
    });
  };

  const _onItemInvoked = (item: IDetailsListBasicExampleItem): void => {
    alert(`Item invoked: ${item.name}`);
  };
  return selection ? (
    <Fabric>
      <div className={exampleChildClass}>{state.selectionDetails}</div>
      <Text>
        Note: While focusing a row, pressing enter or double clicking will
        execute onItemInvoked, which in this example will show an alert.
      </Text>
      <Announced message={state.selectionDetails} />
      <TextField
        className={exampleChildClass}
        label="Filter by name:"
        onChange={(e, t) => _onFilter(e, t ?? "")}
        styles={textFieldStyles}
      />
      <Announced
        message={`Number of items after filter applied: ${state.items.length}.`}
      />
      <MarqueeSelection selection={selection}>
        <DetailsList
          items={state.items}
          columns={_columns}
          setKey="set"
          layoutMode={DetailsListLayoutMode.justified}
          selection={selection}
          selectionPreservedOnEmptyClick={true}
          ariaLabelForSelectionColumn="Toggle selection"
          ariaLabelForSelectAllCheckbox="Toggle selection for all items"
          checkButtonAriaLabel="select row"
          onItemInvoked={_onItemInvoked}
        />
      </MarqueeSelection>
    </Fabric>
  ) : (
    <div>Loading</div>
  );
};

编辑clever-germain-t476s
更新
要在 JSX 中传递这个代码示例,这很容易,您只需要删除所有类型的东西。
为了获取数据,我使用 axios。
见下面的代码:

import * as React from "react";
import { Announced } from "office-ui-fabric-react/lib/Announced";
import { TextField } from "office-ui-fabric-react/lib/TextField";
import {
  DetailsList,
  DetailsListLayoutMode,
  Selection
} from "office-ui-fabric-react/lib/DetailsList";
import { MarqueeSelection } from "office-ui-fabric-react/lib/MarqueeSelection";
import { Fabric } from "office-ui-fabric-react/lib/Fabric";
import { mergeStyles } from "office-ui-fabric-react/lib/Styling";
import { Text } from "office-ui-fabric-react/lib/Text";
import axios from "axios";
const exampleChildClass = mergeStyles({
  display: "block",
  marginBottom: "10px"
});

const textFieldStyles = {
  root: { maxWidth: "300px" }
};

export const DetailsListBasicExampleFunction = () => {
  const _allItems = [];
  const [selection, setSelection] = React.useState();
  function _getSelectionDetails() {
    const selectionCount = selection ? selection.getSelectedCount() : 0;

    switch (selectionCount) {
      case 0:
        return "No items selected";
      case 1:
        return "1 item selected: " + selection.getSelection()[0].name;
      default:
        return `${selectionCount} items selected`;
    }
  }
  const [state, setState] = React.useState({
    items: _allItems,
    selectionDetails: _getSelectionDetails()
  });
  React.useEffect(() => {
    const _selection = new Selection({
      onSelectionChanged: () =>
        setState((prev) => {
          return { ...prev, selectionDetails: _getSelectionDetails() };
        })
    });
    setSelection(_selection);
    //********************** */fetch data from api***************************************
    axios
      .get("/data.json") //pass your url in param
      .then((res) =>
        setState((prev) => {
          return { ...prev, items: res.data };
        })
      ); //pass data in setState
  }, []);
  const _columns = [
    {
      key: "column1",
      name: "Name",
      fieldName: "name",
      minWidth: 100,
      maxWidth: 200,
      isResizable: true
    },
    {
      key: "column2",
      name: "Value",
      fieldName: "value",
      minWidth: 100,
      maxWidth: 200,
      isResizable: true
    }
  ];

  // Populate with items for demos.

  const _onFilter = (ev, text) => {
    console.log(text);
    setState((prev) => {
      return {
        ...prev,
        items: text
          ? _allItems.filter((i) => i.name.toLowerCase().indexOf(text) > -1)
          : _allItems
      };
    });
  };

  const _onItemInvoked = (item) => {
    alert(`Item invoked: ${item.name}`);
  };
  return selection ? (
    <Fabric>
      <div className={exampleChildClass}>{state.selectionDetails}</div>
      <Text>
        Note: While focusing a row, pressing enter or double clicking will
        execute onItemInvoked, which in this example will show an alert.
      </Text>
      <Announced message={state.selectionDetails} />
      <TextField
        className={exampleChildClass}
        label="Filter by name:"
        onChange={(e, t) => _onFilter(e, t ?? "")}
        styles={textFieldStyles}
      />
      <Announced
        message={`Number of items after filter applied: ${state.items.length}.`}
      />
      <MarqueeSelection selection={selection}>
        <DetailsList
          items={state.items}
          columns={_columns}
          setKey="set"
          layoutMode={DetailsListLayoutMode.justified}
          selection={selection}
          selectionPreservedOnEmptyClick={true}
          ariaLabelForSelectionColumn="Toggle selection"
          ariaLabelForSelectAllCheckbox="Toggle selection for all items"
          checkButtonAriaLabel="select row"
          onItemInvoked={_onItemInvoked}
        />
      </MarqueeSelection>
    </Fabric>
  ) : (
    <div>Loading</div>
  );
};

编辑节日-bhabha-jtj65

于 2021-04-01T16:18:51.673 回答