当前位置: 首页 > 工具软件 > Tokenfield > 使用案例 >

php ajax自动完成,php – 如何使用ajax自动完成bootstrap tokenfield?

毋修为
2023-12-01

Bootstrap令牌字段 – 使用ajax远程调用预先输入自动完成功能

Prerequisitics:

= stylesheet_link_tag 'sales_crm/tokenfield-typeahead.css'

= stylesheet_link_tag 'sales_crm/bootstrap-tokenfield.css'

%script{:src => "//code.jquery.com/ui/1.10.3/jquery-ui.js", :type => "text/javascript"}

= javascript_include_tag "sales_crm/bootstrap-tokenfield.js"

= javascript_include_tag "sales_crm/typeahead.bundle.min.js"

在HAML视图文件中:

%input#tokenfield-typeahead.form-control.add_locality_data{:type => "text", :value => "", :identifier=> "Locality"}/

1)初始化BloodHound搜索引擎

var engine = new Bloodhound({

remote: {

url: '/sales_crm/leads/get_lead_associated_info?query=%QUERY&model_class='+$('.add_locality_data').attr("identifier"),

filter: function (response) {

var tagged_user = $('#tokenfield-typeahead').tokenfield('getTokens');

console.log(tagged_user)

return $.map(response.leads, function (user) {

console.log(user)

var exists = false;

// console.log("velava saranam");

for (i=0; i < tagged_user.length; i++) {

if (user.value == tagged_user[i].value) {

var exists = true;

}

}

if (!exists) {

return {

value: user.value,

label: user.label

};

}

});

}

},

datumTokenizer: function (d) {

return Bloodhound.tokenizers.whitespace(d.value);

},

queryTokenizer: Bloodhound.tokenizers.whitespace

});

engine.initialize();

2)然后初始化tokenfield函数

$('#tokenfield-typeahead').tokenfield({

delimiter: false,

typeahead: [

{

name: 'users',

displayKey: 'label',

source: engine.ttAdapter()

}

]

})

.on('tokenfield:createtoken', function (e) {

var existingTokens = $(this).tokenfield('getTokens');

if (existingTokens.length) {

$.each(existingTokens, function(index, token) {

console.log(token)

if (token.value === e.attrs.value) {

e.preventDefault();

}else{

console.log(e.attrs.value)

}

});

}else{

console.log(e.attrs.value)

}

});

 类似资料: