0

jQueryとasp.netでjsonを使用してデータベースからデータを取得し、毎回テーブルごとにこれを行っています。1 つの関数を作成し、すべてのテーブルに (列名、テーブル名、表示するコントロール) などのパラメーターを渡す方法はありますか。

function LoadData(Url, Data, ControlToShow, Columns){
    $.ajax({ type: "post",
        url: Url,
        data: Data,
        contentType: "application/json;charset=utf-8", dataType: "json",
        success: function (data) {
            if (data.d != null || data.d != 'null') {
                var items = data.d;

                $("#" + ControlToShow).append(items[0].Columns[1]);
            }
        }
}

または、別の言葉で、次のような列名を渡すことにより、動的な json 配列オブジェクトを使用します

var items = response.d;
var colName = 'Customers';
alert(items[0].colName);

colName = 'CustomerID';
alert(items[0].colName);

または、このようなもの。

4

4 に答える 4

0

テンプレート ビューを作成し、各テーブルのデータを渡すことができます。本文セクションでは、データを単純に反復処理して、テーブルの本文セクションに追加できます。

<table id="<%= YOUR TABLE NAME %>">
<thead>
<tr>
<th><% YOUR COLUMN NAME %></th>
....
</tr>
<thead>
<tbody class="mytable"></tbody>
...
</table>

success: function (data) {
            if (data.d != null || data.d != 'null') {
                var items = data.d;

var tr = $('<tr></tr>').appendTo('.mytable');
                $.each(data.d, function(){

                  //append your td elements to your tr above

                });
            }
    }
于 2013-10-21T11:40:23.893 に答える