我正在尝试在Selectize中实现Remote Source。
我从api访问数据。格式如下:
concept_id,name
228,Pelecypoda
286,Pelecypoda
当我尝试在渲染中控制项目时,它并没有得到安慰。但是当我成功控制数据时,数据就会得到安慰。
$('#search_text').selectize({
theme: 'name',
valueField: 'url',
labelField: 'name',
searchField: 'name',
options: [],
create: false,
render: {
option: function(item, escape) {
console.log(item);
return '<div>' +
'<span class="title">' +
'<span class="by">' + escape(item.name) + '</span>' +
'</span>' +
'<span class="description">' + escape(item.concept_id) + '</span>' +
'</div>';
}
},
score: function(search) {
var score = this.getScoreFunction(search);
return function(item) {
return score(item) * (1 + Math.min(item.watchers / 100, 1));
};
},
load: function(query, callback) {
if (!query.length) return callback();
$.ajax({
url: "http://192.241.245.176:4567/api1",
dataType: 'text',
type: "GET",
data: {
q: query,
page_limit: 10
},
error: function() {
callback();
},
success: function(res) {
var result = {};
result.name = d3.csv.parse(res);
console.log(result);
callback(result.name);
}
});
}
});
任何人都可以告诉我哪里出错了?
答案 0 :(得分:0)
我认为有一个关键的价值标准被强制,但文档并没有非常彻底地涵盖它。它在那里被提到过一次,但没有进一步讨论。
您必须使用名称和值的属性从服务器返回结果:
名=&GT; namedata
值=&GT; valuedata
Cliffnote:$ items [$ i] ['name'] ['id']
Cliffnote:$ items [$ i] ['value'] [$ key]
在我的案例中构建我的结果看起来像这个PHP代码。
$group['location'] = range(0,5);
//'street',
//'city',
//'county',
//'state',
//'country',
//'country2',
$group['property'] = range(0,4);
//'title',
//'property_sub_type',
//'subdivision_name',
//'sub_cat',
//'interior_features',
$group['school'] = range(0,2);
//'junior_high_School',
//'middle_school',
//'high_school',
$group['mls'] = range(0,1);
//'mls_area'
//'mls_id#'
$items = array();
for ($i = 0; $i < count($res); $i++)
{
$items[$i]['name']['id'] = array_shift($res[$i]);
$offset = 0;
foreach ($group as $key => $val)
{
$items[$i]['value'][$key] = array_filter(array_slice($res[$i], $offset, count($group[$key])));
$offset += count($group[$key]);
}
}
产生这种数据结构
Array
(
[0] => Array
(
[name] => Array
(
[id] => 119
)
[value] => Array
(
[location] => Array
(
[street] => 25700 Cliff Cir
[city] => Spicewood
[county] => Travis
[state] => Texas
)
[property] => Array
(
[property_sub_type] => Single Lot
[subdivision_name] => Lakecliff On Lake Travis Sec 01
)
[school] => Array
(
[junior_high_School] => N/A
[middle_school] => Marble Falls
[high_school] => N/A
)
[mls] => Array
(
[mls_area] => 12
[mls_id#] => 4434048
)
)
)