我创建了一个名为function
的{{1}}。当有人点击sortTable()
的{{1}}时,我呼叫function
。我的代码如下所示。
header
该代码目前不支持table
语句。一旦删除import React, { Component } from 'react';
class newsList extends Component {
constructor(props) {
super(props)
window.info = [];
}
render() {
this.props.arr.result ? window.info.push(this.props.arr.result) : null
var result = window.info.map(item => (
<tbody key={item.id} className="item">
<tr>
<td>{item.profileInfo.profileName}</td>
<td>{item.rows[0][0]}</td>
<td>{item.rows[0][1]}</td>
<td>{item.rows[0][2]}</td>
<td>
<a href="#!" className="secondary-content">
<i className="material-icons">
send
</i>
</a>
</td>
</tr>
</tbody>))
//Sort function starts here.
const sortTable = (n, {arr}) =>{
if(arr){
console.log(arr);
var table, rows, switching, i, x, y, shouldSwitch, dir, switchCount = 0;
table = document.getElementById('#dataTable');
switching = true;
//Sort the sorting direction to ascending:
dir = 'asc';
//Make a loop that will continue until no switching has been done:
while(switching){
//Start by saying: no switching is done:
switching = false;
rows = table.getElementsByTagName('TR');
//Loop through all table rows except the headers:
for(i = 1; i < (rows.length - 1); i++){
//Start by saying there should be no switching:
shouldSwitch = false;
//Compare the two elements:
x = rows[i].getElementsByTagName('TD')[n];
y = rows[i].getElementsByTagName('TD')[n];
//Switch the rows:
if( dir === 'asc'){
if(x.innerHTML.toLowerCase() > y.innerHTML.toLowerCase()){
shouldSwitch = true;
break;
}
} else if (dir === 'desc') {
if (x.innerHTML.toLowerCase() < y.innerHTML.toLowerCase()){
shouldSwitch = true;
break;
}
}
}
if(shouldSwitch) {
//If a switch has been done marked, make the switch and mark that a switch has been done:
rows[i].parentNode.insertBefore(rows[i + 1], rows[i]);
switching = true;
//Each time a switch is done, increase this count by 1:
switchCount++;
} else {
//If no Switching has been done AND the direction is 'asc' set the direction to 'desc' and run the while loop again.
if(switchCount === 0 && dir === 'asc'){
dir = 'desc';
switching = true;
}
}
}
}
}
return (
<div>
<table id="dataTable" className="responsive-table">
<thead>
<tr>
<th onClick={() => sortTable(0, windows.info)}>Account Name</th>
<th>Sessions</th>
<th>Bounces</th>
<th>Users</th>
</tr>
</thead>
{result}
</table>
</div>
)
}
}
export default newsList;
语句,代码就会正常工作,但我在一行显示if
时收到错误。错误是if
。我怎样才能使它发挥作用?