【问题标题】:jQuery Select2 Plugin using Ajax使用 Ajax 的 jQuery Select2 插件
【发布时间】:2014-07-31 11:58:26
【问题描述】:

我正在使用带有 Ajax 的 Select2 插件来连接到我的员工数据库。它允许设置会议并选择您要邀请的所有员工。

这就是代码的样子:

$("#requiredAttendees").select2({
            multiple: true,
            minimumInputLength: 3,
            placeholder: "Search for employee",
            tokenSeparators: [" "],
            ajax: { 
                url: "jsonUser.php",
                dataType: 'json',
                data: function (term) {
                    return {
                        term: term, // search term
                    };
                },
                results: function (data) { // parse the results into the format expected by Select2.
                    return {results: data};
                }
            },
        });

这是我在字段中输入姓氏后的 JSON 响应:

[{"id":"12345","text":"Hussey, Sam},{"id":"67890","text":"Hussey, Carl"}]

我的问题是,在缩小搜索结果范围时,我还试图通过搜索名字来进一步缩小范围。所以我会输入Hussey, c 并希望它只显示我的结果。

但是,只要我输入一个逗号,我就找不到任何结果。

我假设代码将带有逗号的任何内容视为新结果,但我不能确定。

这是我的 JSON 输出:

//Define the output
$firstName  = (string) $emp->FirstName;
$lastName   = (string) $emp->LastName;
$empID      = (string) $emp->EmpID;
$email      = (string) $emp->email;

//Add the resykts to an array
$users[] = array(
    'id' => $empID,
    'text' => $lastName . ', ' . $firstName,
);

}

//Set the content type to JSON  for jquery to understand
header('Content-Type: application/json');

//Print the response to the page
print json_encode($users);

有什么想法会导致这种情况吗?

编辑:

这是我发送查询的部分:

//Define some variables
$query    = $_GET['term'];
$users    = array();

//Prevent running if less than 3 char have bene submitted
if (strlen($query) < 3) {
    die();
}

//Create a new database connection
$objDB = new DB;
$xml   = $objDB->setStoredProc('focusGetEmp')->setParam("LastName", $query)->execStoredProc()->parseXML();

【问题讨论】:

    标签: javascript jquery ajax json jquery-select2


    【解决方案1】:

    现在已解决。搜索词只是直接从查询中获取结果,所以我不得不调整它来修复它。

    之前

    WHERE    LastName LIKE '%' + @LastName + '%'
    

    之后

    WHERE    A.[LastName] + ', ' + A.[FirstName] LIKE '%' + @term + '%'
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-01
      • 1970-01-01
      • 2017-03-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多