最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to Add Columns to Jquery DataTable Dynamically - Stack Overflow

programmeradmin1浏览0评论

I have a student Fee module, and I want to Generate Fee Class wise. Means Generate Fee for a whole Class, not for a Specific Student. DataTable will look like below..

|RegistrationNo | Name | AdmissionFee | TutionFee | SportsFee | Exam Fee| Discount |
------------------------------------------------------------------------------------
|    50020      |   A  |     1000     |     800   |    500    |   400   |   300    |
|    50021      |   B  |     1000     |     800   |    500    |   400   |   100    |

so on, whole class...

The problem is that the Fees are defined by school, so i have not a fix number of fees, e.g Transport Fee can be defined, Library Fee can be defined and any other fee that school wants can define. so these Fee Names e from a FeeDefination Table. Now how i should add these Fees to aoColumns as attribute. I have try the below code...

     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
 });

How ever i have taken aoColumns attributes static in fnServerData, but these will not be fixed, i am just trying that i will work or not, but its not working..

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.

Is there any way to do such task, Any Help..

I have a student Fee module, and I want to Generate Fee Class wise. Means Generate Fee for a whole Class, not for a Specific Student. DataTable will look like below..

|RegistrationNo | Name | AdmissionFee | TutionFee | SportsFee | Exam Fee| Discount |
------------------------------------------------------------------------------------
|    50020      |   A  |     1000     |     800   |    500    |   400   |   300    |
|    50021      |   B  |     1000     |     800   |    500    |   400   |   100    |

so on, whole class...

The problem is that the Fees are defined by school, so i have not a fix number of fees, e.g Transport Fee can be defined, Library Fee can be defined and any other fee that school wants can define. so these Fee Names e from a FeeDefination Table. Now how i should add these Fees to aoColumns as attribute. I have try the below code...

     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
 });

How ever i have taken aoColumns attributes static in fnServerData, but these will not be fixed, i am just trying that i will work or not, but its not working..

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.

Is there any way to do such task, Any Help..

Share Improve this question asked Sep 14, 2013 at 9:51 Shahid IqbalShahid Iqbal 2,1359 gold badges31 silver badges53 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 4

Instead of using jQuery DataTables for this situation, I suggest you use custom HTML tables. Then you can loop through the data (using jQuery's each iterator) and access (e.g.) the header columns by using loop parameters.

For example:

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>');
}); 

Hope this helps.

发布评论

评论列表(0)

  1. 暂无评论