0
1

More than 3 years have passed since last update.

【kintone】サブテーブルの行を自動追加・削除する

Posted at

やりたいこと

数値フィールドに入力された値を元にサブテーブルの行を追加したり削除したりしたい

フィールドとフィールドコード

タイプ フィールド名 フィールドコード
数値 テーブルの行数 rowCount
テーブル テーブル table
文字列(1行) 文字列(1行) string

スクリーンショット 2021-08-29 14.00.40.png

コード

sample.js
(function() {
    "use strict";
    // rowCountフィールドが変更されたときに動かす
    const events = ["app.record.create.change.rowCount",
                    "app.record.edit.change.rowCount"];

    kintone.events.on(events, function(event) {
        const record = event.record;
        const rowCount = record.rowCount.value;
        const tableLength = record.table.value.length;
        if (rowCount > tableLength) {
            // 行を増やす処理
            for (let i = 0; i < (rowCount - tableLength); i++) {
                record.table.value.push({
                    "value": {
                        "string": {
                            //追加した行数を確認するためにvalueに数字を設置セットする
                            "value": i + 1,
                            "type": "SINGLE_LINE_TEXT"
                        }
                    }
                });
            }
        } else if (rowCount < tableLength) {
            // 行を減らす処理
            record.table.value.splice(rowCount, tableLength - rowCount);
        }
        return event;
    });
})();

できた

ezgif.com-gif-maker.gif

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1