テーブルを構築する php から json ajax データを取得するためのボタンを押すと、テーブルの見出し (asc、dsc) をクリックすると問題が発生します。テーブル内の値を並べ替えるには、テーブル見出しで関数 sortresult を使用します。関数のソート結果がテーブルを構築します。json データを正常に受信しました。
データを表示するためにボタンを使用しない場合 (コードを少し変更するだけ)、自動的に ajax を使用して json を取得し、テーブルを作成すると、クリックは正常に機能します。ボタンで動作しない問題は何ですか?
だから私は機能を持っています:
$(document).ready(function(){
$('#submit').click(function(event){
$('#headings th').click(function(){
$('#results').html("");
var id=$(this).attr('id');
var asc =(!$(this).attr('asc'));
$('#headings th').each(function () {
$(this).removeAttr('asc');
});
if(asc) $(this).attr('asc','asc');
sortResult(id, asc);
});
showResult();
});
});
関数 sortResult:
function sortResult(prop, asc){
var val=null;
dataOut = dataOut.sort(function(a,b){
if(asc) return (a[prop] > b[prop]);
else return (b[prop] > a[prop]);
});
showResult();
}
関数 showresult:
function showResult(){
var html='';
for (var i in dataOut){
html +='<tr>'
+'<td>'+dataOut[i].email+'</td>'
...
+'</tr>'
}
html+='</table>'
$('#results').html(html);
}