Code Monkey home page Code Monkey logo

angular-responsive-tables's Introduction

angular-responsive-tables

Make your HTML tables look great on every device. Compatible with AngularJS 1.3.4+.

Live Demo

Why?

Currently, browsers for mobile devices like smartphones doesn't do anything to have a proper presentation of tables, and then scrollbars will show up and ruin your design.

In the search of a solution to this problem I have found many different approaches. Some of them still rely on horizontal scrollbars. While I believe this layout could be useful for some use cases, I felt that a default solution should avoid horizontal scrollbars entirely. Then I came up with this highly reusable directive.

All this work is based on the following assumptions:

  • If it is flexible, then it would solve most problems, even ones not aimed by the library author's;
  • Focusing on the task of adding responsiveness, in order to accomplish a greater objective (easy to use tabular data);
  • Do work with a standard HTML table, not requiring any extraneous markup;
  • Do not change default tabular layout unless a smaller display is detected;
  • Provide convenience without sacrificing flexibility;
  • By keeping code base simple, it is easier to reason about and evolve;
  • By fully covering with tests, it can evolve without introducing bugs.

Features

  • Angular native implementation compatible with 1.3.4+;
  • Keep things DRY;
  • Supports static and dynamic (ng-repeat) rows;
  • Supports conditionally shown (ng-if) columns;
  • Supports dynamic headers (ng-repeat);
  • Supports nested tables (responsive or not in their own right);
  • Easy to apply any style on top of it;
  • Works with any base CSS framework;
  • Should integrate seamlessly with any table component you might choose to use.

Future Work

  • Choose what columns to show/hide according to a given screen resolution;
  • Choose when it would be best to hide columns or collapse all columns;
  • Define a header and/or custom template for collapsed columns/row;
  • Allow collapse/expand column details.

Usage

<table wt-responsive-table>
    <tr>
        <th>Column 1</th>
        <th>Column 2</th>
        <th>Column 3</th>
        <th>Column 4</th>
    </tr>
    <tr>
        <td>...</td>
        <td>...</td>
        <td>...</td>
        <td>...</td>
    </tr>
    <tr>
        <td>...</td>
        <td>...</td>
        <td>...</td>
        <td>...</td>
    </tr>
    <tr>
        <td>...</td>
        <td>...</td>
        <td>...</td>
        <td>...</td>
    </tr>
</table>

Directives

wt-responsive-table

  • table: wt-responsive-table
    • td: responsive-omit-title: title should be ommited
    • td: responsive-omit-if-empty: no row for empty cells
    • td: data-title: use to override the header for a given row/cell

Installation

npm

npm install --save angular-responsive-tables

Bower

bower install angular-responsive-tables --save

Application

HTML

<link rel="stylesheet" href="release/angular-responsive-tables.min.css">
<script type="text/javascript" src="release/angular-responsive-tables.min.js"></script>

JavaScript

var app = angular.module('app', ['wt.responsive']);

Special cases

Header doesn't appear for a row / need to override header

It's possible to override a header with a data-title attribute:

<tr>
    <td data-title="column 1">tom</td>
    <td data-title="column 2">jerry</td>
</tr>

Changes to header text doesn't reflect in responsive mode

This is by design. To avoid expensive digest cycles only the content from the first digest cycle is used. There are no watchers being setup.

Dynamic column names

When loading column names with an asynchronous task, that is, column names are not available when first compiling the table element, rows in responsive mode won't have headers even after the task completes.

To avoid this problem, use an ng-if to conditionally present the element on screen.

<table wt-responsive-table ng-if="columnNames && columnNames.length">

IE9 responsive hack

Because IE9 doesn't handle correctly a display CSS rule for <td>, if you need to support it, you can use the following style, only for IE9:

<!--[if IE 9]>
<style>
/* rules for IE9 only */
.responsive {
    overflow: hidden;
}
.responsive td:nth-child(odd), .responsive td:nth-child(even) {
    float: left;
    width: 100%;
}
</style>
<![endif]-->

Credits

CSS based on original work by Chris Coyier (http://css-tricks.com/responsive-data-tables/). In this article, he covers approaches to responsive tables. I modified it to work around CSS specificity and to keep things DRY.

License

MIT

angular-responsive-tables's People

Contributors

awerlang avatar fujiunsyu avatar somanathreddy avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

angular-responsive-tables's Issues

"Error: th is null" when some TD's are mutually exclusive

A very simple example to reproduce the error ("Error: th is null") in the console:

<!DOCTYPE html>
<html ng-app="test">
    <head>
        <!-- angular -->
        <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular.min.js"></script>
        <!-- angular responsive table -->
        <script src="https://cdn.rawgit.com/awerlang/angular-responsive-tables/master/release/angular-responsive-tables.min.js"></script>
        <link rel="stylesheet" href="https://cdn.rawgit.com/awerlang/angular-responsive-tables/master/release/angular-responsive-tables.min.css">
    </head>
    <body>
        <table data-wt-responsive-table>
            <thead>
                <tr>
                    <th>column</th>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <td data-ng-if="true">tom</td>
                    <td data-ng-if="false">jerry</td>
                </tr>
            </tbody>
        </table>
        <script>angular.module("test", ['wt.responsive'])</script>
    </body>
</html>

Filippo

TypeError: tableCtrl.contains is not a function

It seems that last version introduced a runtime failure:

Repro: http://blog.werlangtecnologia.com.br/angular-responsive-tables/examples/

TypeError: tableCtrl.contains is not a function
    at link (http://blog.werlangtecnologia.com.br/angular-responsive-tables/release/angular-responsive-tables.js:93:32)
    at invokeLinkFn (http://blog.werlangtecnologia.com.br/angular-responsive-tables/bower_components/angular/angular.js:8258:9)
    at nodeLinkFn (http://blog.werlangtecnologia.com.br/angular-responsive-tables/bower_components/angular/angular.js:7768:11)
    at compositeLinkFn (http://blog.werlangtecnologia.com.br/angular-responsive-tables/bower_components/angular/angular.js:7117:13)
    at compositeLinkFn (http://blog.werlangtecnologia.com.br/angular-responsive-tables/bower_components/angular/angular.js:7120:13)
    at compositeLinkFn (http://blog.werlangtecnologia.com.br/angular-responsive-tables/bower_components/angular/angular.js:7120:13)
    at nodeLinkFn (http://blog.werlangtecnologia.com.br/angular-responsive-tables/bower_components/angular/angular.js:7763:24)
    at compositeLinkFn (http://blog.werlangtecnologia.com.br/angular-responsive-tables/bower_components/angular/angular.js:7117:13)
    at nodeLinkFn (http://blog.werlangtecnologia.com.br/angular-responsive-tables/bower_components/angular/angular.js:7763:24)
    at compositeLinkFn (http://blog.werlangtecnologia.com.br/angular-responsive-tables/bower_components/angular/angular.js:7117:13)

Version: 0.4.1
Angular: >=1.3.4 <1.4.0
Browsers: Chrome 54, Firefox 49

Make it work with Bootstrap

Bootstrap, specially by means of table class, overwrite some that makes tables break when the directive is applied.

As this directive is a complement to CSS frameworks, they both must play together.

wt-responsive-table directive not working if under a container with ng-if

If I have a nested table element in a div which contains ng-if directive the wt-responsive-table will not add the responsive class to the table. You have to add this manually. This happens because the variable that is used in the ng-if changes value in the controller from false to true and the wt-reponsive-table directive has already activated. Is there a solution to this?
<div ng-if=......>
<table wt-responsive-table>
....
</table>
</div>

....

P.S. The problem if you add the responsive class manually is that you will not get the complete functionality from the directive, for example the title of the column will be empty.

Allow th with ng-repeat over data from promise/remote

Would be great if you could have dynamic headers with the dynamic content. Dynamic headers are especially useful for user defined content and this currently doesn't work due to timing I assume (th are not yet present when directive compiles...)

Need examples With Huge Export Options For Reference

Able to find huge examples for datatables..However unable to find huge examples for this particular project with huge export options...

May be the examples can be part of the examples folder...Best would be single HTML with huge export options for quick understanding.

Example,

https://github.com/baksoy/datatables

https://datatables.net/extensions/buttons/examples/html5/simple.html

https://datatables.net/extensions/buttons/examples/initialisation/export

https://github.com/hhurz/tableExport.jquery.plugin

https://jsfiddle.net/gr5er1pj/1/

https://codepen.io/GiSmo/pen/pbjzXw

http://plnkr.co/edit/Yz9qYytTkD3UnjrW9J49?p=preview

http://technoscripts.com/export-html-table/

colspan/rowspan in responsive mode

Have a look at this code, in responsive mode (taken from w3schools, example on rowspan):

  <tr>
    <th>Month</th>
    <th>Savings</th>
    <th>Savings for holiday!</th>
  </tr>
  <tr>
    <td>January</td>
    <td>$100</td>
    <td rowspan="2">$50</td>
  </tr>
  <tr>
    <td>February</td>
    <td>$80</td>
  </tr>

What do you think about it? Should the third th be shown also in the second row? Maybe yes, but I'm not so sure.

Column names not showing when setting columns dynamically

Hello,

I made a directive and put the your table in it (a table with 'wt-responsive-table' directive), but I am setting the columns dynamically with ng-repeat on a list of column names. When I size down the screen (to 'responsive mode'), the column names don't appear. Here's part of the code I used:

var table = $('div.table-holder > table');
var thead = $('<thead></thead>');
var tr = $('<tr></tr>');

for(var i in $scope.columnNames)
   tr.append('<th>' + $scope.columnNames[i] + '</th>');

I prepend the thead to the table, afterwards.

Headercolumn translation within data-title

Currently I'm using "ngTasty" to get a proper table (server side) of my data. Now I need to translate the header columns for mobile devices. For the reponsive part, I use your angular-responsive.

The translation within the "normal" header cols works:

<thead>
                        <th class="mobileview"  ng-repeat="h in header.columns" > {{ h.name | uppercase | translate:translationData }}</th>
                      </thead>

Nothing works, to get the translated column names for smaler devices, i.e.:

<td data-title="'NAME' | translate">

or

<td data-title="translate['PREIS']">{{ row.Preis | currency }}</td>

My entire table looks like this:

<div tasty-table bind-filters="filters" bind-resource-callback="getResource" bind-init="init">
                    <table wt-responsive-table class="table table-striped table-condensed">
                      <thead>
                        <th class="mobileview"  ng-repeat="h in header.columns" > {{ h.name | uppercase | translate:translationData }}</th>
                      </thead>
                      <!-- <th class="mobileview" ng-repeat="h in header.columns" style="display:none;"> {{h.name}} </th> -->
                      <tbody>
                        <tr ng-repeat="row in rows">
                          <!-- <td>{{ row.ID }}</td> -->
                          <td>
                            <span action-button color="btn-success" itemid="row.ID" type="'recipe'"></span>
                          </td>
                          <td data-title="'NAME' | translate">{{ row.Name }}</td>
                          <td data-title="DosierungMin">{{row.DosierungMin}}%</td>
                          <td data-title="DosierungMax">{{row.DosierungMax}}%</td>
                          <td data-title="Dosierung">
                            <!-- <p ng-if="flavor.DosierungMax > 0"> -->
                              <span ng-if="row.DosierungMax == 0" class="animate-if label label-success">
                                <i class="fa fa-times fa-fw" aria-hidden="true"></i>
                              </span>
                              <span ng-if="row.DosierungMax < 6 && row.DosierungMax > 0" class="animate-if label label-primary">
                                <i class="fa fa-arrow-down fa-fw" aria-hidden="true"></i>
                              </span>
                              <span ng-if="row.DosierungMax >= 6 && row.DosierungMax < 10" class="animate-if label label-warning">
                                <i class="fa fa-arrows-h fa-fw" aria-hidden="true"></i>
                              </span>
                              <span ng-if="row.DosierungMax >= 10" class="animate-if label label-danger">
                                <i class="fa fa-arrow-up fa-fw" aria-hidden="true"></i>
                              </span>
                            <!-- </p> -->
                          </td>
                          <!-- <td>{{ row.DosierungMin  | volumne }}</td> -->
                          <!-- <td>{{ row.DosierungMax }}mg</td> -->
                          <td data-title="Reifezeit">{{ row.Reifezeit }} {{ 'DAYS' | translate:translationData }}</td>
                          <td data-title="Menge">{{ row.Menge  | volumne }}</td>

                          <td data-title="translate['PREIS']">{{ row.Preis | currency }}</td>
                          <td data-title="Preis100ml">{{ row.Preis100ml | currency }}</td>
                          <td data-title="Hersteller">{{ row.Hersteller }}</td>
                          <!-- <td>{{ row.Herstellungsdatum | amUtc | amDateFormat:'L' }}</td> -->

                          <!-- <td>{{ row.Bewertung }}</td> -->
                          <td data-title="Eigenschaften" ng-bind-html="row.Eigenschaften"></td>
                        </tr>
                      </tbody>
                    </table>
                    <div tasty-pagination bind-list-items-per-page="listItemsPerPage"></div>
                  </div>

Screenshots:

  1. Working "unresponsive" on larger screens:

bildschirmfoto 2016-08-02 um 09 20 53

  1. Not working on mobile:

bildschirmfoto 2016-08-02 um 09 21 00

Do you have a clue?

Support Angular 2+

Should this be a monorepo, supporting both Angular 1 and 2+?
How to share the CSS code, given that only the JS code would have different implementations?

Work with static tables

Should work when table has more than one <tr> element. Most of the time it would be a static table (i.e. no ng-repeat), but it could be anything really.

Support td[colspan]

For instance

<table>
<thead>
  <tr>
    <th>First title</th>
    <th>Second title</th>
    <th>Third title</th>
    <th>Forth title</th>
  </tr>
</thead>
<tbody>
  <tr>
    <td colspan="3">This cell spans for 3 columns</td>
    <td>Forth column</td>
  </tr>
  <tr>
    <td>First column</td>
    <td>Second column</td>
    <td>Third column</td>
    <td>Forth column</td>
  </tr>
</tbody>
</table>

On mobile, the last cell in the first row should be displayed with the title Forth column.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.