c# – 如何在Jqgrid中显示间接数据

前端之家收集整理的这篇文章主要介绍了c# – 如何在Jqgrid中显示间接数据前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我在我的ASP.net MVC Web应用程序中实现Jqgrid.我有这样的数据:
  1. SID SNAME CITY
  2. 1 ABC 11
  3. 2 XYZ 12
  4. 3 ACX 13
  5. 4 KHG 14
  6. 5 ADF 15
  7. 6 KKR 16

和另一张桌子

  1. CID CNAME
  2.  
  3. 11 Chennai
  4. 12 Mumbai
  5. 13 Delhi like this

但是,在网格中我想显示如下:

  1. SID SNAME City
  2. 1 ABC Chennai
  3. 2 XYZ Mumbai
  4. 3 ACX Delhi
  5. 4 KHG Banglore
  6. 5 ADF Hyderabad
  7. 6 KKR Kolkatta

我无法使用join,因为类结构是这样的:

  1. Class Student
  2.  
  3. {
  4. long sid,string sname,long city
  5. }

所以,当我从数据库中读取时,我得到的城市ID不是城市名称.

但是,我想在网格数据中向最终用户显示城市名称而不是城市ID

我需要一些像查找功能的东西,以便在将数据绑定到jQgrid之前,城市ID将与城市名称映射并显示它而不是显示ID

我没有找到办法完成这件事.

请帮忙..

  1. The controller method i am using is as follows:
  2.  
  3.  
  4. public JsonResult Students()
  5. {
  6. List<Students> liStudents = new List<Students>();
  7. SortedList<long,string> slLocations = new SortedList<long,string>();
  8. slLocations = Students.LoadLocations();
  9. liStudents = Students.GetStudents();
  10. return Json(liStudents,JsonRequestBehavior.AllowGet);
  11. }

如何修改return语句以在json响应中抛出slLocations

解决方法

我之前已经回答了已经结束的问题(见 here).然而,我决定详细回答你的问题,因为你描述的问题非常普遍.

我首先提醒jqGrid提供格式化程序:“select”,它使用formatoptions.value或editoptions.value将ID解码为文本.格式化程序:“select”使用值和可选的分隔符,分隔符和defaultValue属性,但它不能使用editoptions.dataUrl从服务器获取所需的数据而不是使用静态值.问题很简单:处理dataUrl异步工作,但在格式化网格体的列期间,不支持延迟填充.所以要使用formatter:“select”必须在jqGrid处理服务器响应之前设置formatoptions.value或editoptions.value.

the old answer中,我建议扩展从服务器返回的JSON响应,并为具有formatter:“select”的列的editoptions.value提供附加数据.我建议设置beforeProcessing.例如,可以按以下格式生成服务器响应:

  1. {
  2. "cityMap": {"11": "Chennai","12": "Mumbai","13": "Delhi"},"rows": [
  3. { "SID": "1","SNAME": "ABC","CITY": "11" },{ "SID": "2","SNAME": "XYZ","CITY": "12" },{ "SID": "3","SNAME": "ACX","CITY": "13" },{ "SID": "4","SNAME": "KHG",{ "SID": "5","SNAME": "ADF",{ "SID": "6","SNAME": "KKR","CITY": "11" }
  4. ]
  5. }

并使用以下jqGrid选项

  1. colModel: [
  2. {name: "SNAME",width: 250},{name: "CITY",width: 180,align: "center"}
  3. ],beforeProcessing: function (response) {
  4. var $self = $(this);
  5. $self.jqGrid("setColProp","CITY",{
  6. formatter: "select",edittype: "select",editoptions: {
  7. value: $.isPlainObject(response.cityMap) ? response.cityMap : []
  8. }
  9. });
  10. },jsonReader: { id: "SID"}

The demo演示了这种方法.它显示

可以使用相同的方法动态设置任何列选项.例如,人们可以使用

  1. {
  2. "colModelOptions": {
  3. "CITY": {
  4. "formatter": "select","edittype": "select","editoptions": {
  5. "value": "11:Chennai;13:Delhi;12:Mumbai"
  6. },"stype": "select","searchoptions": {
  7. "sopt": [ "eq","ne" ],"value": ":Any;11:Chennai;13:Delhi;12:Mumbai"
  8. }
  9. }
  10. },"CITY": "11" }
  11. ]
  12. }

和以下JavaScript代码

  1. var filterToolbarOptions = {defaultSearch: "cn",stringResult: true,searchOperators: true},removeAnyOption = function ($form) {
  2. var $self = $(this),$selects = $form.find("select.input-elm");
  3. $selects.each(function () {
  4. $(this).find("option[value='']").remove();
  5. });
  6. return true; // for beforeShowSearch only
  7. },$grid = $("#list");
  8.  
  9. $.extend($.jgrid.search,{
  10. closeAfterSearch: true,closeAfterReset: true,overlay: 0,recreateForm: true,cloSEOnEscape: true,afterChange: removeAnyOption,beforeShowSearch: removeAnyOption
  11. });
  12.  
  13. $grid.jqGrid({
  14. colModel: [
  15. {name: "SNAME",align: "center"}
  16. ],beforeProcessing: function (response) {
  17. var $self = $(this),options = response.colModelOptions,p,needRecreateSearchingToolbar = false;
  18. if (options != null) {
  19. for (p in options) {
  20. if (options.hasOwnProperty(p)) {
  21. $self.jqGrid("setColProp",options[p]);
  22. if (this.ftoolbar) { // filter toolbar exist
  23. needRecreateSearchingToolbar = true;
  24. }
  25. }
  26. }
  27. if (needRecreateSearchingToolbar) {
  28. $self.jqGrid("destroyFilterToolbar");
  29. $self.jqGrid("filterToolbar",filterToolbarOptions);
  30. }
  31. }
  32. },jsonReader: { id: "SID"}
  33. });
  34. $grid.jqGrid("navGrid","#pager",{add: false,edit: false,del: false})
  35. $grid.jqGrid("filterToolbar",filterToolbarOptions);

该演示使用上面的代码.

如果动态更改任何选项,我们将重新创建搜索过滤器.该方法允许实施更灵活的解决方案.例如,服务器可以检测客户端(Web浏览器)的语言首选项,并根据选项返回数字,日期等格式选项.我相信每个人都可以建议其他有趣的场景.

再说一遍.如果你在select in(searchoptions.value和editoptions.value)中有太多项目,我建议你不要使用字符串而不是对象作为searchoptions.value和editoptions.value的值.它允许您指定select元素中的项目顺序.

如果您选择的项目太多(例如您所在国家/地区的所有城市),那么您可以考虑使用我在the answer演示的select2插件.它简化了选项的选择,因为它转换了非常接近jQuery的select元素UI自动填充.

The next demo演示了select2插件用法.如果单击搜索工具栏或搜索对话框的“选择”元素的下拉箭头,则会获得可用于快速搜索的其他输入字段.如果开始在输入框中键入一些文本(例如下图中示例中的“e”),则选项列表将缩减为具有键入文本作为子字符串的选项:

我个人认为这种“选择搜索”控件非常实用.

顺便说一句,我在the another answer中描述了如何动态设置colNames. In可用于从服务器端管理更多信息.

更新:相应的控制器操作学生可以是关于以下内容

  1. public class Student {
  2. public long SID { get; set; }
  3. public string SNAME { get; set; }
  4. public long CITY { get; set; }
  5. }
  6. public class City {
  7. public long CID { get; set; }
  8. public string CNAME { get; set; }
  9. }
  10. ...
  11. public class HomeController : Controller {
  12. ...
  13. public JsonResult Students () {
  14. var students = new List<Student> {
  15. new Student { SID = 1,SNAME = "ABC",CITY = 11 },new Student { SID = 2,CITY = 12 },new Student { SID = 3,CITY = 13 },new Student { SID = 4,new Student { SID = 5,new Student { SID = 6,CITY = 11 }
  16. };
  17. var locations = new List<City> {
  18. new City { CID = 11,CNAME = "Chennai"},new City { CID = 12,CNAME = "Mumbai"},new City { CID = 13,CNAME = "Delhi"}
  19. };
  20. // sort and concatinate location corresponds to jqGrid editoptions.value format
  21. var sortedLocations = locations.OrderBy(location => location.CNAME);
  22. var sbLocations = new StringBuilder();
  23. foreach (var sortedLocation in sortedLocations) {
  24. sbLocations.Append(sortedLocation.CID);
  25. sbLocations.Append(':');
  26. sbLocations.Append(sortedLocation.CNAME);
  27. sbLocations.Append(';');
  28. }
  29. if (sbLocations.Length > 0)
  30. sbLocations.Length -= 1; // remove last ';'
  31. return Json(new {
  32. colModelOptions = new {
  33. CITY = new {
  34. formatter = "select",edittype = "select",editoptions = new {
  35. value = sbLocations.ToString()
  36. },stype = "select",searchoptions = new {
  37. sopt = new[] { "eq","ne" },value = ":Any;" + sbLocations
  38. }
  39. }
  40. },rows = students
  41. },JsonRequestBehavior.AllowGet);
  42. }
  43. }

猜你在找的C#相关文章