1

The table has pagination and sorting functionality through datatables, but the css is not being applied. I am using rails 4.2.

I have all my css and image files loaded into vendor/assets/stylesheets and images, respectively.

I am using less for stylesheets. I import the datatables css in application.less

...
@import "dataTables.bootstrap.css";
@import "jquery.dataTables.css";

The table data is loaded by ajax and datatables is initialized on ajax success.

LeaderBoards.prototype.updateResults = function() {
    var valuesToSubmit = $(this.form).serialize();
    var that = this;
    $.ajax({
        url: $(this.form).attr('action'),
        type: "GET",
        data: valuesToSubmit,
        dataType: "html"
    }).success(function(response){
        that.resultBox.html(response);
        $('#leaderboard-table-test').DataTable( {
            searching: false,
            "iDisplayLength": 3,
            "bLengthChange": false
        });
    }).error(function(){
        var text = I18n.t('leaderboards.index.no_results_found');
        that.resultBox.html('<div class="no-results-found">'+ text +'</div>');
    });
};

Here is an example of a table I am trying:

<table id="table_id" class="display">
    <thead>
    <tr>
        <th>Column 1</th>
        <th>Column 2</th>
    </tr>
    </thead>
    <tbody>
    <tr>
        <td>Row 1 Data 1</td>
        <td>Row 1 Data 2</td>
    </tr>
    </tbody>
</table>

I can see datatables classes being added to this after the table is loaded. For example, it is wrapped inside a div:

<div id="#table_id_wrapper" class="dataTables_wrapper form-inline dt-bootstrap no-footer">

In the application.css, I see the @import statements for my datatables css imports, but the datatables css itself is not included.

4
  • do you see the datatables code in your compiled main.css? Commented Aug 3, 2016 at 20:36
  • @Polyov I added in some more info to address your comment. Commented Aug 3, 2016 at 21:09
  • 1
    if the code isn't appearing in application.css, then it's not being imported properly. try some different paths or formats for the @import statement. Commented Aug 3, 2016 at 22:06
  • I agree it is not being imported properly, but I can't figure out why. The css files are located in vendor/assets/stylesheets. Any folder directly under assets should be included in the load path by rails, so @import "file_name.css"; should be enough to work. I have tried other path formats and been unsuccessful as well. Commented Aug 4, 2016 at 0:42

1 Answer 1

1

I was able to figure out the problem. The DataTables CSS files were not being imported properly as suspected. The trick was to force them to be interpreted as less files instead of regular css.

I changed my import statement to @import (less) "jquery.dataTables.css"; and it was loaded properly.
Thanks to Polyov for helping me to arrive at this conclusion.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.