2

JSON データセットを html テーブルに変換する次のコードがあります。それが最速の方法であるかどうかを知りたいですか、それとも jquery で jqote2 を使用してテンプレートを作成する必要がありますか?

要件:

  • クリックで列定義を変更できます (クライアントはテーブルの表示方法を変更でき、列定義配列が変更され、テーブルを再構築できます)
  • 並べ替え、フィルター、ページネーション (生データを並べ替えて、テーブルを再構築できると思います)
  • 変換 (たとえば、1 つの列に異なる単位の長さのデータが含まれている場合、データを並べ替えることができるように、1 つの同じ単位のデータを持つ生データ セットの新しい列を追加できます)

それで、私が正しい軌道に乗っているか、すでに存在するものに取り組んでいる場合、誰かが私を導くことができますか?

<div id="hii"><!-- table loads here --></div>

<script>
var set1={ // JSON dataset & col definitions
    'col':[ // definition/template of data to load in column cells
        [function(x){return '<a href="?sid='+x[1]+'">'+x[0]+'</a>';}],
        [function(x){return '<a href="?id='+x[2]+'">'+x[2]+'</a>';}],
    ],
    'data':[ // raw data, output table has only 2cells/row using these 3 values from each row
        ['Name 1','00000001','Value 1'],
        ['Name 2','00000002','Value 2'],
        ['Name 3','00000003','Value 3'],
        ['Name 4','00000004','Value 4'],
        ['Name 1','00000001','Value 5'],
        ['Name 5','00000005','Value 1'],
        ['Name 6','00000006','Value 1'],
        ['Name 7','00000007','Value 2'],
        ['Name 8','00000008','Value 6'],
        ['Name 9','00000009','Value 3'],
        ['Name A','00000010','Value 7'],
        ['Name B','00000011','Value 7'],
        ['Name C','00000012','Value 1'],
    ],
};
function tbody(x){ // function to create table, using dataset name passed to x
    for(i=0,data='';i<x.data.length;i++){
        data+='<tr>';
        for(j=0;j<x.col.length;j++){
            data+='<td>'+x.col[j][0](x.data[i])+'</td>';
        }
        data+='</tr>';
    }
    return data;
}
document.getElementById('hii').innerHTML='<table>'+tbody(set1)+'</table>';
</script>
4

4 に答える 4

2

Datatablesを使用できます。jQuery で動作し、テーマ設定も可能です。要件を満たすことができる、設定できるオプションがかなりあります。

于 2011-07-31T03:06:36.637 に答える
1

それほど簡単ではありません。Sencha (extJS)、特にグリッドを見てみましょう - http://www.sencha.com/learn/legacy/Tutorialshttp://www.sencha.com/learn/legacy/Tutorials#Grids

于 2011-07-31T02:56:03.350 に答える
0

jQuery Templates Plugin を見てみましょう。

http://api.jquery.com/category/plugins/templates/

<ul id="movieList"></ul>

<script id="movieTemplate" type="text/x-jquery-tmpl">
    <li><b>${Name}</b> (${ReleaseYear})</li>
</script>

<script type="text/javascript">
    var movies = [
        { Name: "The Red Violin", ReleaseYear: "1998" },
        { Name: "Eyes Wide Shut", ReleaseYear: "1999" },
        { Name: "The Inheritance", ReleaseYear: "1976" }
    ];

    // Render the template with the movies data and insert
    // the rendered HTML under the "movieList" element
    $( "#movieTemplate" ).tmpl( movies )
        .appendTo( "#movieList" );
</script>
于 2011-07-31T03:03:02.563 に答える