これは典型的な問題です。列を動的に(ajaxで)追加、更新、削除できるデータ表があるとします。
データテーブルは複雑で、多くのフィールド、ドロップダウン、テキストボックスなどがあります。javascriptで複雑なHTMLを作成する
どのようにしてJavaScriptで行を作成しますか?私は現在、多くのJS/Jqueryを作成してノードを生成していますが、それは非常に醜いものであり、あまり再利用できません。コードビハインドロードは、ちょうどfacetsSerializedHidden
に現在のデータをシリアライズするとき
<!-- table to contain the data -->
<table id="facetsTable">
<thead>
<tr>
<td> </td>
<th>Label</th>
<th>Display type</th>
<th>Is filter</th>
<th>Sort type</th>
<th> </th>
</tr>
</thead>
<tbody>
</tbody>
</table>
<br />
<!-- dropdown containing the facets to add -->
<asp:DropDownList runat="server" ID="facetsToAddDdl">
<asp:ListItem Text="" Value=""></asp:ListItem>
</asp:DropDownList>
<asp:Button runat="server" ID="facetAddButton" Text="[ADD]" />
<!-- hidden fields to contain both the current data and all the possible data -->
<asp:HiddenField runat="server" ID="facetsSerializedHidden" />
<asp:HiddenField runat="server" ID="allAttributesSerializedHidden" />
:
私のaspxのはこれです。保存するときに、逆シリアル化します。
とJavaScriptはこれです:
var allAttributes = null;
/*****************************
* Adds a facet to the table
*****************************/
function facetAdd(data) {
// build the table row
var tr = $('<tr>');
// "move" handle
tr.append($('<td>').addClass('handle').append($('<img/>').attr('src', 'img/drag-drop.gif')));
// label
tr.append(
$('<td>')
.addClass('label')
.text(data.attr.DefaultListLabel.DefaultValue + ' (' + data.attr.Code + ')')
.append($('<input />').attr('type', 'hidden').attr('name', 'code').val(data.attr.Code)));
// display type
var disp = $('<select>').attr('name', 'displayType');
// snip: fill options of the select
if (data.facet && data.facet.DisplayType !== '') { disp.val(data.facet.DisplayType) }
disp.change(function() { serializeFacets(); });
tr.append($('<td>').append(disp));
// ###### snip similar logic for the other fields ######
// 'remove' image button
var delImg = $('<img />').attr('src', 'img/delete.png');
delImg.click(function() { facetRemove($(this)) });
tr.append($('<td>').addClass('delete').append(delImg));
// add the row to the table
$('#facetsTable tbody').append(tr);
}
/*****************************
* Removes a facet from the table
*****************************/
function facetRemove(src) {
var tr = src.parents('tr');
var code = tr.find('[name=code]').val();
var label = tr.find('td.label').text();
// remove from table
$(tr).remove();
// add back item to dropdown list
var drop = $('#<%= facetsToAddDdl.ClientID %>');
drop.append($('<option>', { value: code, text: label }));
// re-serialize
serializeFacets();
}
/*****************************
* Serialize the facets
*****************************/
function serializeFacets() {
var ser = [];
var i = 0;
$('#facetsTable tbody tr').each(function() {
var facet = new Object();
facet.Code = $(this).find('[name=code]').val();
facet.Id = $(this).find('[name=code]').val();
facet.DisplayType = $(this).find('[name=displayType]').val();
facet.IsFilter = $(this).find('[name=isFilter]').prop('checked');
facet.SortType = $(this).find('[name=sortType]').val();
facet.Order = i;
ser.push(facet);
i++;
});
$('#<%= facetsSerializedHidden.ClientID %>').val(JSON.stringify(ser));
}
/*****************************
* Activate sorting on the table
*****************************/
function activateSorting() {
$('#facetsTable tbody').sortable({
stop: function() { serializeFacets(); },
handle: ".handle"
});
}
/*****************************
* Get the facet data from the attributes list
*****************************/
function getFacetData(code, existingFacet) {
if (allAttributes === null) { return null; }
for (var i = 0; i < allAttributes.length; i++) {
if (allAttributes[i].Code === code) {
var facet = {};
if (existingFacet === null) {
facet.Code = code;
facet.Id = code;
facet.Order = -1;
} else {
facet = existingFacet;
}
return { attr: allAttributes[i], facet: facet };
}
}
return null;
}
$(function() {
// read attributes list
var allAttributesSerialized = $("#<%= allAttributesSerializedHidden.ClientID %>").val();
if (allAttributesSerialized !== '' && allAttributesSerialized !== null) {
allAttributes = JSON.parse(allAttributesSerialized);
}
// activate sorting on the table
activateSorting();
/*****************************
* Initialize facets on load
*****************************/
var currentFacetsSerialized = $('#<%= facetsSerializedHidden.ClientID %>').val();
if (currentFacetsSerialized !== '' && currentFacetsSerialized !== null) {
var currentFacets = JSON.parse(currentFacetsSerialized);
for (var i = 0; i < currentFacets.length; i++) {
facetAdd(getFacetData(currentFacets[i].Code, currentFacets[i]));
}
}
/*****************************
* Add a facet to the table on button click
*****************************/
$("#<%= facetAddButton.ClientID %>").click(function() {
var code = $('#<%= facetsToAddDdl.ClientID %>').val();
if (code === null || code === '') { return false; }
facetAdd(getFacetData(code, null));
$('#<%= facetsToAddDdl.ClientID %> option:selected').remove();
activateSorting();
serializeFacets();
return false;
});
});
私は行を作成できるように、私は、私が(例えばサービスを通じて)アヤックスに呼び出すことができますユーザーコントロールに私の行のHTMLを持っているのが大好きですJSと.netの両方で、私はどのように進むのかは分かりません。
スタート、および_you_合ったものを選びますベスト。 –
現在、「醜い」とは何ですか? (たぶん、これは、スタックオーバーフローの代わりにコードレビューサイトに属しています)私はちょうど与えられた値のセットで行を作成し、ちょうど私が行を追加する必要があるときにその関数を呼び出すJavaScriptでいくつかのテンプレート関数を作成すると思います。 – David
これは非常に未解決の問題です。私が間違っていない限り(それを無視してください) – evolutionxbox