reactjs 在AgGrid中显示筛选值

ghhkc1vu  于 2023-02-18  发布在  React
关注(0)|答案(1)|浏览(135)

我使用自定义的CategoryFilter为我的类别AgGrid:

const columnDefs = [
    {
        field: 'categoryId',
        headerName: t('category', { ns: 'common' }),
        cellRendererSelector: (params: ICellRendererParams<IProduct>) => ({
            component: CategoryColumn,
            params: {
                ...params,
                defaultLanguage
            }
        }),
        filter: CategoryFilter,
        floatingFilterComponentParams: {
            suppressFilterButton: false,
        },
    }
]

当我按类别筛选时,我可以在输入字段中输入值,但它仍然没有显示在列名称下:

如何在“类别”列标题下显示所选类别?

mwkjh3gx

mwkjh3gx1#

您可以通过forwardRef<CategoryColumn/>引用传递给父组件,并通过<CategoryColumn/>内部的useImperativeHandle函数参数访问父模型的值。

    • 示例**

CategoryColumn

import React, {
  Fragment,
  forwardRef,
  useImperativeHandle,
  useRef,
} from 'react';
import { IFloatingFilterParams } from 'ag-grid-community';

export default forwardRef((props: IFloatingFilterParams<any>, ref) => {
  const inputRef = useRef<HTMLInputElement>(null);

  // expose AG Grid Filter Lifecycle callbacks
  useImperativeHandle(ref, () => {
    return {
      onParentModelChanged(parentModel: number | null) {
        console.log(parentModel)
        // When the filter is empty we will receive a null value here
        if (parentModel == null) {
          inputRef.current!.value = '';
        } else {
          inputRef.current!.value = parentModel + '';
        }
      },
    };
  });

  return (
    <Fragment>
      &gt;{' '}
      <input
        ref={inputRef}
        style={{ width: '30px' }}
        type="number"
        min="0"
      />
    </Fragment>
  );
});

index.tsx

... 

  const [columnDefs, setColumnDefs] = useState<ColDef[]>([
    { field: 'athlete', filter: 'agTextColumnFilter' },
    {
      field: 'gold',
      floatingFilterComponent: NumberFloatingFilterComponent,
      floatingFilterComponentParams: {
        suppressFilterButton: true,
      },
      filter: NumberFilterComponent,
    },
  ]);

  const defaultColDef = useMemo<ColDef>(() => {
    return {
      filter: true,
      floatingFilter: true,
    };
  }, []);
...

读过银栅浮动过滤器!

相关问题