我使用選擇二如下:如何在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