Skip to content

context.AddResponse

Overview

The "Server Script" returns an arbitrary client response.

Limitations

  1. Even if you specify the method "Href", the product's own behavior may take precedence in some cases, such as the screen transition after creating a child record with a create button in "Creation of linked item".

Syntax

context.AddResponse(method,target,value)

Parameters

Parameter Type Overview
method string Specify the operation type of the response. Refer to the method section below.
target string Specify the ID of the target element.
value object Specify the element or value.

Methods

Type Description
ReplaceAll Replace the element specified for target above with the element specified for value.
Set Store information in the form.

Return Value

There is no return value.

Usage Example 1

In the following example, the field for ClassC is replaced with \

Pleasanter\
.

context.AddResponse('ReplaceAll','#Results_ClassCField','<div class="field-normal" style="color:red">Pleasanter</div>');

Usage Example 2

In the following example, the following information is stored in the form ($p.data.MainForm).
【Information to store】

  • Column: NumA Column
  • Value: 123
context.AddResponse('Set','NumA',123);

Usage Example 3

In the following example, after the record is created, the screen transitions to the table with site ID 1234.

context.AddResponse('Href','','/items/1234/index');

Code Samples

1. Display an announcement in the guide when overdue records exist in the table

This is sample code that dynamically replaces the text of the guide with the ReplaceAll method.

When the index of a time-limited table is displayed, it gets the overdue records, and if there is at least one overdue record, it replaces the contents of the guide with the ReplaceAll method.

Notes

This sample supports version 1.5.0.0 and later.

CSS

Setting the following style makes the text color of the guide red.

1
2
3
.md-viewer * {
    color: red !important;
}

JavaScript

Condition: Before opening the page

// Run only on the index screen (index)
if (context.Action !== 'index') return;
// Minimal escaping, because the string is inserted as HTML
const escapeHtml = (s) =>
    String(s ?? '').replace(
        /[&<>"']/g,
        (c) =>
            ({
                '&': '&amp;',
                '<': '&lt;',
                '>': '&gt;',
                '"': '&quot;',
                "'": '&#39;',
            })[c],
    );
// Drop <id> and after from /items/<id>... to make a URL ending with /items/
const getItemsBaseUrl = () => {
    const url = String(context.Url ?? '');
    const m = url.match(/^(.*?\/items\/)/);
    return m ? m[1] : '';
};
// Get the overdue (Overdue) records
const getOverdueItems = () => {
    const param = {
        View: { Overdue: true, ColumnSorterHash: { IssueId: 'asc' } },
    };
    return Array.from(items.Get(context.SiteId, JSON.stringify(param)));
};
// Main process
// Get the overdue records
const overdueItems = getOverdueItems();
// Exit if there are no overdue records
if (overdueItems.length === 0) return;
// Generate the base URL
const baseUrl = getItemsBaseUrl();
// Generate the HTML for the list of links to the overdue records
const linksText = overdueItems
    .map((item) => {
        const id = item.IssueId;
        const title = escapeHtml(item.Title);
        return `- ### [${title}](${baseUrl}${id})`;
    })
    .join('\n');
const guideText = `
[md]
# There are overdue items. Please check them immediately.
## Target records
${linksText}
`;
// Generate the HTML for the guide area
const html = `
<div id="Guide">
    <div>
        <markdown-field class="is-disabled">
            <textarea class="md-text" id="guide-textarea" data-readonly="1">${guideText}</textarea>
        </markdown-field>
    </div>
</div>
`;
// Replace the guide area (#Guide)
context.AddResponse('ReplaceAll', '#Guide', html);