整合选择二与Laravel 4应用程序 [英] Integrating Select2 with Laravel 4 App
问题描述
我决定在我的多选搜索过滤器来实现选择2,我可以做一些帮助整合它。
I've decided to implement select2 in my multiselect search filter and I could do with some help integrating it.
我有以下方法:
public function getContactByName($name)
{
return Contact::select(array('id', DB::raw('concat(first_name," ",last_name) as name')))->where(DB::raw('concat(first_name," ",last_name)'), 'like', "%$name%")->get();
}
这个方法返回其中FIRST_NAME和姓氏就像在URL输入的字词,例如 http://www.example.com/admin/get-contact-name/ (名称)。
This method returns any records where 'first_name' and 'last_name' are like the term entered in the URL, for example http://www.example.com/admin/get-contact-name/{name}.
下面的路由处理请求:
Route::get('admin/get-contact-name/{name}', 'AdminContactListController@getContactByName');
这一切的伟大工程。我只是需要一些帮助,选择2和AJAX设置它。我真的不知道摆在了JS的选项是什么。
This all works great. I just need some help setting it up with select2 and AJAX. I'm not really sure what to put in the options for the JS.
我有以下的表单字段设置:
I have the following form field setup:
<input name="contact_names_value" type="text" class="input-medium contact_names_value" id="contact_names_value">
任何人都可以点我在正确的方向。干杯。
Can anyone point me in the right direction. Cheers.
修改:排序我打这里拍摄,并在黑暗中,因为我不知道所有这些要素的意思是,如网页等,但这样的$ C $三我到目前为止,我只想说这是不工作:
EDIT: I'm sort of playing about here and shooting in the dark, as I'm not sure what each of these elements mean, such as page etc but this is the code I have so far, suffice to say it's not working:
var name = $('#contact_names_value').val();
$('#contact_names_value').select2({
placeholder: 'Search contacts',
minimumInputLength: 3,
ajax: {
url: '/admin/get-contact-name' + name,
dataType: 'json',
data: function (term, page) {
return {
q: term,
page_limit: 10
};
},
results: function (data, page) {
return {results: data};
}
},
});
修改:好,大家好,我觉得我更接近,我想我得到通过选择2的结果,但现在我得到一个JavaScript错误。这是我到目前为止所。
EDIT: OK guys, I think I'm much closer, and I think I'm getting results via select2 now but I'm getting a JavaScript error. Here's what I have so far.
方法:
public function getContactByName()
{
$name = Input::get('name');
return Contact::select(array('id', DB::raw('concat(first_name," ",last_name) as name')))->where(DB::raw('concat(first_name," ",last_name)'), 'like', "%$name%")->get();
}
路线:
Route::get('admin/get-contact', 'AdminContactListController@getContactByName');
选择二JavaScript的:
Select2 JavaScript:
$('#contact_names_value').select2({
name: $('.select2-input').val(),
placeholder: 'Search contacts',
minimumInputLength: 3,
ajax: {
url: '/admin/get-contact' + name,
dataType: 'json',
data: function (term) {
return {
name: term
};
},
results: function (data) {
return {results: data};
},
dropdownCssClass: 'bigdrop'
},
});
选择二JS错误:
Select2 JS Error:
Uncaught TypeError: Cannot call method 'toUpperCase' of undefined plugins.js:1549
C plugins.js:1549
a.fn.select2.defaults.formatResult plugins.js:1550
g plugins.js:1549
a.extend.populateResults plugins.js:1549
f.query.callback plugins.js:1549
(anonymous function) plugins.js:1549
a.extend.success plugins.js:1549
c jquery-1.9.1.min.js:3
p.fireWith jquery-1.9.1.min.js:3
k jquery-1.9.1.min.js:5
r
任何建议家伙?
Any advice guys?
推荐答案
OK,我这就像一个魅力的工作了。
OK, I have this working like a charm now.
下面是我的JS code:
Here's my JS code:
$('#contact_names_value').select2({
placeholder: 'Search contacts',
minimumInputLength: 3,
ajax: {
url: '/admin/get-contact',
dataType: 'json',
data: function (term, page) {
return {
contact_names_value: term
};
},
results: function (data, page) {
return {results: data};
}
},
tags: true
});
我并不需要的名称变量引用,该插件处理这一点,并追加选择/输入查询字符串的值。
I did not need the 'name' variable reference, as the plugin handles this and appends the value of the select/input to the query string.
其次,在我的形式,我需要设置为隐藏的类型,如previously它是文本。
Secondly, in my form, I needed to set the type to 'hidden', as previously it was text.
您还会注意到上面我说页面这两个结果和数据功能。不知道这是否发挥了作用。
You'll also notice above that I added 'page' to both the results and data functions. Not sure if this made a difference.
我还添加了'标签:真正的'使其工作就像一个多选
I also added 'tags: true' to make it work like a multiselect.
下面是我的方法,code:
Here's my method code:
public function getContactByName()
{
$name = Input::get('contact_names_value');
return Contact::select(array('id', DB::raw('concat(first_name," ",last_name) as text')))->where(DB::raw('concat(first_name," ",last_name)'), 'like', "%$name%")->get();
}
注意在我的SELECT语句中,我做了DB:原设置如first_name和姓氏字段选择为文本。我认为这是主要的问题之一,因为插件需要'身份证'和'文'发挥作用。
Notice during my select statement, I do a DB::raw and set the 'first_name' and 'last_name' fields to be selected as 'text'. I think this was one of the major issues, as the plugin requires 'id' and 'text' to function.
我的路线是简单的:
Route::get('admin/get-contact', 'AdminContactsController@getContactByName');
这篇关于整合选择二与Laravel 4应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!