Select2 自动设置选项
Select2 set option automatically
我正在构建一个支持应用程序,它有一些 select 框。我为此使用 select2。我遇到的问题是,在我当前的构造中,只有当有人在项目 select2 填写项目后单击线索 select2 时,才会出现项目对应线索的 selection select2.
目标是当有人设置项目 select2 值时,程序会自动将潜在客户 select 框的 select2 设置为潜在客户对应项目的ID。
如果我在工程select2处select工程1082,需要在select2处自动设置"J. Johnson",不用我必须先点击 selectbox.
即使有官方文档 (https://select2.org/programmatic-control/add-select-clear-items) 也能达到我设想的解决方案,但我无法提供帮助,因此我将不胜感激。
项目selection onchange函数调用
$('.select2project').on("change", function() {
var value = $(this).val();
searchLeadsByProjectID(value);
});
searchLeadsByProjectID 函数
function searchLeadsByProjectID(id){
$('.select2lead').select2({
ajax: {
type: 'GET',
url: '/modules/support/ajaxLeadSearch2.php',
dataType: 'json',
delay: 250,
data: {
"id": id
},
processResults: function (data) {
return {
results: data,
more: false
};
}
}
});
}
ajaxLeadSearch2.php
<?php
require_once('../../config.php');
$login = new Login();
if (!$login->checkLogin()) {
echo lang($_SESSION['language'], "INSUFFICIENT_RIGHTS");
exit();
}
$db = new Database();
$query = "
SELECT
LeadID AS lead_id,
REPLACE(CONCAT_WS(' ', LeadInitials, LeadInsertion, LeadLastName), ' ', ' ') AS name,
REPLACE(CONCAT_WS(' ', LeadStreet, LeadStreetNumber, LeadNumberAdjective), ' ', ' ') AS address,
LeadZiPCode AS zipcode,
LeadCity AS city
FROM
`LeadTBL`
WHERE
LeadID = :leadID
ORDER BY
LeadLastName
ASC
";
$binds = array(
':leadID' => $_GET['id'],
);
$result = $db->select($query, $binds);
$json = array();
foreach ($result as $row){
$json[] = array(
'id' => $row['lead_id'],
'text' => $row['name'] .' - '. $row['address'] .', '. $row['zipcode'] .' '. $row['city'] .' ('. $row['lead_id'] .')'
);
}
echo json_encode($json);
追加一个新选项,welp。已解决。
function searchLeadsByProjectID(id) {
$.ajax({
type: 'GET',
url: '/modules/support/ajaxLeadSearch2.php',
dataType: 'json',
delay: 250,
data: {"id": id}
}).success(function (data) {
var $leadSelect = $('.select2lead');
for (var index in data) {
var d = data[index];
var newOption = new Option(d.text, d.id, true, true);
$leadSelect.append(newOption).trigger('change');
}
});
}
我正在构建一个支持应用程序,它有一些 select 框。我为此使用 select2。我遇到的问题是,在我当前的构造中,只有当有人在项目 select2 填写项目后单击线索 select2 时,才会出现项目对应线索的 selection select2.
目标是当有人设置项目 select2 值时,程序会自动将潜在客户 select 框的 select2 设置为潜在客户对应项目的ID。
如果我在工程select2处select工程1082,需要在select2处自动设置"J. Johnson",不用我必须先点击 selectbox.
即使有官方文档 (https://select2.org/programmatic-control/add-select-clear-items) 也能达到我设想的解决方案,但我无法提供帮助,因此我将不胜感激。
项目selection onchange函数调用
$('.select2project').on("change", function() {
var value = $(this).val();
searchLeadsByProjectID(value);
});
searchLeadsByProjectID 函数
function searchLeadsByProjectID(id){
$('.select2lead').select2({
ajax: {
type: 'GET',
url: '/modules/support/ajaxLeadSearch2.php',
dataType: 'json',
delay: 250,
data: {
"id": id
},
processResults: function (data) {
return {
results: data,
more: false
};
}
}
});
}
ajaxLeadSearch2.php
<?php
require_once('../../config.php');
$login = new Login();
if (!$login->checkLogin()) {
echo lang($_SESSION['language'], "INSUFFICIENT_RIGHTS");
exit();
}
$db = new Database();
$query = "
SELECT
LeadID AS lead_id,
REPLACE(CONCAT_WS(' ', LeadInitials, LeadInsertion, LeadLastName), ' ', ' ') AS name,
REPLACE(CONCAT_WS(' ', LeadStreet, LeadStreetNumber, LeadNumberAdjective), ' ', ' ') AS address,
LeadZiPCode AS zipcode,
LeadCity AS city
FROM
`LeadTBL`
WHERE
LeadID = :leadID
ORDER BY
LeadLastName
ASC
";
$binds = array(
':leadID' => $_GET['id'],
);
$result = $db->select($query, $binds);
$json = array();
foreach ($result as $row){
$json[] = array(
'id' => $row['lead_id'],
'text' => $row['name'] .' - '. $row['address'] .', '. $row['zipcode'] .' '. $row['city'] .' ('. $row['lead_id'] .')'
);
}
echo json_encode($json);
追加一个新选项,welp。已解决。
function searchLeadsByProjectID(id) {
$.ajax({
type: 'GET',
url: '/modules/support/ajaxLeadSearch2.php',
dataType: 'json',
delay: 250,
data: {"id": id}
}).success(function (data) {
var $leadSelect = $('.select2lead');
for (var index in data) {
var d = data[index];
var newOption = new Option(d.text, d.id, true, true);
$leadSelect.append(newOption).trigger('change');
}
});
}