In Tabulator.js, how can I set the value of a cell to a string that contains angle brackets? e.g. "a<b<c"

huangapple go评论154阅读模式
英文:

In Tabulator.js, how can I set the value of a cell to a string that contains angle brackets? e.g. "a<b<c"

问题

我在我的Tabulator表格中有一个单元格,它使用了一个列表类型的编辑器。选项是由一个看起来像这样的字符串数组填充的(作为示例):

   [
   "a&lt;b",
   "b&lt;c",
   "c&lt;d",
   ]

当列表编辑器被填充时(在列定义中使用editorParams:{values:exampleOptions}),或者当我直接在单元格上使用setValue()时,它会删除尖括号之后的所有内容。所以在下拉列表中我看到类似这样的东西:

   "a",
   "b",
   "c",

如何将完整的字符串放入下拉列表/单元格中?

英文:

I have a cell in my Tabulator table that uses a list type editor. The options are populated by an array of strings that looks like (as an example):

   [
   &quot;a&lt;b&quot;,
   &quot;b&lt;c&quot;,
   &quot;c&lt;d&quot;,
   ]

When the list editor is populated (using editorParams:{values:exampleOptions} in the column definition), or when I use setValue() on the cell directly, it drops everything after the angle bracket. So I see something like this in the dropdown list:

   &quot;a&quot;,
   &quot;b&quot;,
   &quot;c&quot;,

How can I get the full string into that dropdown list/cell?

答案1

得分: 1

一种方法是在editorParams中使用itemFormatter选项来为值列表定义一个新的元素节点,并返回包含列表值的元素的innerHTML

editorParams: {
  values: ["a&lt;b", "b&lt;c", "c&lt;d"],
  itemFormatter: (label, value, item, element) => {
    const listNode = document.createTextNode(value);
    element.appendChild(listNode);

    return element.innerHTML;
  }
},

以下是在name列上的示例:

const data = [{
    id: 1,
    name: 'Billy Bob',
    age: '12',
    gender: 'male',
    height: 1,
    col: 'red',
    dob: '',
    cheese: 1
  },
  {
    id: 2,
    name: 'Mary May',
    age: '1',
    gender: 'female',
    height: 2,
    col: 'blue',
    dob: '14/05/1982',
    cheese: true
  },
  {
    id: 10,
    name: 'Margret Marmajuke',
    age: '16',
    gender: 'female',
    height: 5,
    col: 'yellow',
    dob: '31/01/1999'
  }
];

const table = new Tabulator('#example-table', {
  data: data,
  columns: [{
      title: 'Name',
      field: 'name',
      editor: "list",
      editorParams: {
        values: ["a&lt;b", "b&lt;c", "c&lt;d"],
        itemFormatter: (label, value, item, element) => {
          const listNode = document.createTextNode(value);
          element.appendChild(listNode);

          return element.innerHTML;
        }
      },
    },
    {
      title: 'Age',
      field: 'age'
    },
    {
      title: 'Gender',
      field: 'gender'
    },
    {
      title: 'Height',
      field: 'height'
    },
    {
      title: 'Favourite Color',
      field: 'col'
    }
  ]
});

name列上使用了上述示例。

英文:

One way would be to use the itemFormatter option in editorParams to define a new element node for the values list, and return the innerHTML of the element containing the list value:

editorParams: {
values: [&quot;a&lt;b&quot;, &quot;b&lt;c&quot;, &quot;c&lt;d&quot;],
itemFormatter: (label, value, item, element) =&gt; {
const listNode = document.createTextNode(value);
element.appendChild(listNode);
return element.innerHTML;
}
},

Here is an example on the name column:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const data = [{
id: 1,
name: &#39;Billy Bob&#39;,
age: &#39;12&#39;,
gender: &#39;male&#39;,
height: 1,
col: &#39;red&#39;,
dob: &#39;&#39;,
cheese: 1
},
{
id: 2,
name: &#39;Mary May&#39;,
age: &#39;1&#39;,
gender: &#39;female&#39;,
height: 2,
col: &#39;blue&#39;,
dob: &#39;14/05/1982&#39;,
cheese: true
},
{
id: 10,
name: &#39;Margret Marmajuke&#39;,
age: &#39;16&#39;,
gender: &#39;female&#39;,
height: 5,
col: &#39;yellow&#39;,
dob: &#39;31/01/1999&#39;
}
]
const table = new Tabulator(&#39;#example-table&#39;, {
data: data,
columns: [{
title: &#39;Name&#39;,
field: &#39;name&#39;,
editor: &quot;list&quot;,
editorParams: {
values: [&quot;a&lt;b&quot;, &quot;b&lt;c&quot;, &quot;c&lt;d&quot;],
itemFormatter: (label, value, item, element) =&gt; {
const listNode = document.createTextNode(value);
element.appendChild(listNode);
return element.innerHTML;
}
},
},
{
title: &#39;Age&#39;,
field: &#39;age&#39;
},
{
title: &#39;Gender&#39;,
field: &#39;gender&#39;
},
{
title: &#39;Height&#39;,
field: &#39;height&#39;
},
{
title: &#39;Favourite Color&#39;,
field: &#39;col&#39;
}
]
})

<!-- language: lang-html -->

&lt;link href=&quot;https://unpkg.com/tabulator-tables@5.5.1/dist/css/tabulator.min.css&quot; rel=&quot;stylesheet&quot; /&gt;
&lt;script type=&quot;text/javascript&quot; src=&quot;https://unpkg.com/tabulator-tables@5.5.1/dist/js/tabulator.min.js&quot;&gt;&lt;/script&gt;
&lt;div id=&quot;example-table&quot;&gt;&lt;/div&gt;

<!-- end snippet -->

答案2

得分: 0

&quot;&lt;&quot;替换为&quot;&amp;lt;&quot;解决了这个问题。

英文:

Solved it by replacing &quot;&lt;&quot; with &quot;&amp;lt;&quot;

huangapple
  • 本文由 发表于 2023年8月4日 04:59:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/76831566.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定