如何将内联 HTML 字段添加到 Suitelet 以保存 html 标记?

How do I add an inline HTML field to a Suitelet to hold html markup?

编辑:代码已更新 我正在尝试关注这个博客post,它展示了如何创建一个具有格式化 table https://followingnetsuite.com/2020/10/16/skip-freemarker-by-delivering-saved-search-results-in-a-suitelet/

的 Suitelet

博客 post 引用了添加内联 html 字段来保存 html 标记。尽管我知道我已经走了很远,但我已尝试将其添加到代码中:


    /**
     *@NApiVersion 2.x
     *@NScriptType Suitelet
     */

define(["N/search", "N/ui/serverWidget"], function (search, ui) {
  function onRequest(context) {
    if (context.request.method === "GET") {
      var form = ui.createForm({ title: "freemarker test" });

      function getIssues() {
        var issues = new Array();

        var mySearch = search.load({
          id: "customsearchcustomerallview",
        });

        var myPages = mySearch.runPaged({ pageSize: 1000 });

        for (var i = 0; i < myPages.pageRanges.length; i++) {
          var myPage = myPages.fetch({ index: i });
          myPage.data.forEach(function (result) {
            var issue = {};
            mySearch.columns.forEach(function (col, index) {
              issue["column_" + index] = {
                label: col.label,
                text: result.getText(col),
                value: result.getValue(col),
              };
            });
            issues.push(issue);
          });
        }
        return issues;
      }
      function formatIssues(issues) {
        var html = new Array();

        html.push('<table class="RPT">');
        html.push("<thead>");

        if (issues.length > 0) {
          var issue = issues[0];

          html.push("<tr>");
          for (var i = 0; i < 20; i++) {
            if (issue.hasOwnProperty("column_" + i)) {
              var sortType = isNaN(
                issue["column_" + i].text || issue["column_" + i].value
              )
                ? "string"
                : "float";
              html.push(
                '<th data-sort="' +
                  sortType +
                  '">' +
                  issue["column_" + i].label +
                  "</th>"
              );
            }
          }
          html.push("</tr>");
        }

        html.push("</thead>");
        html.push("<tbody>");

        issues.forEach(function (issue) {
          html.push("<tr>");
          for (var i = 0; i < 20; i++) {
            if (issue.hasOwnProperty("column_" + i)) {
              var vAlign = isNaN(
                issue["column_" + i].text || issue["column_" + i].value
              )
                ? "left"
                : "right";
              html.push(
                '<td align="' +
                  vAlign +
                  '">' +
                  (issue["column_" + i].text || issue["column_" + i].value) +
                  "</td>"
              );
            } else {
              break;
            }
          }
          html.push("</tr>");
        });

        html.push("</tbody>");
        html.push("</table>");

        return html.join("\n");
      }
      var htmlField = html.addField({
        id: "custpage_html",
        label: "html",
        type: ui.FieldType.INLINEHTML,
      });
      htmlField.defaultValue = formatIssues(getIssues());
      context.response.writePage(form);
    }
  }

  return {
    onRequest: onRequest,
  };
});

虽然我看不到添加它的正确方法。我在哪里添加内联 html 字段?

对于 Suitelet,'context.response.writePage(form)' 是否需要位于其余代码的末尾? (即在与 html 标记相关的函数之后?

谢谢

您通过从表单对象调用 Form.addField 将字段添加到您的表单:

var field = html.addField({
    id : 'custpage_inlineresults',
    type : serverWidget.FieldType.INLINEHTML,
    label : 'Search Results'
});

参见 SuiteAnswer #43669。

通过内联 HTML 字段的默认值 属性 添加您的 HTML。 按如下方式构建您的脚本:

    /**
    *@NApiVersion 2.x
     *@NScriptType Suitelet
     */
define(["N/search", "N/ui/serverWidget"], function (search, ui) {
  function onRequest(context) {
    if (context.request.method === "GET") {
      var form = ui.createForm({ title: "freemarker test" });

      function getIssues() {
        var issues = [];
        return issues;
      }

      function formatIssues(issues) {
        var html = [];
        return html.join("\n");
      }

      var htmlField = form.addField({
        id: "custpage_html",
        label: "html",
        type: ui.FieldType.INLINEHTML,
      });
      htmlField.defaultValue = formatIssues(getIssues());
      context.response.writePage(form);

    }
  }

  return {
    onRequest: onRequest,
  };
});

或者,如果您不需要任何其他 NetSuite 表单元素:

    /**
    *@NApiVersion 2.x
     *@NScriptType Suitelet
     */
define(["N/search"], function (search) {
  function onRequest(context) {
    if (context.request.method === "GET") {

      function getIssues() {
        var issues = [];
        return issues;
      }

      function formatIssues(issues) {
        var html = [];
        return html.join("\n");
      }

      context.response.write(formatIssues(getIssues()));

    }
  }

  return {
    onRequest: onRequest,
  };
});