1

Firebaseを使用して、データベース内の各要素を動的に編集および削除するためのボタンを作成していますjavascript。また、データベースに要素が 1 つしかない場合 (これらの要素は投票/選挙を表します)、以下のコードは正常に機能します。しかし、複数のデータベース要素があり、したがってボタンの行が複数ある場合、追加された編集/削除ボタンの最後のペアのみが実際にクリックイベントにバインドされます。これは、以前のすべてのバインドが何らかの方法で上書きされていることを意味すると思います? pollsRef.once()また、との両方が非同期関数呼び出しであることにも言及する必要がありpollsSnapshot.forEach()ます (すべてのFirebaseAPI 呼び出しと同様)。ボタンを作成してバインドする関数は次のとおりです。...

function displayCurrentPollsForEditing(pollsRef)
{
    var tbl = createTable();
    var th = ('<th>Polls</th>');
    $(th).attr('colspan', '3');
    $(th).appendTo($(tbl).children('thead'));
    pollsRef.once('value', function(pollsSnapshot) {
        pollsSnapshot.forEach(function(pollsChild) {
            var type = pollsChild.name();
            // If this is true if means we have a poll node
            if ($.trim(type) !== "NumPolls") 
            {
                // Create variables
                var pollRef = pollsRef.child(type);
                var pollName = pollsChild.val().Name;
                var btnEditPoll = $('<button>EDIT</button>');
                var btnRemovePoll = $('<button>REMOVE</button>');
                var tr = $('<tr></tr>');
                var voterColumn = $('<td></td>');
                var editColumn = $('<td></td>');
                var rmvColumn = $('<td></td>');
                // Append text and set attributes and listeners
                $(voterColumn).text(pollName);
                $(voterColumn).attr('width', '300px');
                $(btnEditPoll).attr({
                    'class': 'formee-table-button',
                    'font-size': '1.0em'
                });
                $(btnRemovePoll).attr({
                    'class': 'formee-table-remove-button',
                    'font-size': '1.0em'
                });
                $(btnEditPoll).appendTo($(editColumn));
                $(btnRemovePoll).appendTo($(rmvColumn));
                // Append to row and row to table body
                $(tr).append(voterColumn).append(editColumn).append(rmvColumn);
                $(tr).appendTo($(tbl).children('tbody'));
                // Append table to div to be displayed
                $('div#divEditPoll fieldset#selectPoll div#appendPolls').empty();
                $(tbl).appendTo('div#divEditPoll fieldset#selectPoll div#appendPolls');
                $(btnEditPoll).click(function() {
                    displayPollEditOptions(pollRef);
                    return false;
                });
                $(btnRemovePoll).click(function() {
                    deletePoll($(this), pollsRef);
                    return false;
                });
            }
        });
    });
}

ボタンはプログラムで生成されたテーブルにレンダリングされているだけで、すべてのjQueryセレクターは正しいです。

4

0 に答える 0