从jquery datatable

时间:2016-09-13 11:43:00

标签: javascript jquery datatables

我是一个动态创建的数据表。行数因用户而异。有些可能会创建2行,有些可能会创建3等。例如,表格可能如下所示:

|     Category      |   User   |
|      Manager      |  Krishna |
| Assistant Manager |  Guru    |

将从选择下拉列表中选择它们。这就是我在单击提交按钮时尝试从表中检索所有数据的方法:

var noofrows = tabResources.data().count();
var data = tabResources.rows(0).data();
                alert(data);

在警报中,我得到[object, object]。但是,我不知道如何检索每个值。我怎样才能做到这一点?这是表格:

tabResources = $("#tabResources").DataTable({
                "sDom":"t",
                ajax:{
                    "url":"../api/projectapi.php?action=getresourcebyprojectid",
                    "type":"GET",
                    "data":{"projectid": <?php echo $_GET ["projectid"]?>}
                },
                "destroy":true,
                "ordering":false,
                "columns":[

                           {"title":"Resource Category", "data":"resourcecategoryid","render":function(data, type, row){
                               return '<select class="form-control selectresourcecategoryid" name="resourcecategoryid[]" valueid=' + data + '></select>';
                            }},
                            {"title":"User", "data":"userid","render":function(data, type, row){
                                   return '<select class="form-control selectuserid" name="userid[]" valueid=' + data + '></select>';
                                }},
                                {"orderable":false, render: function ( data, type, row ) {
                                    return '<a href="javascript:void(0)"  class="btn btn-info btn-sm btn-remove">Remove</a>';               
                                    } 
                                }
                ]
});

1 个答案:

答案 0 :(得分:0)

签出代码段,这可以解决您的问题吗?

&#13;
&#13;
$(document).ready(function() {  
    var data = [
       ['name1', 'position1', 'office1', 15],
       ['name2', 'position2', 'office2', 30]
    ];
    var table = $('#example').DataTable({
      data: data,
      columns: [
        {
          title: "Name"
        },{
          title: "Position"
        },{
          title: "Office"
        }
      ]
    });
 
    var data = table.data();
    for (var i = 0; i < data.length; i++) {
      console.log(data[i]);
    }
      
} );
&#13;
<script src="//code.jquery.com/jquery-1.12.3.js"></script>
<script src="https://cdn.datatables.net/1.10.12/js/jquery.dataTables.min.js"></script>
<link href="https://cdn.datatables.net/1.10.12/css/jquery.dataTables.min.css" rel="stylesheet"/>

<table id="example" class="display" cellspacing="0" width="100%"></table>
&#13;
&#13;
&#13;