我正在尝试将dxTagBox连接到我的搜索功能,我无法弄清楚如何让两者协同工作。
似乎该标签框在加载时正在寻找一组预定义的结果,但我想根据用户键入的内容生成结果:
搜索功能:
var searchUsers = function (query) {
return $pnp.sp
.search({
Querytext: "*",
SourceId: "B09A7990-05EA-4AF9-81EF-EDFAB16C4E31",
StartRow: 0,
RowLimit: 500,
RowsPerPage: 50,
SelectProperties: ["PreferredName", "WorkEmail", "PictureUrl", "AccountName", "JobTitle", "Id"]
})
.then((r) => {
var results = _.filter(r.PrimarySearchResults, user => _.includes(user.AccountName, "membership"));
// console.log(results)
var users = results.reduce((prev, u) => {
prev.push({
name: u.PreferredName,
loginName: u.AccountName,
email: u.WorkEmail
});
return prev;
}, []);
// console.log(users);
return users;
})
.catch(console.log);
};
DataSource:
var peopleSearchDataSource = new DevExpress.data.DataSource({
load: searchUsers,
byKey: function(key) {
return { id: key };
}
});
TagBox:
$("#productsCustom").dxTagBox({
dataSource: peopleSearchDataSource,
displayExpr: "name",
valueExpr: "uid",
itemTemplate: function (user) {
return "<div class='custom-item'><div class='product-name'>" +
user.name + "</div></div>";
},
applyValueMode: 'useButtons',
showSelectionControls: true,
hideSelectedItems: true,
showClearButton: true,
placeholder: "Select users...",
searchEnabled: true,
searchTimeout: 2000,
showDataBeforeSearch: false,
minSearchLength: 2,
onValueChanged: function (e) {
e.value.forEach(function (lastUser) {
ensureUser(lastUser)
.then(function (user) {
$("span:contains('" + user.name + "')")
.closest(".dx-tag-content")
.addClass("user-validated");
})
.catch(function () {
console.log("naaaw");
});
});
}
});
答案 0 :(得分:1)
我认为您的代码没问题,但是您的数据源存在问题,所以试试这个,它可以正常工作:
Api代码(C#web api)
public dynamic Get(string nombre)
{
var tTablas = db.TipoTabla.Where(x=>x.tipNombre.Contains(nombre)).Select(x => new { Id = x.Id, Nombre = x.tipNombre });
return tTablas;
}
数据源这是您的问题,请特别注意此部分 loadOptions.searchValue
var dsTipoTabla = new DevExpress.data.DataSource({
load: function (loadOptions) {
var d = $.Deferred();
$.ajax({
url: UrlApi() + '/Api/Tipotabla?nombre=' + loadOptions.searchValue,
method: "GET",
headers: headers,
}).done(function (result) {
d.resolve(result, {
});
});
return d.promise();
},
});
dxTagBox
$("#cmbTipoTabla").dxTagBox({
dataSource: dsTipoTabla,
placeholder: 'T. Tbl',
displayExpr: "Nombre",
valueExpr: 'Nombre',
minSearchLength: 3,
searchTimeout: 500,
searchMode: "contains",
showClearButton: true,
placeholder: "Select users...",
searchEnabled: true,
});
答案 1 :(得分:1)
我能够像这样解决它:
source: new DevExpress.data.DataSource({
sort: "name",
load: function (loadOptions) {
// console.log("loadoptions", loadOptions);
return _searchUsers(loadOptions.searchValue);
},
byKey: function (e) {
//console.log(e);
}
}),