Spanish Total.js
Italiano
Documentazione Per agenti IA
Indice

Estensioni

Add your own server code and buttons to Tables with extension files.

.md Sito web

Questa pagina non è ancora tradotta. Stai leggendo l’originale.

In questa pagina

An extension is an HTML file that adds your own code to Tables: server code, for example an action that an action button calls, and browser code, for example a button in the record window.

Where extensions live

  1. Create a folder named extensions in the folder of the app.
  2. Put your extension files in it, for example extensions/orders.html.
  3. Restart Tables. It loads every .html file of the folder at the start.

Files whose name contains -bk or _bk are skipped, so you can keep a disabled copy next to the active one.

The parts of an extension file

An extension has up to two parts:

  • <script total>: server code. It runs once in Tables at the start, with the full Total.js 5 API, for example NEWACTION() and ON().
  • <script>: browser code. Tables adds it to the page of every user.
<script total>
    // Server: an action that the browser part calls
    NEWACTION('Orders|hello', {
        route: '+API ?',
        action: function($, model) {
            $.callback('Hello ' + (model.name || ''));
        }
    });
</script>

<script>
    // Browser: a button in the detail of every record
    ON('detail', function(meta) {
        meta.columns.push({ type: 'custom', html: '<button class="exec" data-exec="?/sayhello">Say hello</button>' });
        meta.plugin.sayhello = function() {
            let payload = meta.plugin.collect();
            meta.plugin.tapi('Orders|hello', payload, function(response) {
                alert(response);
            });
        };
    });
</script>

The button appears at the end of the fields of each record. Clicking it sends the values of the record to the server action, which answers with a text:

A record with the Say hello button added by an extension

React to saved records

Server code can listen to the operations of Tables. Put @ before the name of the operation:

<script total>
    ON('@Data|save', function($) {
        let model = $.payload;
        if (model.tableid === 't118h7') {
            // an order was saved
        }
    });
</script>

Call your action from an action button

A server action of an extension can be the Endpoint of an action: enter its name, for example Orders|approve, instead of an address. The action receives the data described in Hook and action data, and the text it returns is shown to the user.

<script total>
    NEWACTION('Orders|approve', {
        action: function($, model) {
            // model.id, model.data, model.user ...
            $.callback('Order approved');
        }
    });
</script>

All the events and helpers are in Extension reference.

Fatto in Europa