React Material-UI的TablePagination中的labelDisplayedRows

huangapple go评论67阅读模式
英文:

React Material-UI labelDisplayedRows on TablePagination

问题

labelDisplayedRows 是一个函数,它的参数 fromto 是在组件内部自动传递的,不需要手动定义。你可以直接使用这个函数,无需关心参数的来源。在你的代码中,labelDisplayedRows 函数会接收自动传递的 fromtocount 参数,用于生成显示在页面上的文本。

英文:

I have this component for TablePagination:

<TablePagination
	rowsPerPageOptions={[5, 10, 25, 50]}
	component="div"
	count={rows.length}
	rowsPerPage={rowsPerPage}
	page={page}
	onChangePage={handleChangePage}
	onChangeRowsPerPage={handleChangeRowsPerPage}
	style={{width:'10px'}}
	labelRowsPerPage={'Filas por página'}
	labelDisplayedRows={ (from, to, count=rows.length) => (`${from}-${to === -1 ? count : to} de ${count}`)
	}
/>

According to https://material-ui.com/es/api/table-pagination/

labelDisplayedRows is a func but I don't understand where are defined those vars from, and to

I get: [object Object]-undefined de 11 in the render

答案1

得分: 2

worked for me:

labelDisplayedRows={(page) =>
  `${page.from}-${page.to === -1 ? page.count : page.to}${page.count} 中`
}

also you can use components here, like this:

function defaultLabelDisplayedRows({ from }) {
  return (<div>{from}</div>)
}

and then in your pagination component it should be:

labelDisplayedRows={(page) => defaultLabelDisplayedRows(page)}
英文:

worked for me:

labelDisplayedRows={(page) =&gt;
              `${page.from}-${page.to === -1 ? page.count : page.to} از ${
                page.count
              }`
            }

also you can use components here, like this:

function defaultLabelDisplayedRows({ from }) {
    return (&lt;div&gt;{from}&lt;/div&gt;)
  }

and then in your pagination component it should be:

labelDisplayedRows={(page) =&gt; defaultLabelDisplayedRows(page)}

答案2

得分: 1

我从中得到了:

labelDisplayedRows={(from=page) => (`${from.from}-${from.to === -1 ? from.count : from.to} of ${from.count}`})
英文:

Well, I got it from:

labelDisplayedRows={ (from=page) =&gt; (`${from.from}-${from.to === -1 ? from.count : from.to} de ${from.count}`})

答案3

得分: 0

labelDisplayedRows={(from = page) => ${from.from}-${from.to === -1 ? from.count : from.to} de ${from.count}}

英文:

@pmiranda it works perfect but there is an error with some curly brace and parenthesis.

It should be:

labelDisplayedRows={(from = page) => ${from.from}-${from.to === -1 ? from.count : from.to} de ${from.count}}

答案4

得分: 0

只返回翻译好的代码部分:

const defaultLabelDisplayedRows: React.FC<LabelDisplayedRowsArgs> = (props): React.ReactElement<any, any> => {
  return (
    <>
      <p>
        <b> Mostrando itens {" "}
          <span style={{ color: "#FFB03A" }}>
            {props.from > 0 && props.from < 10 ? `0${props.from}-0${props.to}` : `${props.from}-${props.to}`}
          </span>
        </b>
        <p style={{ padding: 0, margin: 0 }}>total de {props.count} itens</p>
      </p>
    </>
  );
}

...

<TablePagination
  count={props.count}
  page={props.page}
  rowsPerPage={props.rowsPerPage}
  labelDisplayedRows={({ from, to, count, page }) => {
    return defaultLabelDisplayedRows({ from, to, count, page });
  }}
/>

请注意,翻译的代码保留了原始HTML实体(如&lt;&gt;)。

英文:

work for me

const defaultLabelDisplayedRows: React.FC&lt;LabelDisplayedRowsArgs&gt; = (props): React.ReactElement&lt;any, any&gt; =&gt; {
  return(
    &lt;&gt;
      &lt;p&gt;
        &lt;b&gt; Mostrando itens {&quot; &quot;}
          &lt;span style={{color:&quot;#FFB03A&quot;}}&gt;
            { props.from &gt; 0 &amp;&amp; props.from &lt; 10 ? `0${props.from}-0${props.to}` : `${props.from}-${props.to}`}
          &lt;/span&gt;
        &lt;/b&gt;   
        &lt;p style={{padding: 0, margin: 0}}&gt;total de {props.count} itens&lt;/p&gt; 
      &lt;/p&gt;
    &lt;/&gt;
  )
}
      &lt;TablePagination 
        count={props.count}
        page={props.page}
        rowsPerPage={props.rowsPerPage}
        labelDisplayedRows={ ({ from, to, count, page }) =&gt; { 
          return defaultLabelDisplayedRows({from, to, count, page}) 
        }}

results

React Material-UI的TablePagination中的labelDisplayedRows

答案5

得分: -1

Pagination: (props) => (
<TablePagination
{...props}
labelDisplayedRows={({from, to, count}) => <>{${from}-${to} ${t('dataGrid:rowsOf')} ${count}}</>}
/>
)

英文:
Pagination: (props) =&gt; (
    &lt;TablePagination
    {...props}        
    labelDisplayedRows={({from, to, count}) =&gt; &lt;&gt;{`${from}-${to} ${t(&#39;dataGrid:rowsOf&#39;)} ${count}`}&lt;/&gt;}
    /&gt;
)

huangapple
  • 本文由 发表于 2020年1月6日 23:19:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/59614607.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定