1

I am using Jquery DataTables 1.7.6 with JQuery 1.8.6. I am trying to setup a datatable that has a button in one row and then capture the click of that button to move the row to another table. I'm having a problem getting at the data in the DataTable to call the add and remove functions.

<script type="text/javascript">
    $(document).ready(function () {
        var eligibleCreatives = $('#EligibleCreativeTableId').dataTable({
            "bJQueryUI": true,
            "bStateSave": true,
            "bAutoWidth": false,
            "aoColumnDefs": [
                { "bSortable": false, "aTargets": [0] },
                { "bVisible": false, "aTargets": [3] },
            ],
            "aaSorting": [[1, "asc"]]
        });
        associatedCreatives = $('#AssociatedCreativeTableId').dataTable({
            "bJQueryUI": true,
            "bStateSave": true,
            "bAutoWidth": false,
            "aoColumnDefs": [
                { "bSortable": false, "aTargets": [0] },
                { "bVisible": false, "aTargets": [3] },
            ],
            "aaSorting": [[1, "asc"]]
        });

        eligibleCreatives.$('tr').click(function () {
            var data = .fnGetData( this );
            // this tells me that eligibleCreatives has no method $

        });


        $('#disassociate-creative').click(function () {
        //I can't get at the actual row node here.
        var data = associatedCreatives.fnGetData($(this).closest('tr')[0]);
        eligibleCreatives.fnAddData(data);
        associatedCreatives.fnDeleteRow(this); 
        return false;
        });
        $('#associate-creative').click(function () {
        var data = associatedCreatives.fnGetData($(this).closest('tr')[0]);
        associatedCreatives.fnAddData(data);
        eligibleCreatives.fnDeleteRow(this);
        return false;
        });

    });
    function fnClickAssociate() {
        $('#AssociatedCreativeTableId').dataTable().fnDeleteRow();
        $('#AssociatedCreativeTableId').dataTable().fnAddData([]);
    }
</script>
4

1 に答える 1

1

問題は、fnGetData がノード自体ではなく、特定の行の内容のデータ オブジェクトを返すことだと思います。ただしfnAddDatafnDeleteRowそれぞれにデータ オブジェクトではなく TR ノードが必要です。これを解決するには、次の 2 つの方法があります。

オプション 1 : 必要な行の TR 要素全体が返されていることが確実な場合は、各関数$(this).closest('tr')[0]の最初の行を次のように変更します。click

var data = $(this).closest('tr')[0];

これにより、次の 2 行の関数に、アクションを実行するための適切な TR ノードが提供されます。

オプション 2$(this).closest('tr')[0] : TR 要素全体が返されているかどうかわからない場合(適切なコンテンツが返されているかどうかを確認するには、これを確認できます) 、ボタンがある TD 要素でconsole.log()Datatable.net を使用することを検討します。はるかに柔軟で、 andfnGetPositionで使用できる要素を常に返すことがわかりました。fnAddDatafnDeleteRow

幸運を!

于 2012-03-16T11:35:15.177 に答える