我使用选择二如下:如何在Select2中从数据源设置数据*?
$('select#fields').select2({
placeholder: 'Select a field',
data: data.fields
});
data.fields
是这样一个JSON:
"fields": [
{
"id": "companies_id",
"text": "companies_id",
"data-type": "int"
},
{
"id": "parent_companies_id",
"text": "parent_companies_id",
"data-type": "int"
},
{
"id": "client_of_companies_id",
"text": "client_of_companies_id",
"data-type": "int"
},
{
"id": "asset_locations_id",
"text": "asset_locations_id",
"data-type": "int"
},
{
"id": "companies_name",
"text": "companies_name",
"data-type": "varchar"
},
{
"id": "companies_number",
"text": "companies_number",
"data-type": "varchar"
}
]
id
和text
用于填充选项值,我可以使用第三值data-type
和将它设置为<option>
的属性?如果是这样如何?任何可以给我一个例子吗?
可能会尝试与[模板]的东西(https://select2.github.io /examples.html#templating)。 –
@lamelemon,实际上这里不需要模板。你可以检查我的答案:) – Dekel