I'm trying to use a combo in a grid as follows:
@(Html.Infragistics() .Grid(Model) .ID("grid1") .PrimaryKey(nameof(LuItemType.ItemTypeId)) .UpdateUrl(Url.Action("UpdatingSaveChanges", "ItemType")) .AutoGenerateLayouts(false) .AutoCommit(true) .Columns(column => { column.For(x => x.ItemDesc).HeaderText("Description").DataType("string"); column.For(x => x.SafeWorkingLoad).HeaderText("SWL").DataType("string"); column.For(x => x.InspectionPeriodId).HeaderText("InspectionPeriod"); }) .Width("600px") .Height("500px") .Features(features => { features.Updating() .EditMode(GridEditMode.Row) .EnableDeleteRow(true) .EnableAddRow(true) .ColumnSettings(settings => { settings.ColumnSetting().ColumnKey(nameof(LuItemType.ItemDesc)) .EditorType(ColumnEditorType.Text).Required(true).Validation(true); settings.ColumnSetting().ColumnKey(nameof(LuItemType.SafeWorkingLoad)) .EditorType(ColumnEditorType.Text).Required(true).Validation(true); settings.ColumnSetting().ColumnKey(nameof(LuItemType.InspectionPeriodId)) .EditorType(ColumnEditorType.Combo) .ComboEditorOptions(options => { options.DataSourceUrl(Url.Action("GetComboData", "ItemType")) .TextKey(nameof(LuInspectionPeriod.InspectionPeriodDesc)) .ValueKey(nameof(LuInspectionPeriod.InspectionPeriodId)) .Mode(ComboMode.DropDown); }) .Required(true).Validation(true); });
}) .DataSourceUrl(Url.Action("GetGridData", "ItemType")) .DataBind() .Render())
However, the combo isn't loaded with any data, so I'm seeing the Id of the row rather than the text description, and when I try to drop the list there's no data in it.The data source method is as follows:
public class ItemType : LookUpController<LuItemType> {
... SNIP ...
[ComboDataSourceAction] public ActionResult GetComboData() {return View(RsContext.LuInspectionPeriod.AsQueryable()); }
}
And the entity class looks like:
public partial class LuInspectionPeriod { public LuInspectionPeriod() { LiftingItem = new HashSet<LiftingItem>(); LuItemType = new HashSet<LuItemType>(); }
[Required] public int InspectionPeriodId { get; set; } [Required] public string InspectionDesc { get; set; } [Required] public int? InspectionPeriodDays { get; set; } [Required] public int? InspectionPeriodMonths { get; set; }
public virtual ICollection<LiftingItem> LiftingItem { get; set; } public virtual ICollection<LuItemType> LuItemType { get; set; } }
When I click on a the cell with the combo I get this error in the console:
jquery.js:9566 GET https://localhost:44307/ItemType/GetComboData?textKey=InspectionPeriodId&valueKey=InspectionPeriodId&toLower=1&_=1492017867793 500 (Internal Server Error)
Is there anything obviously wrong with how I've set this up?
Hi Stamen
I've tried to wire that in (as per code below), but it's not working because the datasource for the combo column is still empty at this point. I've put a break point in my GetComboData() and it isn't called until the combo is dropped down for the first time. Is there something I need to do to get it to eager load the data? I tried adding a DataBind call in the comboEditorOptions but that didn't help. Do you have an example of this that I could look at?
@(Html.Infragistics() .Grid(Model) .ID("grid1") .PrimaryKey(nameof(LuItemType.ItemTypeId)) .UpdateUrl(Url.Action("UpdatingSaveChanges", "ItemType")) .AutoGenerateLayouts(false) .AutoCommit(true) .Columns(column => { column.For(x => x.ItemTypeId).HeaderText("ItemTypeId").DataType("number").Hidden(true); column.For(x => x.ItemDesc).HeaderText("Description").DataType("string"); column.For(x => x.SafeWorkingLoad).HeaderText("SWL").DataType("string"); column.For(x => x.InspectionPeriodId).HeaderText("InspectionPeriod")/*.FormatterFunction("lookupProductName")*/; }) .Width("600px") .Height("500px") .Features(features => { features.Updating() .EditMode(GridEditMode.Row) .EnableDeleteRow(true) .EnableAddRow(true) .ColumnSettings(settings => { settings.ColumnSetting().ColumnKey(nameof(LuItemType.ItemDesc)) .EditorType(ColumnEditorType.Text).Required(true).Validation(true); settings.ColumnSetting().ColumnKey(nameof(LuItemType.SafeWorkingLoad)) .EditorType(ColumnEditorType.Text).Required(true).Validation(true); settings.ColumnSetting().ColumnKey(nameof(LuItemType.InspectionPeriodId)) .EditorType(ColumnEditorType.Combo) .ComboEditorOptions(options => { options.DataSourceUrl(Url.Action("GetComboData", "ItemType")) .TextKey(nameof(LuInspectionPeriod.InspectionDesc)) .ValueKey(nameof(LuInspectionPeriod.InspectionPeriodId)) .EnableClearButton(true) .Mode(ComboMode.DropDown); }) .Required(true).Validation(true);
});
features.Sorting().ColumnSettings(sorting => { sorting.ColumnSetting().ColumnKey(nameof(LuItemType.ItemDesc)) .CurrentSortDirection("ascending").AllowSorting(true); });
}) .AddClientEvent("rendered", "fillList()") .DataSourceUrl(Url.Action("GetGridData", "ItemType")) .DataBind() .Render())<br /><input type="button" class="btn btn-success" id="saveChanges" value="Save Changes" />
<script type="text/javascript">
$("#saveChanges").bind({ click: function () { $("#grid1").igGrid("saveChanges"); } });
function fillList() { fillProductNameLookup(lookupProductList); }
// from https://es.infragistics.com/community/forums/p/72892/370474.aspx#370474 function fillProductNameLookup(objectToFill) { var colSettings = $("#grid1").igGridUpdating("option", "columnSettings"); var colSetting; for (var i = 0; i < colSettings.length; i++) { colSetting = colSettings[i]; if (colSetting.columnKey === "InspectionPeriodId") { if (colSetting.editorType && colSetting.editorType === "combo") { var ds = colSetting.editorOptions.dataSource; var textKey = colSetting.editorOptions.textKey; var valueKey = colSetting.editorOptions.valueKey; var item; for (var j = 0; i < ds.length; i++) { item = ds[i]; objectToFill[item[valueKey]] = item[textKey]; } } break; } } }
var lookupProductList = {};
function lookupProductName(productNumber) { return lookupProductList[productNumber]; }</script>
Hello,
I am glad I was able to help. You can execute the function on the rendered event that you can subscribe to with the AddClientEvent method in the view.
Creating a lookup function to be executed for the column through a formatter is still the recommended approach. There is no automation that is available for this use case through the MVC wrappers.
Please, let me know if you have any other issues or have troubles implementing this solution!
Best regards,
Stamen Stoychev
The column then gets its data as follows:
column.For(x => x.InspectionPeriodId).HeaderText("InspectionPeriod").FormatterFunction("lookupProductName");
So I've tried to get Martin's solution working as follows:
fillProductNameLookup(lookupProductList);
function fillProductNameLookup(objectToFill) { var colSettings = $("#grid1").igGridUpdating("option", "columnSettings"); var colSetting; for (var i = 0; i < colSettings.length; i++) { colSetting = colSettings[i]; if (colSetting.columnKey === "InspectionPeriodId") { if (colSetting.editorType && colSetting.editorType === "combo") { var ds = colSetting.editorOptions.dataSource; var textKey = colSetting.editorOptions.textKey; var valueKey = colSetting.editorOptions.valueKey; var item; for (var j = 0; i < ds.length; i++) { item = ds[i]; objectToFill[item[valueKey]] = item[textKey]; } } break; } } }
The problem I'm having though is that fillProductNameLookup(lookupProductList) is trying to run before the grid is initialised. Currently I'm not using the loader (I'm using the CDN), but even if I was I don't know how I'd force this code to run after the mvc wrapper has completed? Is there a "grid initialized" event I can wrap the fillProductNameLookup(lookupProductList) call in?
Yes that definitely helped. So, the grid was stuck in read only mode because I'd incorrectly set the DataType of ItemTypeId to "string" rather than "number" (I'd actually originally set it to "int", which I keep accidentally doing, then changed to string to try and fix that).
Then I tried changing the GetComboData method to return a List<LuInspectionPeriod> rather than a Queryable and that fixed the data issue and the combo's list now contains data.
So, having solved that I'm still seeing the grid displaying the Id rather than the text InspectionDesc of the LuInspectionPeriod, despite now having set the TextKey to retrieve the description. Am I right in thinking this is the expected behaviour, because all the stuff with the combo only effects the cell when it is in edit mode? I'm used to the wingrid control that work differently to that.
I've seen this answer from Martin which suggests that's the case: https://es.infragistics.com/community/forums/p/72892/370474.aspx#370474? Is that still the recommended approach for showing the description field when the cell is not in edit mode? Is there an example of that using the Html Wrapper syntax?