|
| 1 | +--- |
| 2 | +title: Enable ForeignKey Column Sorting by Text |
| 3 | +page_title: ForeignKey Column Sorting by Text |
| 4 | +description: "An example on how to enable ForeignKey column sorting by text in the Telerik UI Grid for {{ site.framework }}." |
| 5 | +slug: howto_enable_foreignkey_sotringby_text_grid |
| 6 | +tags: grid, enable, foreignkey, column, sort, text |
| 7 | +component: grid |
| 8 | +type: how-to |
| 9 | +res_type: kb |
| 10 | +--- |
| 11 | + |
| 12 | +## Environment |
| 13 | + |
| 14 | +<table> |
| 15 | + <tr> |
| 16 | + <td>Product</td> |
| 17 | + <td>Telerik {{ site.product_short }} Grid</td> |
| 18 | + </tr> |
| 19 | + <tr> |
| 20 | + <td>Operating System</td> |
| 21 | + <td>Windows 10 64bit</td> |
| 22 | + </tr> |
| 23 | +</table> |
| 24 | + |
| 25 | +## Description |
| 26 | + |
| 27 | +How can I enable ForeignKey column sorting by text in the {{ site.product }} Grid? |
| 28 | + |
| 29 | +## Solution |
| 30 | + |
| 31 | +The following example demonstrates how to enable the sort-by-text functionality in a ForeignKey column by using a calculated field in a Grid. |
| 32 | + |
| 33 | +* Create an additional text field that will be used for sorting in the ViewModel of the Grid. |
| 34 | +``` |
| 35 | + public class ProductViewModel |
| 36 | + { |
| 37 | + public string CategoryName |
| 38 | + { |
| 39 | + get; |
| 40 | + set; |
| 41 | + } |
| 42 | + } |
| 43 | +``` |
| 44 | + |
| 45 | +* Prepopulate the data for the Foreign Key column within the controller. |
| 46 | +``` |
| 47 | + public class GridController : Controller |
| 48 | + { |
| 49 | + public IActionResult Index() |
| 50 | + { |
| 51 | + ViewData["categories"] = GetCategories(); |
| 52 | + return View(); |
| 53 | + } |
| 54 | + } |
| 55 | +``` |
| 56 | + |
| 57 | +* Create a dictionary of key-value pairs using the prepopulated data. |
| 58 | +``` |
| 59 | + <script type="text/javascript"> |
| 60 | + var categories= @Html.Raw(Json.Serialize(@ViewData["categories"])); //serialize the data |
| 61 | + |
| 62 | + //create dictionary of text-values for the FKC |
| 63 | + var categoriesDict = {}; |
| 64 | + for (var i = 0; i < categories.length; i++) { |
| 65 | + categoriesDict[categories[i].CategoryID] = categories[i].CategoryName; |
| 66 | + } |
| 67 | + </script> |
| 68 | +
|
| 69 | +``` |
| 70 | +* Bind the additional text field using the [.Bound()](https://docs.telerik.com/aspnet-core/api/Kendo.Mvc.UI.Fluent/GridColumnFactory?&_ga=2.8632309.1913833702.1649765178-415541100.1638373975#boundsystemlinqexpressionsexpressionsystemfunctt1) configuration option and calculate the field by utilizing the [.ClientTemplate()](https://docs.telerik.com/aspnet-core/api/Kendo.Mvc.UI.Fluent/GridBoundColumnBuilder#clienttemplatesystemstring) setting. |
| 71 | +``` |
| 72 | + @(Html.Kendo().Grid<Kendo.Mvc.Examples.Models.ProductViewModel>() |
| 73 | + .Name("grid") |
| 74 | + .Columns(columns => |
| 75 | + { |
| 76 | + columns.Bound(p => p.CategoryName).ClientTemplate("#=calculateField(CategoryID)#").EditorTemplateName("CategoryNameEditor"); |
| 77 | + |
| 78 | + }) |
| 79 | + ) |
| 80 | +
|
| 81 | +
|
| 82 | + <script type="text/javascript"> |
| 83 | + function calculateField(categoryId){ |
| 84 | + return categoriesDict[categoryId]; |
| 85 | + } |
| 86 | + </script> |
| 87 | +``` |
| 88 | +* Specify a DropDownList for the text field editor in the **~View/Shared/EditorTemplates** folder and specify the value field with the **data-bind** attribute. |
| 89 | +``` |
| 90 | + @model ProductViewModel |
| 91 | +
|
| 92 | + @(Html.Kendo().DropDownListFor(m => m.CategoryName) |
| 93 | + .DataValueField("CategoryID") |
| 94 | + .DataTextField("CategoryName") |
| 95 | + .AutoBind(false) |
| 96 | + .HtmlAttributes(new {data_bind="value:CategoryID"}) |
| 97 | + .BindTo((System.Collections.IEnumerable)ViewData["categories"]) |
| 98 | + ) |
| 99 | +
|
| 100 | +``` |
| 101 | + |
| 102 | +Example: |
| 103 | + |
| 104 | +```Model |
| 105 | + public class ProductViewModel |
| 106 | + { |
| 107 | + public int ProductID |
| 108 | + { |
| 109 | + get; |
| 110 | + set; |
| 111 | + } |
| 112 | +
|
| 113 | + public string ProductName |
| 114 | + { |
| 115 | + get; |
| 116 | + set; |
| 117 | + } |
| 118 | +
|
| 119 | + public decimal UnitPrice |
| 120 | + { |
| 121 | + get; |
| 122 | + set; |
| 123 | + } |
| 124 | +
|
| 125 | +
|
| 126 | + [UIHint("CategoryNameEditor")] |
| 127 | + public string CategoryName |
| 128 | + { |
| 129 | + get; |
| 130 | + set; |
| 131 | + } |
| 132 | +
|
| 133 | + public int? CategoryID { get; set; } |
| 134 | + } |
| 135 | +``` |
| 136 | +```Index.cshtml |
| 137 | + @(Html.Kendo().Grid<Kendo.Mvc.Examples.Models.ProductViewModel>() |
| 138 | + .Name("grid") |
| 139 | + .Columns(columns => |
| 140 | + { |
| 141 | + columns.Bound(p => p.ProductName); |
| 142 | + columns.Bound(p => p.CategoryName).ClientTemplate("#=calculateField(CategoryID)#").EditorTemplateName("CategoryNameEditor").Width(200); |
| 143 | + columns.Bound(p => p.UnitPrice).Format("{0:c}").Width(200); |
| 144 | + columns.Command(command => command.Destroy()).Width(150); |
| 145 | + }) |
| 146 | + .ToolBar(toolBar => |
| 147 | + { |
| 148 | + toolBar.Create(); |
| 149 | + toolBar.Save(); |
| 150 | + }) |
| 151 | + .Editable(editable => editable.Mode(GridEditMode.InCell)) |
| 152 | + .Pageable() |
| 153 | + .Sortable() |
| 154 | + .Scrollable() |
| 155 | + .HtmlAttributes(new { style = "height:540px;" }) |
| 156 | + .DataSource(dataSource => dataSource |
| 157 | + .Ajax() |
| 158 | + .Batch(true) |
| 159 | + .PageSize(20) |
| 160 | + .ServerOperation(false) |
| 161 | + .Model(model => |
| 162 | + { |
| 163 | + model.Id(p => p.ProductID); |
| 164 | + model.Field(p => p.ProductID).Editable(false); |
| 165 | + }) |
| 166 | + .Read(read => read.Action("Products_Read", "Grid")) |
| 167 | + .Update(update => update.Action("Products_Update", "Grid")) |
| 168 | + .Create(create => create.Action("Products_Create", "Grid")) |
| 169 | + .Destroy(destroy => destroy.Action("Products_Destroy", "Grid")) |
| 170 | + ) |
| 171 | + ) |
| 172 | +``` |
| 173 | +```Controller |
| 174 | + public class GridController : Controller |
| 175 | + { |
| 176 | + public IActionResult Index() |
| 177 | + { |
| 178 | + ViewData["categories"] = GetCategories(); |
| 179 | + return View(); |
| 180 | + } |
| 181 | + public IEnumerable<CategoryViewModel> GetCategories() |
| 182 | + { |
| 183 | + var firms = Enumerable.Range(1, 10) |
| 184 | + .Select(i => new CategoryViewModel |
| 185 | + { |
| 186 | + CategoryID = i, |
| 187 | + CategoryName = "CategoryName " + i |
| 188 | + }).ToList(); |
| 189 | + |
| 190 | + return firms; |
| 191 | + } |
| 192 | + public IActionResult Products_Read([DataSourceRequest] DataSourceRequest request) |
| 193 | + { |
| 194 | + IEnumerable<ProductViewModel> products = GetProducts(); |
| 195 | +
|
| 196 | + return Json(products.ToDataSourceResult(request)); |
| 197 | + } |
| 198 | +
|
| 199 | + private static IEnumerable<ProductViewModel> GetProducts() |
| 200 | + { |
| 201 | + return Enumerable.Range(1, 20) |
| 202 | + .Select(i => new ProductViewModel |
| 203 | + { |
| 204 | + ProductID = i, |
| 205 | + ProductName = "ProductName" + i, |
| 206 | + UnitPrice = 1250.50M, |
| 207 | + CategoryID = i % 5 |
| 208 | + }); |
| 209 | + } |
| 210 | + } |
| 211 | +``` |
| 212 | +```CategoryNameEditor.cshtml |
| 213 | + @model ProductViewModel |
| 214 | +
|
| 215 | + @(Html.Kendo().DropDownListFor(m=>m.CategoryName) |
| 216 | + .DataValueField("CategoryID") |
| 217 | + .DataTextField("CategoryName") |
| 218 | + .AutoBind(false) |
| 219 | + .HtmlAttributes(new {data_bind="value:CategoryID"}) |
| 220 | + .BindTo((System.Collections.IEnumerable)ViewData["categories"]) |
| 221 | + ) |
| 222 | +
|
| 223 | +``` |
| 224 | +```JavaScript |
| 225 | + <script type="text/javascript"> |
| 226 | + var categories= @Html.Raw(Json.Serialize(@ViewData["categories"])); |
| 227 | + |
| 228 | + //create dictionary of text-values for the FKC |
| 229 | + var categoriesDict = {}; |
| 230 | + for (var i = 0; i < categories.length; i++) { |
| 231 | + console.log(categories[i]); |
| 232 | + categoriesDict[categories[i].CategoryID] = categories[i].CategoryName; |
| 233 | + } |
| 234 | + |
| 235 | + function calculateField(firmId){ |
| 236 | + return categoriesDict[firmId]; |
| 237 | + } |
| 238 | + </script> |
| 239 | +``` |
0 commit comments