admin管理员组文章数量:1425675
Currently I have a table of my employees using material-table
. There are some employees with certain flags which make the background row red. This is what it looks like:
What I want to do is to be able to edit the rows with the red backgrounds and only these rows. I don't want to put the editable
prop on the entire table because that would take up space with icons for every single row, but I only want to be able to edit the rows with these special flags.
Here is my React code. Note that forgotClockOut
is the special flag that I have.
<MaterialTable
icons={icons}
title="Daily Report"
columns={[
{ title: 'Name', field: 'name' },
{
title: 'Time In',
field: 'started',
render: ({ started }) =>
started
? moment(started).format('h:mm A')
: 'Not yet',
cellStyle: (value, { started, clockedOut }) => {
if (!started) {
return null;
}
if (clockedOut) {
return { color: 'red' };
}
return { color: '#06c92d' };
},
},
{ title: 'Time Out', field: 'clockedOut', render: ({clockedOut}) => clockedOut ? moment(clockedOut).format('h:mm A') : 'Not yet'},
{ title: 'Time Worked', field: 'minutesWorked', render: ({minutesWorked}) => `${Math.floor(minutesWorked / 60)}h ${minutesWorked % 60}m`},
]}
options={{
rowStyle: ({forgotClockOut}) => {
if(forgotClockOut) {
return { backgroundColor: '#ffaaaa' };
}
}
}}
onRowClick={(event, rowData) => {
const {forgotClockOut} = rowData;
// ?? What to do here ??
}}
data={employees}
/>
Is there any way to only edit certain rows in a table made using material-table
?
Currently I have a table of my employees using material-table
. There are some employees with certain flags which make the background row red. This is what it looks like:
What I want to do is to be able to edit the rows with the red backgrounds and only these rows. I don't want to put the editable
prop on the entire table because that would take up space with icons for every single row, but I only want to be able to edit the rows with these special flags.
Here is my React code. Note that forgotClockOut
is the special flag that I have.
<MaterialTable
icons={icons}
title="Daily Report"
columns={[
{ title: 'Name', field: 'name' },
{
title: 'Time In',
field: 'started',
render: ({ started }) =>
started
? moment(started).format('h:mm A')
: 'Not yet',
cellStyle: (value, { started, clockedOut }) => {
if (!started) {
return null;
}
if (clockedOut) {
return { color: 'red' };
}
return { color: '#06c92d' };
},
},
{ title: 'Time Out', field: 'clockedOut', render: ({clockedOut}) => clockedOut ? moment(clockedOut).format('h:mm A') : 'Not yet'},
{ title: 'Time Worked', field: 'minutesWorked', render: ({minutesWorked}) => `${Math.floor(minutesWorked / 60)}h ${minutesWorked % 60}m`},
]}
options={{
rowStyle: ({forgotClockOut}) => {
if(forgotClockOut) {
return { backgroundColor: '#ffaaaa' };
}
}
}}
onRowClick={(event, rowData) => {
const {forgotClockOut} = rowData;
// ?? What to do here ??
}}
data={employees}
/>
Is there any way to only edit certain rows in a table made using material-table
?
-
Will you be able to share your code on how you display
employees
? – bertdida Commented Aug 10, 2020 at 1:15 - @bertdida Sure! I've added it to the question. – matthewcoding0 Commented Aug 10, 2020 at 1:21
2 Answers
Reset to default 4You can use the edit prop to define that:
<MaterialTable
editable={{
isEditable: rowData => rowData.name === 'a', // only name(a) rows would be editable
}}/>
You can see that in docs: https://material-table./#/docs/features/editable
You can't hide those 2 icons even if using prop isEditable and isDeletable. If you want to pletely hide them, try to use Conditionals Actions.
If you need a quick workaround this problem, you can create an Action that appears under condition with the hidden prop.
const actions={
[
rowData => ({
icon: 'delete',
isFreeAction: false,
tooltip: 'Delete User',
hidden: !rowData.deletable
onClick:() => {...}
})
]}
本文标签: javascriptHow to make just 1 row editable in materialtableStack Overflow
版权声明:本文标题:javascript - How to make just 1 row editable in material-table? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745452706a2658959.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论