我有一个这样的数据库表:
ID | 价值 | 可编辑 |
---|---|---|
0 | 公司0 | 和 |
1 | 公司1 | N |
2 | 公司2 | N |
5 | 公司5 | 和 |
99 | 公司其他 | N |
在我的反应代码中,在对该表进行 api 调用(本文未显示)后,结果存储在sessionStorage
浏览器中Key
,Value
如下所示。
钥匙 : categories
价值:
[{
"id": 0,
"value": "Company0",
"isEditable": "Y",
"_links": {
"self": {
"href": "http://mydevserver.com:443/WebServices/api/categories/0"
},
"category": {
"href": "http://mydevserver.com:443/WebServices/api/categories/0"
}
}
},
{
"id": 1,
"value": "Company1",
"isEditable": "Y",
"_links": {
"self": {
"href": "http://mydevserver.com:443/WebServices/api/categories/1"
},
"category": {
"href": "http://mydevserver.com:443/WebServices/api/categories/1"
}
}
},
{
"id": 2,
"value": "Company2",
"isEditable": "Y",
"_links": {
"self": {
"href": "http://mydevserver.com:443/WebServices/api/categories/2"
},
"category": {
"href": "http://mydevserver.com:443/WebServices/api/categories/2"
}
}
},
{
"id": 5,
"value": "Company 5",
"isEditable": "Y",
"_links": {
"self": {
"href": "http://mydevserver.com:443/WebServices/api/categories/5"
},
"category": {
"href": "http://mydevserver.com:443/WebServices/api/categories/5"
}
}
},
{
"id": 99,
"value": "Company Other",
"isEditable": "Y",
"_links": {
"self": {
"href": "http://mydevserver.com:443/WebServices/api/categories/99"
},
"category": {
"href": "http://mydevserver.com:443/WebServices/api/categories/99"
}
}
}
]
我有以下actionTemplate
功能来显示EDIT
,DELETE
和DOWNLOAD
按钮,如下所示:
在rowData
变量中,我可以categoryId
通过 using rowData.categoryId
which 来访问,这相当于ID
上表的列。
actionTemplate = (rowData: any) => (
console.log(rowData),
<div style={{textAlign: 'center', width: '6em'}}>
<span>
{ JSON.parse(sessionStorage.getItem('categories') as string)[rowData.categoryId].isEditable === 'N' ?
<Button icon="pi pi-trash" style={{marginRight: '5px'}} tooltip='Delete' />
:
<span>
<Button type='button' icon="pi pi-pencil" style={{marginRight: '5px'}} onClick={(e) => this.handleClick(rowData, e)} tooltip='Edit'/>
<Button icon="pi pi-trash" style={{marginRight: '5px'}} tooltip='Delete' />
</span>
}
{
rowData.fullPathName !== null &&
<Button icon="pi pi-download" tooltip='Download' onClick={(e) => this.handleDownloadClick(rowData, e)} />
}
</span>
</div>
);
我的要求:
EDIT
当ISEDITABLE
列设置为时不显示按钮N
。并EDIT
在ISEDITABLE
列设置为时显示按钮Y
。
所以,我对上面的函数进行了修改,以达到上面的要求。但是,当数组索引与ID
上表的列不匹配时,我遇到了问题。
例如,只要rowData.categoryId
= 0,1 和 2上面的代码运行良好,因为直到ID = 2
,它匹配数组索引。但是,一旦它达到 ID = 5 或 99,它就会中断,因为ID = 5
is4
和 for ID = 99
is 的数组索引位置5
。
除了我使用的方法之外,是否有解决此问题的方法?
因为categories
是一个对象数组,所以您需要使用它find
来获取您想要的元素,如下所示:
{
JSON.parse(sessionStorage.getItem('categories') as string).find(
category => category.id === rowData.categoryId
)?.isEditable === 'N' ? (
<Button
icon="pi pi-trash"
style={{ marginRight: '5px' }}
tooltip="Delete"
/>
) : (
<span>
<Button
type="button"
icon="pi pi-pencil"
style={{ marginRight: '5px' }}
onClick={e => this.handleClick(rowData, e)}
tooltip="Edit"
/>
<Button
icon="pi pi-trash"
style={{ marginRight: '5px' }}
tooltip="Delete"
/>
</span>
);
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句