2016-03-19 7 views
0

私はJavaScriptオブジェクトを持っています。私はデータテーブルに新しい行を追加しようとしています。 1つの列はフィールドであり、他の列はボタンを含む必要があります。データテーブルの新しい行にボタンを追加します。未定義です

var appendLastCategory = function() { 
    var obj = { 
     id: 'jh2i4h34ubi43', 
     name: 'Lolo', 
    }; 
    $('#categoriesList').DataTable().row.add({ 
     name: obj.name, //First column 
     mRender: function() { //Second column 
      return "<a class='md-btn' onClick='deleteCategory(this, &quot;" + obj.id + "&quot;)'>Delete</a>"; 
     } 
    }).draw(); 
}; 

「削除」ボタンをクリックすると、「ID」値に「未定義」が表示されます。そしてなぜ私は最初のデータでテーブルを初期化し、 'mRender'をほとんど同じ方法で使用しているので、なぜ動作するのか分かりません。

var deleteCategory = function(element, id) { 
    console.log(id); 
    //Blah blah, all the code to delete on backend and remove the row 
}; 
+0

結果のマークアップで、結果のonClick属性のIDがリストされていることを確認できますか? 'deleteCategory(this、" jh2i4h34ubi43 ")' –

+0

私はコードで 'id'を焼いても、未定義を示し続けていることを確認できます。 –

答えて

0

私はこれが役立つことを願っ:応答のための

var deleteCategory = function(element, id) { 
 
    alert(id); 
 
    //Blah blah, all the code to delete on backend and remove the row 
 
}; 
 
var appendLastCategory = function() { 
 
    var obj = { 
 
    id: 'jh2i4h34ubi43', 
 
    name: 'Lolo', 
 
    }; 
 
    $('#categoriesList').DataTable().row.add({ 
 
    '0': obj.name, //First column 
 
    '1': "<a href='#' class='btn btn-info' role='button' onClick='deleteCategory(this, &quot;" + obj.id + "&quot;)'>Delete</a>", 
 
    }).draw(); 
 
}; 
 

 
$(function() { 
 
    appendLastCategory(); 
 
});
<script src="http://code.jquery.com/jquery-1.12.1.min.js"></script> 
 
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous"> 
 
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS" crossorigin="anonymous"></script> 
 

 
<link href="http://cdn.datatables.net/1.10.11/css/jquery.dataTables.min.css" rel="stylesheet"> 
 
<script src="http://cdn.datatables.net/1.10.11/js/jquery.dataTables.min.js"></script> 
 

 

 

 
<table id="categoriesList" class="display" cellspacing="0" width="100%"> 
 
    <thead> 
 
    <tr> 
 
     <th>name</th> 
 
     <th>button</th> 
 
    </tr> 
 
    </thead> 
 
    <tfoot> 
 
    <tr> 
 
     <th>name</th> 
 
     <th>button</th> 
 
    </tr> 
 
    </tfoot> 
 
    <tbody> 
 
    <tr> 
 
     <td>Tiger Nixon</td> 
 
     <td>Tiger Nixon</td> 
 
    </tr> 
 
    <tr> 
 
     <td>Garrett Winters</td> 
 
     <td>Tiger Nixon</td> 
 
    </tr> 
 
    <tr> 
 
     <td>Ashton Cox</td> 
 
     <td>Tiger Nixon</td> 
 
    </tr> 
 
    </tbody> 
 
</table>

0

ありがとう!私は解決しました。 DataTableを初期化する

、これは次のコードです:ちょうどこの方法ではJavaScriptオブジェクトを追加する解決しまった最後のすべてで

var datax = [{ 
     id: '67i67i634r2r', 
     name: 'Lala', 
    },{ 
     id: 'g45y45y657t3', 
     name: 'Lele', 
    }]; 
$('#categoriesList').DataTable({ 
    "data": datax, 
    "columns": [ 
     { data: "name" }, { 
      render: function(o, type, data) { 
       return "<a class='md-btn' onClick='deleteCategory(this, &quot;" + data.id + "&quot;)'>Delete</a>"; 
      } 
     } 
    ] 
}); 

。これがより多くの人々に役立つことを願っています。

var appendLastCategory = function() { 
    var obj = { 
     id: 'jh2i4h34ubi43', 
     name: 'Lolo', 
    }; 
    $('#categoriesList').DataTable().row.add(obj).draw(); 
}; 
関連する問題