![](/img/trans.png)
[英]How to dynamically add row to the jquery Datatable and refine the columns to show?
[英]How to Add Columns to Jquery DataTable Dynamically
我有一个学生费模块,我想明智地生成费用。 意味着为整个班级而不是特定学生产生费用。 DataTable
将如下所示..
|RegistrationNo | Name | AdmissionFee | TutionFee | SportsFee | Exam Fee| Discount |
------------------------------------------------------------------------------------
| 50020 | A | 1000 | 800 | 500 | 400 | 300 |
| 50021 | B | 1000 | 800 | 500 | 400 | 100 |
等等,全班......
问题是,该Fees
由学校确定,所以我还没有收费修复数量,如Transport Fee
可以被定义, Library Fee
可以定义和学校要可以定义任何其他费用。 所以这些费用名称来自FeeDefination
表。 现在我应该如何将这些费用作为属性添加到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的each
迭代器)并使用循环参数访问(例如)标题列。
例如:
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>');
});
希望这可以帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.