1.1.1 • Published 8 years ago

angular.tableview v1.1.1

Weekly downloads
5
License
MIT
Repository
github
Last release
8 years ago

license experiments

AngularJS TableView (example)

A data grid for AngularJS.

Example

Table of contents

  1. Quick Start
  1. Reference API
  1. Complete online usage example

Quick Start

Dependencies

  • No dependencies

Downloading TableView using npm

AngularJS TableView is registered as a package on npm. You can install the latest version of TableView with the command:

npm install angular.tableview

This will install TableView in the node_modules directory. Within node_modules/angular.tableview/dist/ you will find a compressed release files.

Downloading TableView using Bower

AngularJS TableView is also registered as a package with Bower. You can install the latest version of TableView with the command:

bower install angular.tableview

This will install TableView to Bower's install directory, the default being bower_components. Within bower_components/angular.tableview/dist/ you will find a compressed release files.

Make sure to embed it in your HTML document:

<script src="path/to/angular.min.js"></script>
<script src="path/to/dist/angular.tableview.min.js"></script>
<!-- Required CSS --> <link href="path/to/dist/angular.tableview.min.css" rel="stylesheet" />
<!-- Theme CSS --> <link href="path/to/dist/angular.tableview.material.min.css" rel="stylesheet" />

Turn on tableview module in the your awesome application:

var application = angular.module("app", ["tableview"]);

Pass grid configuration via tableview attribute:

<div tableview="configuration"></div>

In the case if you need to completely change default template of tableview directive, you can define path to your template via tableview-template-url attribute:

<div tableview="configuration" tableview-theme="'material'" tableview-template-url="path/to/your/template.html"></div>

Directive attributes:

AttributeTypeDetails
tableviewrequired ObjectTableView instance configuration object
tableview-themeoptional StringThe name of TableView theme to be used here
tableview-template-urloptional StringPath to your custom template of the TableView

All that you needed is to define in your controller configuration and data provider function that receives current request object and callback function as arguments and should provide response object for the current request to the instance of TableView via calling of callback function by passing response object as argument.

Minimal application example:

[index.html]

<html ng-app="app">
  <head>
    <script type="text/javascript" src="angular.min.js"></script>
    <script type="text/javascript" src="angular.tableview.min.js"></script>
    <link rel="stylesheet" type="text/css" href="angular.tableview.min.css" />
    <script type="text/javascript" src="script.js"></script>
  </head>
  <body ng-controller="Ctrl">
    <h1>AngularJS TableView Example</h1>
    <div tableview="configuration"></div>
  </body>
</html>

[script.js]

angular
.module("app", ["tableview"]);
.controller("Ctrl", function ($scope) {

  $scope.configuration = {
    columns: [
      {field: "id"},
      {field: "name", title:"Name"},
      {field: "email", title:"Email"}
    ],
    provider: provider
  };

  function provider (request, callback) {
    asyncRequestToServer (request, function (response) {
      callback(response);
    });
  }

});

Reference API

REQUEST object structure

All properties of the REQUEST object are [optional]

PropertyTypeDetails
limitIntegerHow many entries should to be requested from the server
pageIntegerThe page number, that should to be requested from the server
likeObjectAn object that describes filtering by fields, where name of property is field name and value of property is search string
orderArrayAn array of objects that describes fields with sorting order by which should be sorted result entries
orderxObjectAn object that describes sorting order by the field
orderx.fieldStringThe property that defines field name by which should to be sorted result entries
orderx.sortingString ASC|DESCThe property that defines sorting order for the field name by which should to be sorted result entries (ASC or DESC)

Request object example:

var request = {
  limit: 10,
  page: 1,
  order: [
    {field:"id", sorting:"ASC"},
    {field:"name", sorting:"DESC"},
    {field:"email", sorting:"DESC"},
    // ...
  ],
  like: {
    "name": "John",
    "email": "smith@",
    // ...
  }
};

RESPONSE object structure

PropertyTypeDetails
limitIntegerLimit that was returned from the server to show results in the TableView. Used for correction of the mapping
pageIntegerThe page number, that was returned from the server. Used for correction of the mapping when it needed (when filters has been applied for example)
amountIntegerSummary amount of the records that available by the sent request. Used for calculation and building of the pager section
rowsArrayAn array of objects (entries) to display considering to request

Response object example:

var response = {
  limit: 10,
  page: 1,
  amount: 12345,
  rows: [
    { "id":123 , "name":"John", "email":"smith@gmail.com" },
 // {...}
  ]
};

CONFIGURATION object structure

All properties of the CONFIGURATION object except columns and provider are [optional]

PropertyTypeDetails
providerrequired FunctionAccepted arguments: (request, callback). The function that receives current request object and callback function as arguments and should provide response object for the current request to the instance of TableView via calling of callback function by passing response object as argument
columnsrequired ArrayArray of column Objects with definitions of visible columns
column.fieldoptional StringThe name of field in DB that can be used for the sorting and filtering logic. In additional it creates CSS class "column-{{field_name}}" that can be used for UI customisation
column.nameoptional StringAn unique name of the field that can be used for UI customisation from CSS via using of CSS class "column-{{name}}" when field is not defined
column.titleoptional StringThe column title
column.placeholderoptional StringPlaceholder for the filter input. Default: "Search..."
column.sortableoptional BooleanEnables sorting logic for the column. Requires field property. In additional it creates CSS classes "sortable" and "sortable-{{ascdesc}}" that can be used for UI customisation
column.filterableoptional BooleanEnables filtering logic for the column. Requires "field" property. In additional it creates CSS class "filterable" that can be used for UI customisation
column.editableoptional Object,BooleanEnables editable logic for the column. Requires "field" property. In additional it creates CSS class "editable" that can be used for UI customisation
column.editable.typeoptional StringAvailable values: text, textarea. Default: text. The type of HTML tag (input or textarea) that will be used in edition mode
column.editable.validateoptional FunctionAccepted arguments: (column, row, field, value). Should return an object: { status: Boolean, message: String }. The function that will be used for validation of the field before changing of the model in edition mode.
column.editable.changeoptional FunctionAccepted arguments: (column, row, field, value). The function that can be used for saving of changed fields on the server.
column.templateoptional ObjectThe list of templates that should to be replaced for the current column cell by using custom templates (see AngularJS ng-template). The name of property is reserved word, that used to define replacement area in TableView template file.
column.template"head.cell"optional StringAn identifier of template to be used as cell of table header
column.template"body.cell"optional StringAn identifier of template to be used as cell of table body
column.template"body.cell.edit"optional StringAn identifier of template to be used as cell of table body in edition (see: editable) mode
column.template"foot.cell"optional StringAn identifier of template to be used as cell of table footer. Requires of template["foot"] generic template implementation.
templateoptional ObjectThe list of templates that should to be replaced by using custom templates (see AngularJS ng-template). The name of property is reserved word, that used to define replacement area in TableView template file
template"head.cell"optional StringAn identifier of the generic template to be used as cell of table header
template"head.cell.select"optional StringAn identifier of the template that contains implementation of toggle selection logic for all rows on the page when selectable logic used
template"body.cell"optional StringAn identifier of the generic template to be used as cell of table body
template"body.cell.edit"optional StringAn identifier of the generic template to be used as cell of table body in edition (see: editable) mode
template"body.cell.select"optional StringAn identifier of the template that contains implementation of toggle selection logic for the current row when selectable logic used
template"foot"optional StringAn identifier of the generic template to be used as table header. Not implemented by default
template"pager"optional StringAn identifier of the generic template to be used as pager section of the table
template"pager.limit"optional StringAn identifier of the generic template to be used as pager limit section of the pager
template"pager.controls"optional StringAn identifier of the generic template to be used as pager limit controls section of the pager
template"pager.selection"optional StringAn identifier of the generic template to be used as selection controls section when selectableBy mode enabled
requestoptional ObjectInitial custom request object that can be used to provide stored request from previous user session
multisortingoptional BooleanTurns on multicolumns sorting logic
selectableByoptional StringTurns on rows selection logic by primary key field
scrollableoptional ObjectTurns on scrollable logic for the table area and allows to provide custom styles for scrollable area such as {maxHeight: "400px"}
limitsoptional ArrayDefault: 10, 25, 50, 100. Custom list of limit numbers
themeoptional StringThe name of the custom TableView theme to be used for the current instance (The material theme as example is available in dist/angular.tableview.material.min.css)

Configuration object example:

$scope.configuration = {
  template: {
    "head.cell": "your/custom/angular/template.name.html",
    "body.cell": "embed.to.the.view.angular.template.id",
  },
  columns: [
    {
      field: "field_name",
      name: "name",
      title: "Id",
      placeholder: "Filter placeholder string",
      sortable:true,
      filterable:false,
      editable: {
        type: "textarea",
        validate: fieldValidator,
        change: saveValidChangedField
      },
      template: {
        "head.cell": "your/custom/angular/template.name.html",
        "body.cell": "embed.to.the.view.angular.template.id",
      },
    },
    {
      field: "simple_editable_field",
      title: "Title",
      editable: true
    }
    // ...
  ],
  provider: dataProvider,
  request: {/* see `REQUEST` structure */},
  multisorting: false,
  limits: [10, 50, 100],
  scrollable: {
    maxHeight: "400px"
  },
  selectableBy: "id",
  theme: "material"
};

function dataProvider (request, callback) {
  callback(response);
}

function fieldValidator (column, row, field, value) {
  var status = typeof value == "string" && value.trim().length;
  return {
    message: status ? "" : "The field '" + column.title + "' can not be empty",
    status: status
  };
}

function saveValidChangedField (column, row, field, value) {
  console.log (
    arguments.callee.name + "(column, row, field, value) =>",
    field,
    "=",
    value,
    column,
    row
  );
}

$tableViewProvider configuration

To define global configuration for your application you can use $tableViewProvider.

For example:

var app = angular.module("app", ["tableview"]);

app.config(function($tableViewProvider){
  $tableViewProvider.theme = "material";
  $tableViewProvider.templateUrl = "path/to/your/complete/template.html";
  $tableViewProvider.template = {
    "head.cell": "your/custom/angular/template.name.html",
    // ...
  };
});

The list of configuration-dependency CSS classes that provided in the default template

CSS ClassAreaDetails
column-nametableview>.holder>table>thead|tbody>tr>th|tdTo identify current column by the columns configuration. The name is a value of column.name or column.field object property
filterabletableview>.holder>table>thead|tbody>tr>th|tdTo identify that is the column is the filterable by the columns configuration
sortabletableview>.holder>table>thead|tbody>tr>th|tdTo identify that is the column is the sortable by the columns configuration
sortable-asc|desctableview>.holder>table>thead|tbody>tr>th|tdTo identify the sorting order of the column
selectabletableview>.holder>table>thead|tbody>tr>th|tdFor customization of the selection column
selectedtableview>.holder>table>thead|tbody>trTo identify that the row is selected
scrollabletableviewTo identify that the table is scrollable
editabletableview>.holder>table>thead|tbody>tr>th|tdTo identify that the column is editable by the columns configuration
editiontableview>.holder>table>tbody>tr>th|tdTo identify that the cell is editable and in edition mode
invalidtableview>.holder>table>tbody>tr>th|tdTo identify that the cell is editable and in edition mode and has been invalidated by the field validator