datatable-datalet.html 4.06 KB
<!--
@license
    The MIT License (MIT)

    Copyright (c) 2015 Dipartimento di Informatica - Università di Salerno - Italy

    Permission is hereby granted, free of charge, to any person obtaining a copy
    of this software and associated documentation files (the "Software"), to deal
    in the Software without restriction, including without limitation the rights
    to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    copies of the Software, and to permit persons to whom the Software is
    furnished to do so, subject to the following conditions:

    The above copyright notice and this permission notice shall be included in
    all copies or substantial portions of the Software.

    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    THE SOFTWARE.
-->

<link rel="import" href="../base-ajax-json-jsonpath-datalet/base-ajax-json-jsonpath-datalet.html">

<!--
The `datatable-datalet` is a porting of Datatables JQuery library in a web component that has built up by using Polymer.
Pass to this component a data url(CKAN api uri) and a string with one or multiple query in JsonPath format(separated by spaces) and it'll show a
table with data and query attributes with all Datatables library features.

Example:

    <datatable-datalet
        data-url="http://ckan.routetopa.eu/api/action/datastore_search?resource_id=#"
        fields='["field1","field2"]'>
    </datatables-datalet>

@element datatable-datalet
@status v0.1
@demo demo/index.html
@group datalets
-->

<dom-module id="datatable-datalet">
    <template>
        <link rel="stylesheet" href="js/DataTables-master/media/css/jquery.dataTables.min.css">

        <table id="datatable" class="table table-striped table-bordered" cellspacing="0" style="height: auto; width: auto;">
        </table>
        <base-ajax-json-jsonpath-datalet data-url="{{dataUrl}}" fields="{{fields}}"></base-ajax-json-jsonpath-datalet>
    </template>

    <script src="js/DataTables-master/media/js/jquery.dataTables.js"></script>

    <script>
        var DatatableBehavior = {

            presentData: function(){
                if(!this.data || this.data == undefined) return;
                html = "";
                html += '<thead>'+
                           '<tr>';
                for(var x = 0; x<this.data.length; x++){
                   html += '<th>' + this.data[x].name + '</th>';
                }
                html +=  '</tr>' +
                         '</thead>' +
                         '<tfoot>' +
                         '<tr>';
                for(var x = 0; x<this.data.length; x++){
                    html += '<th>' + this.data[x].name + '</th>';
                }
                html += '</tr>' +
                        '</tfoot>' +
                        '<tbody>';
                for(var i = 0; i<this.data[0].data.length; i++){
                    html += '<tr>';
                    for(var x = 0; x<this.data.length; x++){
                      html += '<td>' + this.data[x].data[i] + '</td>';
                    }
                    html += '</tr>';
                }
                html += '</tbody>';

                $(this._component.$.datatable).append(html);
                $(this._component.$.datatable).DataTable();
            }
        };

        Polymer({
            is : 'datatable-datalet' ,
            ready: function(){
                var DatatableComponentBehavior =  $.extend(true, {}, BaseDataletBehavior, WorkcycleBehavior, AjaxJsonJsonPathBehavior, DatatableBehavior);
                DatatableComponentBehavior.init(this);
            }

        });
    </script>
</dom-module>