Mit*_*ave 9 javascript php ajax jquery selectize.js
我正在使用Jquery的Selectize标记库,这对我来说效果很好.
以下是我使用过的代码.
Javascript代码:
$('#q').selectize({
plugins: ['remove_button'],
valueField: 'address',
labelField: 'address',
searchField: 'address',
create: true,
render: {
item: function(data, escape) {
return '<div>' + escape(data.address) + '</div>';
}
},
onChange: function(value) {
$(".selectize-input input[placeholder]").attr("style", "width: 100%;");
},
load: function(query, callback) {
if (!query.length) return callback();
$.ajax({
url: base_url + '/search-property-autocomplete',
type: 'POST',
dataType: 'json',
data: {
q: query,
},
error: function() {
callback();
},
success: function(res) {
console.log(res.properties);
callback(res.properties);
}
});
}
});
Run Code Online (Sandbox Code Playgroud)
PHP代码:
/* API for autocomplete list of properties */
Route::post('/search-property-autocomplete', function () {
if (!empty(trim($_POST['q']))) {
$search_term = trim($_POST['q']);
// getting Suburb State and Postcode of the properties based on search
$query = Property::join('technobrave_suburbs_', function($join) {
$join->on('technobrave_properties_.suburb_id', '=', 'technobrave_suburbs_.id');
});
$query->join('technobrave_states_', function($join) {
$join->on('technobrave_properties_.state_id', '=', 'technobrave_states_.id');
});
$query->select('technobrave_properties_.*', 'technobrave_suburbs_.suburb', 'technobrave_states_.state_name');
$query->where(function($query) use ($search_term) {
$query->where('technobrave_properties_.post_code', 'LIKE', '%' . $search_term . '%');
$query->orwhere('technobrave_suburbs_.suburb', 'LIKE', '%' . $search_term . '%');
$query->orwhere('technobrave_states_.state_name', 'LIKE', '%' . $search_term . '%');
});
$data = $query->take(8)->get(); // getting maximum 8 records only
if ($data) {
foreach ($data as $current_record) {
$result[] = array(
'address' => $current_record->suburb . ' ' . $current_record->state_name . ' ' . $current_record->post_code
);
}
}
} else {
$result = [];
}
echo json_encode(array('properties' => $result));
});
Run Code Online (Sandbox Code Playgroud)
正如你在上面的代码中看到的,我正在使用Ajax来填充数据并通过调用我的php函数来获取记录.
现在,我想在我的所有结果的顶部添加一个项目作为超链接,例如Clear All,每当我搜索或输入我的输入框时都会出现.
如果我点击Clear All链接,则应清除下面附加的结果.
要使用clearoptions()Selectize提供的活动,我已create:在我的JavaScript代码中更新了以下内容:
create: function(input, callback) {
$.ajax({
url: base_url + '/search-property-autocomplete',
type: 'POST',
dataType: 'json',
data: {
q: query,
},
error: function() {
callback();
},
success: function(res) {
return callback({ address: "<a href='javascript:void(0)'>Clear All</a>" });
}
});
},
Run Code Online (Sandbox Code Playgroud)
但它似乎无法正常工作,因为我无法看到我在那里添加的选项.在我的结果填充后,我无法看到我的超链接.
我已经知道使用下面代码的一些思考将在我搜索后删除我填充的记录.
$('.my-hyperlink-custom-class').on('click', function() {
control.clearOptions();
});
Run Code Online (Sandbox Code Playgroud)
但我坚持将这个新项目附加或推送到我的代码中,并使用Ajax填充结果.
有人可以指导我如何实现这一目标.
非常感谢大家的帮助和支持。特别感谢@afeique的提示来实现此功能。
这是我最终想出的解决方案。
$('#q').selectize({
plugins: ['remove_button'],
valueField: 'address',
labelField: 'address',
searchField: 'address',
create: true,
render: {
item: function(data, escape) {
return '<div>' + escape(data.address) + '</div>';
}
},
onChange: function(value) {
$(".selectize-input input[placeholder]").attr("style", "width: 100%;");
},
load: function(query, callback) {
if (!query.length) return callback();
// Appending only if not exists
if($('.clearAllSuggestions').length == 0) {
$(".selectize-dropdown").append('<a href="#" class="clearAllSuggestions">Clear All</a>');
}
$.ajax({
url: base_url + '/search-property-autocomplete',
type: 'POST',
dataType: 'json',
data: {
q: query,
},
error: function() {
callback();
},
success: function(res) {
console.log(res.properties);
callback(res.properties);
}
});
}
});
$('body').on('click', '.clearAllSuggestions', function() {
var $select = $('#q').selectize({});
var control = $select[0].selectize;
control.clearCache('option');
control.clearOptions();
control.refreshOptions(true);
});
Run Code Online (Sandbox Code Playgroud)
我稍微修改了我的代码,并将下面的代码放在我的load事件中,以在我的建议标签选项中附加“全部清除”锚标签。
// Appending only if not exists
if($('.clearAllSuggestions').length == 0) {
$(".selectize-dropdown").append('<a href="#" class="clearAllSuggestions">Clear All</a>');
}
Run Code Online (Sandbox Code Playgroud)
然后,我只是想清除缓存和选项,因此我输入了下面的代码。
$('body').on('click', '.clearAllSuggestions', function() {
var $select = $('#q').selectize({});
var control = $select[0].selectize;
control.clearCache('option');
control.clearOptions();
control.refreshOptions(true);
});
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助。