如何在Primereact中为Datatable添加样式?

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

How can i add style to the Datatable in Primereact?

问题

我正在使用PrimeReact设计一个Datatable。它占据了整个屏幕的宽度,但我想要缩小它。所以我使用了DataTable的tableStyle属性,它对表格起作用,但不对表头起作用,表头仍然占据整个屏幕的大小。

我尝试将样式属性写在HTML标记中,但它不起作用。

以下是代码:

const header = (
    <div className="flex flex-wrap align-items-center justify-content-between gap-2">
        <span className="text-xl text-900 font-bold">Employees</span>
        <Button icon="pi pi-refresh" className='ml-5' rounded raised />
    </div>
);

return (
    <DataTable
        value={data}
        header={header}
        showGridlines
        tableStyle={{
            minWidth: '60rem',
            width: '400px',
            alignItems: 'center',
            marginLeft: 'auto',
            marginRight: 'auto'
        }}
    >
        <Column field="name" header="Name"></Column>
        <Column field="email_id" header="Email-id"></Column>
        <Column header="Status" body={checkbody}></Column>
    </DataTable>
)

附带的屏幕截图显示了当前的用户界面,其中我的表头占据整个屏幕的大小,刷新图标也没有出现在屏幕的右侧。

英文:

I am designing a Datatable using PrimeReact. It takes up the full width of the screen, but I want to make it smaller. So I use the tableStyle attribute of DataTable, and it works for the table, but not to the header, which still takes the full size of the screen.

I tried writing the style attribute in the HTML tag, but it doesn't work.

Here is the code:

 const header = (
    &lt;div className=&quot;flex flex-wrap align-items-center justify-content-between gap-2&quot;&gt;
        &lt;span className=&quot;text-xl text-900 font-bold&quot;&gt;Employees&lt;/span&gt;
        &lt;Button icon=&quot;pi pi-refresh&quot; className=&#39;ml-5&#39; rounded raised /&gt;
    &lt;/div&gt;
);
  return (
    &lt;DataTable value={data} header={header}  showGridlines tableStyle={{ minWidth: &#39;60rem&#39; ,width:&#39;400px&#39;,alignItems:&#39;center&#39;,marginLeft:&#39;auto&#39;,marginRight:&#39;auto&#39;}}&gt;
    &lt;Column field=&quot;name&quot; header=&quot;Name&quot;&gt;&lt;/Column&gt;
    &lt;Column field=&quot;email_id&quot; header=&quot;Email-id&quot;&gt;&lt;/Column&gt;
    &lt;Column header=&quot;Status&quot; body={checkbody}&gt;&lt;/Column&gt;
  &lt;/DataTable&gt;
)
}

Attached screenshot shows the current UI, where my header is full size nor refresh icon coming to the right side of the screen:

如何在Primereact中为Datatable添加样式?

答案1

得分: 1

你需要安装 primeflex:

npm install primeflex

然后将其导入到你的应用程序:

import 'primeflex/primeflex.css';

以上操作将为页眉应用 flex CSS。要添加固定宽度,你可以通过以下方式覆盖页眉的 CSS:

.p-datatable .p-datatable-header {
    min-width: 60rem;
    width: 400px;
    margin: 0 auto;
}
英文:

You need to install primeflex

npm install primeflex

and import this to your app

import &#39;primeflex/primeflex.css&#39;;

This above will apply flex css for header. To add fixed width you can override the header css using

.p-datatable .p-datatable-header {
    min-width: 60rem;
    width: 400px;
    margin: 0 auto;
}

huangapple
  • 本文由 发表于 2023年3月1日 16:30:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/75601209.html
匿名

发表评论

匿名网友

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

确定