APIからフェッチされたデータをテーブルに出力しています。カラム値をアルファベット順および数値順に並べ替えるのが困難です。 API内の列のデータが数値で構成されている場合、すなわち10.01, 333.01, 8.99 or 100.88
など100 -> 0
を降順最初のクリックにし、2回目のクリックで0 -> 100
から昇順でソートし、アルファベット順の値に対して同じロジック、つまりINにa-z && Z-A
およびその逆。私はJavaScriptでそれを符号化するために使用されるように私はonclick="sortTable(0)"
に精通していますReactJS:動的にレンダリングされたonClick()ソート要素
機能は、リアクトで同じように機能しません。おそらく、私はES6の標準を誤解していますが、わかりません。ここで
onclick="sortTable(0)"
と私のサンプルコードですが、それがうまく機能していないです:
class App extends React.Component
{
constructor()
{
super();
this.state = {
rows: [],
columns: [],
clicked: false
}
this.handleClick = this.handleClick.bind(this);
}
handleClick()
{
this.setState({
clicked: true
});
}
sortTable(n)
{
var table, columns, switching, i, x, y, shouldSwitch, dir, switchcount = 0;
table = document.getElementById("datagrid");
switching = true;
//Set 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;
columns = table.getElementsByTagName("TH");
/*Loop through all table rows (except the
first, which contains table headers):*/
for (i = 1; i < (columns.length - 1); i++)
{
//start by saying there should be no switching:
shouldSwitch = false;
/*Get the two elements you want to compare,
one from current row and one from the next:*/
x = columns[i].getElementsByTagName("TH")[n];
y = columns[i + 1].getElementsByTagName("TH")[n];
/*check if the two rows should switch place,
based on the direction, asc or desc:*/
if (dir == "asc")
{
if (x.innerHTML.toLowerCase() > y.innerHTML.toLowerCase())
{
//if so, mark as a switch and break the loop:
shouldSwitch = true;
break;
}
} else if (dir == "desc")
{
if (x.innerHTML.toLowerCase() < y.innerHTML.toLowerCase())
{
//if so, mark as a switch and break the loop:
shouldSwitch = true;
break;
}
}
}
if (shouldSwitch)
{
/*If a switch has been 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;
}
}
}
}
componentDidMount()
{
fetch("http://ickata.net/sag/api/staff/bonuses/")
.then(function (response)
{
return response.json();
})
.then(data =>
{
this.setState({ rows: data.rows, columns: data.columns });
});
}
render()
{
return (
<div id="container" className="container">
<h1>Final Table with React JS</h1>
<table className="datagrid">
<thead>
<tr> {
this.state.columns.map((column, index) =>
{
return (<th onClick={this.handleClick}>{column}</th>)
}
)
}
</tr>
</thead>
<tbody> {
this.state.rows.map(row => (
<tr>{row.map(cell => (
<td>{typeof cell === 'number' ? cell.toFixed(2) : cell}</td>
))}
</tr>
))
}
</tbody>
</table>
</div>
)
}
}
ReactDOM.render(<div id="container"><App /></div>, document.querySelector('body'));
をあなたは私のレポに直接貢献することを歓迎している:Fetching API data into a table
マイ例えば、onclick="sortTable(0)"
が使用された直後には、coのエラーは発生しませんnsoleと同じ結果を出力します。私は正しいとは思わない<th onClick={this.handleClick}>{column}</th>
要素です。
残念ながら、私は、私はどちらかSORの機能をデバッグすることができないように、それは、全くonClickイベントを機能していないです
に関連するドキュメントを見つけることができませんでした。
ご了承ください。
こんにちは@ stack26回答ありがとうございますが、これは私のケースに関連しているとは思わないです。最初に、ここでは列と行を別々に配列しています。上記の説明では、列のヘッダー要素を使ってonClickイベントを通じて行の値の並べ替えを処理したいと述べています。 – antdimit