0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

kintone × JavaScriptカスタマイズ で使える小技10選(コピペOKサンプルコード)

Last updated at Posted at 2025-09-30

環境

  • kintone
  • javascript

対象

  • javaScriptのカスタマイズも試したい人

前置き

kintoneのカスタマイズは基本的にプラグインが効率的ですが、対応できないケースもあります。
少量の調整には customine などのサービスは割高になることも。
そんなとき、JavaScriptの知識があれば柔軟に対応できる場面も多いです。

JavaScriptカスタマイズで便利なプラグイン

📌こちらのプラグイン入れておくと便利です。

🔧 1. フィールドにプレースホルダー(入力例)を表示する

    kintone.events.on(['app.record.create.show','app.record.edit.show'], function(event) {
      const fields = Object.values(cybozu.data.page.FORM_DATA.schema.table.fieldList)
      const field = fields.find(_ => _.var === '電話番号')
      const input = document.querySelector(`.value-${field.id} input`)
      if (input) {
        input.setAttribute('placeholder', '例: 090-1234-5678');
      }
    });

🔧 2. 保存前に空欄チェックしてアラート

    kintone.events.on('app.record.create.submit', function(event) {
      if (!event.record.担当者.value) {
        alert('担当者は必須項目です。');
        return false;
      }
    });

🔧 3. レコード保存前に文字数チェックを行う

    kintone.events.on('app.record.create.submit', function(event) {
      const text = event.record.備考.value || '';
      if (text.length > 20) {
        alert('備考は20文字以内で入力してください。');
        return false;
      }
    });

🔧 4. サブテーブル全行の背景色を変更

    kintone.events.on('app.record.detail.show', function(event) {
      const rows = document.querySelectorAll('.subtable-row-gaia');
      rows.forEach(row => {
        row.style.backgroundColor = '#f0f8ff';
      });
    });

🔧 5. 詳細画面で一部のフィールドを非表示に

    kintone.events.on('app.record.detail.show', function(event) {
      kintone.app.record.setFieldShown('顧客コード', false);
      return event;
    });

🔧 6. ステータスによって詳細画面のスペースの色を変える

  kintone.events.on('app.record.detail.show', function(event) {
    if (event.record.ステータス.value !== '緊急') return event;
    const spaceEl = kintone.app.record.getSpaceElement('HIGHLIGHT');
    if (spaceEl) {
      spaceEl.style.backgroundColor = '#ffeaea';  // 薄い赤
    }
    return event;
  });

🔧 7. ユーザーによって初期値を変える

    kintone.events.on('app.record.create.show', function(event) {
      const user = kintone.getLoginUser();

      // ユーザーによって優先度を変える
      if (user.code === 'tanaka') {
        event.record.優先度.value = '';
      } else if (user.code === 'suzuki') {
        event.record.優先度.value = '';
      } else {
        event.record.優先度.value = '';
      }
    
      return event;
    });

🔧 8. 日付を「年-月」だけに表示(詳細画面)

    kintone.events.on('app.record.detail.show', function(event) {
      const el = kintone.app.record.getFieldElement('契約日');
      const dateStr = event.record.契約日.value;
      if (dateStr) {
        const y = new Date(dateStr).getFullYear();
        const m = new Date(dateStr).getMonth() + 1;
        el.textContent = `${y}${m}月`;
      }
    });

🔧 9. 一覧画面で条件付きで行の背景を色分け

    kintone.events.on('app.record.index.show', function(event) {
      event.records.forEach((record, i) => {
        if (record.状態.value === '対応中') {
          const row = document.querySelectorAll('.recordlist-row-gaia')[i];
          row.style.backgroundColor = '#fffacc';
        }
      });
    });

🔧 10. モバイル端末でだけ要素を表示

    kintone.events.on('mobile.app.record.detail.show', function(event) {
      const el = kintone.mobile.app.getHeaderSpaceElement();
      const msg = document.createElement('div');
      msg.textContent = '📱 モバイル表示中';
      msg.style.padding = '4px';
      msg.style.backgroundColor = '#007bff';
      msg.style.color = '#fff';
      el.appendChild(msg);
    });

✅ まとめ

JavaScriptを使うことで、kintoneはもっと便利になります。
APIが公開されているのでより自分に必要なカスタマイズを見つけてみて下さい。

参考情報

0
2
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
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?