Stop Datatables removing my row classes / attributes / data

Stop Datatables removing my row classes / attributes / data

doomsknightdoomsknight Posts: 6Questions: 1Answers: 0
edited October 2014 in Free community support

I am using MVC (C#), and datatables.

It is very easy and convinient for me to set data-href attribute on a table row.

And then, add a jquery onclick tr event to open the link.

However, datatables in all its frustration, removes these custom set attributes.

How can i stop it doing this...?

ON a side note, i tried setting:

    "fnRowCallback": function( nRow, aData, iDisplayIndex, iDisplayIndexFull ) {
                  // Bold the grade for all 'A' grade browsers

                    $('td:eq(4)', nRow).html( '<b>A</b>' );

                }

but it does not work. So that i could reattach (inconvieniently) the href/ data-href attribute back.

How do you disable this strange feature. And why is it even there.

Answers

  • doomsknightdoomsknight Posts: 6Questions: 1Answers: 0

    Ok, my function call back was in the wrong place.
    But i still dont have access to the data i want to set as link, as easily as MVC.
    I can put it in a hidden row then set the attribute from the hidden data, but this is just obsolete.

    Best to disable the destructive part of datatables.. but how?

  • allanallan Posts: 63,523Questions: 1Answers: 10,473 Site admin

    Can you link to the page showing the issue please (as noted in the forum rules). DataTables should not be removing any attributes.

    Allan

  • doomsknightdoomsknight Posts: 6Questions: 1Answers: 0

    Well now im confused...

    http://live.datatables.net/qocagiwu/1/edit?html,css,js,output

    I tried it in there and it works. However, the attributes in my production code ONLY dissapear when i call DataTable on the table. It has done this for months...
    Hence why I have to re-assign sClass to my columns.

      "aoColumnDefs": [
                { "sClass": "la", "aTargets": [1, 2] }
            ],
    

    The scenario with the data (any) attributes being removed is more troublesome in the required scenario for different data values per row.

    Im using MVC 4 .Net .
    Jquery 1.7.1
    Jquery UI 1.8.20
    Modernizor

    Any ideas what could be clashing.

  • allanallan Posts: 63,523Questions: 1Answers: 10,473 Site admin

    Not really I'm afraid. As I say, it should work, and it appears to be doing so in your test case. I would need an example that doesn't work as expected to be able to understand what is going wrong.

    Allan

  • doomsknightdoomsknight Posts: 6Questions: 1Answers: 0

    I have copied the table example above, into my project with its own references. Works fine.
    I then referenced the same things my project uses. And it still works fine.

    I have noticed however, that tr is not gaining the attributes: role and class. Now i Suspect what ever is adding theses is causing the issue with taking other and all parameters off.

       <tr role="row" class="odd"><td>
    

    Can you confirm please that datatables does not add these attributes.

  • doomsknightdoomsknight Posts: 6Questions: 1Answers: 0
    edited October 2014

    After lots of playing around, I've found that removing the line

            "deferRender": true,
    

    fixes the issue.

    However, i cannot replicate the same in the JS Bin. So am truely lost as to why this works.

    ( I commented out sections bit by bit and tested variations between the two )

  • allanallan Posts: 63,523Questions: 1Answers: 10,473 Site admin

    Can you confirm please that datatables does not add these attributes.

    I can't, because it does :-). The role is an ARIA attribute for accessibility and the class is for the row striping (because ::nth-child() isn't supported by all the browsers that DataTables supports).

    "deferRender": true,

    Deferred rendering will not make any difference in the example because the rows have already been rendered by the time DataTables reads them. It only makes any difference when you are using Ajax loaded data.

    However, it still should never remove attributes once the rows have been created.

    Allan

  • doomsknightdoomsknight Posts: 6Questions: 1Answers: 0
    edited October 2014

    Yes, those role and odd/even classes still get added, with no conflict. So they are not impacting it.

    I have been through all the tables i had issues with and removed the single line

      "deferRender": true,
    

    This has fixed all my tables, to no longer remove my attributes.

    I don't think MVC is loading it ajax either. The table is generated in full (with data), then the datatable script is run on it.

    I do know that attributes that are set to blank values, dissapear completely in MVC. However, i also set data-href="test" etc, so again, unsure where the error is creeping in, regarding deferRender.

    I can also point out, that set to true or false, the issue persists. Its only when you remove the line, that it is fixed.

    I do not understand as to why this fixes it. Just that it has. (For over 10 different tables)
    Hopefully no one else experiences this issue.

    Thanks for the help in identifying it. As well as pointing out that removing attributes is not the way datatable is supposed to work.

  • allanallan Posts: 63,523Questions: 1Answers: 10,473 Site admin
    edited October 2014

    Thanks for the update - good to hear you have this working now.

    For anyone else who reads this thread and does experience this issue, please link to a test case so I can debug it and provide some assistance.

    Allan

This discussion has been closed.