Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何向Jquery数据表动态添加列_Javascript_Jquery_Jquery Datatables - Fatal编程技术网

Javascript 如何向Jquery数据表动态添加列

Javascript 如何向Jquery数据表动态添加列,javascript,jquery,jquery-datatables,Javascript,Jquery,Jquery Datatables,我有一个学生费用模块,我想按课程生成费用。是指为整个班级而不是特定学生产生费用数据表如下所示 |RegistrationNo | Name | AdmissionFee | TutionFee | SportsFee | Exam Fee| Discount | ------------------------------------------------------------------------------------ | 50020 | A | 1000

我有一个学生费用模块,我想按课程生成费用。是指为整个班级而不是特定学生产生费用<代码>数据表如下所示

|RegistrationNo | Name | AdmissionFee | TutionFee | SportsFee | Exam Fee| Discount |
------------------------------------------------------------------------------------
|    50020      |   A  |     1000     |     800   |    500    |   400   |   300    |
|    50021      |   B  |     1000     |     800   |    500    |   400   |   100    |
等等,全班同学

问题是,
费用
由学校定义,因此我没有固定数量的费用,例如
交通费
可以定义,
图书馆费
可以定义,学校想要的任何其他费用都可以定义。因此,这些费用名称来自一个
feedefinition
表。现在,我应该如何将这些费用添加到
aoColumns
属性中。 我已经尝试了下面的代码

     var html = '[';
     var oTable = $('#GenerateFeeDataTable').dataTable({
                "bJQueryUI": true,
                "bServerSide": true,
                "bPaginate": false,
                "bFilter": false,
                "bInfo": false,
                "sAjaxSource": "/forms/StudentFee/studentfee.aspx/GenerateStudentFee?CampusId=" + campusId + "&ClassId= " + classId + '&Session=' + JSON.stringify(session) + "&FeeModeId=" + feeModeId,
                "fnServerData": function (sSource, aoData, fnCallback) {
                    $.ajax({
                        "type": "GET",
                        "dataType": 'json',
                        "contentType": "application/json; charset=utf-8",
                        "url": sSource,
                        "data": aoData,
                        "success": function (data) {
                            var data = data.d;
                            html += '{"sTitle":"Registration No","mDataProp":"RegistrationNo","bSearchable":false,"bSortable":false},';
                            html += '{"sTitle":"Student Name","mDataProp":"StudentName","bSearchable":false,"bSortable":false},';
                            html = html.substring(0, html.length - 1);
                            html += ']';
                            fnCallback(data);
                        }
                    });
                },
                "aoColumns": html
 });
无论我如何在
fnServerData
中使用
aoColumns
静态属性,但这些属性都不会被修复,我只是在尝试是否工作,但它不工作

My Questions are :
1) How to handle this situation, means how to add aoColumns dynamically.
2) How to get Header/Variables Name from JSON aaData, below is the Image to understand.


有没有任何方法可以完成这样的任务,有什么帮助?

对于这种情况,我建议您使用自定义HTML表,而不是使用jQuery DataTables。然后,您可以循环数据(使用jQuery的
每个
迭代器)并使用循环参数访问(例如)标题列

例如:

var data = data[0]; // access the first row only
$.each(data, function(k, v) {        // here k is an index and v is a value
    alert(k); // show the column's name in alert
    $('body').append('<table><tr><td>' + v.RegistrationNo + '</td></tr></table>');
}); 
var data=data[0];//仅访问第一行
$.each(数据,函数(k,v){//这里k是索引,v是值
alert(k);//在alert中显示列的名称
$('body')。追加(''+v.RegistrationNo+'');
}); 
希望这有帮助