reactjs - Material-ui 表排序不起作用reactjs

标签 reactjs material-ui

美好的一天!目前使用 Material-ui 表,我在排序时遇到问题。我关注了 documentation 上的内容但排序在我这边不起作用。

这是我的代码:

import TablePagination from '@material-ui/core/TablePagination';
import Table from '@material-ui/core/Table';
import TableBody from '@material-ui/core/TableBody';
import TableCell from '@material-ui/core/TableCell';
import TableContainer from '@material-ui/core/TableContainer';
import TableHead from '@material-ui/core/TableHead';
import TableFooter from '@material-ui/core/TableFooter'
import TableRow from '@material-ui/core/TableRow';
import Paper from '@material-ui/core/Paper';
import TableSortLabel from '@material-ui/core/TableSortLabel';

function descendingComparator(a,b, orderBy) {
  console.log('a',a)
  console.log('b',b)
  console.log('orderBy',orderBy)
  if(b[orderBy] < a[orderBy]){
    console.log('-1')
    return -1;
  }
  if(b[orderBy] > a[orderBy]){
    console.log('1')
    return 1;
  }
  console.log('0')
  return 0;
}

function getComparator(order, orderBy){
  return order === "desc"
    ? (a,b) => descendingComparator(a,b, orderBy)
    : (a,b) => -descendingComparator(a,b, orderBy)
}

const sortedRowInformation = (rowArray, comparator) => {
  const stabilizedRowArray = rowArray.map((el, index) => [el, index])
  stabilizedRowArray.sort((a,b) =>{
    const order = comparator(a[0], b[0])
    if(order !==0) return order;
    return a[1] - b[1];
})
return stabilizedRowArray.map((el) => el[0])
}

function Test(){
  const [dummyDatas, setdummyDatas] = useState([])
  const getInformation= async () => {
    await axios.get('/API')
        .then(response => {
            setdummyDatas(response.dummyDatas)
        })
  }
}

useEffect(()=>{
  getInformation()
})

const [page, setPage] = useState(0)
const [rowsPerPage, setRowsPerPage] = useState(5)
const [pageIndex, setPageIndex] = useState(0)
const [order, setOrder] = useState()
const [orderBy, setorderBy] = useState()
const [orderDirection, setorderDirection] = useState("asc")
const [valueToOrderBy, setvalueToOrderBy] = useState("details")

return(
 <div>
   <TableContainer component={Paper}>
     <Table className={classes.table} aria-label="information table">
       <TableHead>
         <TableRow>
           <TableCell align = "left" key = "details">
             <TableSortLabel
                active={valueToOrderBy === "details"}
                direction={valueToOrderBy === "details" ? orderDirection: 'asc'}
                onClick={createSortHandle("details")}
             >
                Details
             </TableSortLabel>
           </TableCell>
           <TableCell align = "left" key = "status">
             <TableSortLabel
               active={valueToOrderBy === "status"}
               direction={valueToOrderBy === "status" ? orderDirection: 'asc'}
               onClick={createSortHandle("status")}
             >
               Status
             </TableSortLabel>
           </TableCell>
           <TableCell align = "left" key = "system">
             <TableSortLabel
               active={valueToOrderBy === "system"}
               direction={valueToOrderBy === "system" ? orderDirection: 'asc'}
               onClick={createSortHandle("system")}
             >
               System
             </TableSortLabel>
           </TableCell>
         </TableRow>
       </TableHead>
       <TableBody>
         {
           sortedRowInformation(dummyDatas, getComparator(orderDirection, valueToOrderBy))
           .map((row, index1) => (
                        <TableRow key={index1}>
                            <TableCell>
                                        {row.ADVISORYTITLE}
                            </TableCell>
                            <TableCell>
                                        {row.STATUS}
                            </TableCell>
                            <TableCell>
                                        {row.SYSTEMID}
                            </TableCell>
                        </TableRow>
                    ))}
       </TableBody>
     </Table>
   </TableContainer>
 </div>
)

单击状态列上的排序箭头后,没有任何反应。这是我的 console.log 的样子。 enter image description here

希望你能帮我解决这个问题。提前非常感谢您。

最佳答案

使 valueToOrderBy 与数据列相同。 (例如咨询标题)

关于reactjs - Material-ui 表排序不起作用reactjs,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67830934/

相关文章:

reactjs - React Formik Material UI 自动完成 : How can I populate value inside of autocomplete from localStorage?

reactjs - 如何清理单页应用程序上的嵌入式 youtube 订阅按钮

mysql - React-Admin 演示项目正在使用的 SQL 数据库

javascript - 服务器端呈现和单页应用程序

javascript - 在 React 中编辑多个输入字段

reactjs - 在 React 中按下文本字段 @mui 内的 Enter 键后,如何将文本转换为标签

javascript - Material UI ListItem 不允许我设置唯一的 key prop

reactjs - 如何在 Material UI 中创建每个标签具有多个标签的自定义标签?

javascript - 第一个使用 React-Native 获得了意外的 token

javascript - JS/Reactjs - 访问从顶级函数传递的参数