Tanlangan qutilarni almashtirish uchun select2 kutubxonasidan foydalanmoqdaman. Men 7-misolni Select2 kutubxonasi sahifasida topishingiz mumkin bo‘lgan 7-misolni qayta tartibladim (identifikator $("#e7").select2
va hokazo bilan pastga aylantiring... ). Men ketma-ket json ma'lumotlarini qaytaradigan o'zimning umumiy ishlov beruvchimni yaratdim:
GetData.asxh ko'rinishi : umumiy sinf GetData : IHttpHandler { public bool IsReusable { get { return false; } }
public class RecipesList
{
public int total { get; set; }
public List<TopRecipeTable> recipes { get; set; }
public RecipesList() { }
public RecipesList(int total, List<TopRecipeTable> recipes)
{
this.total = total;
this.recipes = recipes;
}
}
private string GenerateJsonSerializedObject(int languageId, string orderBy)
{
RecipesList recipeList = new RecipesList(15, DBDataBase.GetTopRecipesByNumberOfRecipes(languageId, 15));
return new JavaScriptSerializer().Serialize(recipeList);
}
public void ProcessRequest(HttpContext context)
{
int languageId;
bool languageParsed = int.TryParse(context.Request["languageId"], out languageId);
string orderBy = (string)context.Request["orderBy"];
if (languageParsed && orderBy != string.Empty)
{enter code here
context.Response.ContentType = "application/json";
var jsonValue = GenerateJsonSerializedObject(languageId, orderBy);
context.Response.Write(jsonValue);
}
}
Ushbu umumiy ishlov beruvchi jsonning to'g'ri formatini qaytaradi (men uni ushbu URL orqali tekshirdim). Mening natijam (json) ham yuqoridagi sahifadagi misol bilan bir xil. Ammo bundan keyin jQuery endi ishga tushmaydi.
Mening skriptim:
$(document).ready(function () {
$("#e8").select2({
placeholder: "Search for a recipe",
//minimumInputLength: 1,
ajax: {
url: "/Handlers/GetData.ashx",
dataType: 'jsonp',
data: function (term, page) {
return {
languageId: 1,
orderBy: "TA"
};
},
results: function (data, page) {
alert(data.total);
var more = (page * 10) < data.total; // whether or not there are more results available
// notice we return the value of more so Select2 knows if more results can be loaded
return { results: data.recipes, more: more };
}
},
formatResult: movieFormatResult, // omitted for brevity, see the source of this page
formatSelection: movieFormatSelection, // omitted for brevity, see the source of this page
dropdownCssClass: "bigdrop", // apply css that makes the dropdown taller
escapeMarkup: function (m) { return m; } // we do not want to escape markup since we are displaying html in results
});
});
Asl misolda xuddi shu alert(data.total)
ni yozishga harakat qildim va u ishladi, lekin mening versiyamda emas. Shunday qilib, menda to'g'ri json formati bor, jQuery mening umumiy ishlov beruvchimni chaqiradi, shuningdek, tilId parametrlarini oldi ... va to'g'ri json formatini qaytaradi, lekin hech narsa emas. Men bu erda biror narsa etishmayotganimni bilmayman, chunki bu narsa umumiy ishlov beruvchi bilan ham ishlashi mumkinligiga aminman. Umid qilamanki, men muammom haqida etarli ma'lumot berdim.
I can also add my result in jquery .ajax error handler :
xhr.status = 200
ajaxOptions = parsererror
horwnError = SyntaxError : invalid label
If this is any helpful information