Hello,
I'm working with an MVC5 project, and I'm trying to figure out the best way to approach this problem. In my grid, there is a column for "Name" which displays a particular name that has meaning in my business logic. This column is not the primary key of the grid. However, the rule is that "Name" must be unique for all items in the collection, so if the user edits a row in the grid and changes the Name to one that is already in use, I need to prevent that.
Looking through the iggrid documentation, I wasn't sure if there is a validation option in the MVC helper that I could use, or if it would be better for me to do something with the editRowEnding event, perhaps, and check the new Name against all of the other names in the grid. Ideally, I'd like to provide a little validation message on the cell (like what I see when I mark a cell as required and don't input a value), and of course I need to stop the row from being updated.
I'm mainly posting because I didn't want to lose a lot of time chasing an approach that doesn't make good use of the API, and since this has probably been done before, I wanted to see if others had any opinions about how to approach this.
Thanks!
Hi! I tried to do the same, but nothing happens. It looks like the event not raises. I've downloaded the sample and write it in Razor
This is my code:
@(Html.Infragistics() .Grid(Model) .ID("DeliveryMethodsGrid") .Width("100%") .PrimaryKey("ID") .AutoGenerateColumns(false) .AutoGenerateLayouts(false) .Columns(column => { column.For(x => x.Name).HeaderText("Name").Width("100%");
}) .Features(features => { features.Filtering().Mode(FilterMode.Advanced).ColumnSettings(setting => { setting.ColumnSetting().ColumnKey("Name").AllowFiltering(true);
}); features.Updating().EditMode(GridEditMode.None).EnableAddRow(true).EnableDeleteRow(true).ColumnSettings(cs => { cs.ColumnSetting().ColumnKey("Name").Required(true).EditorOptions("type: 'text',validatorOptions: { onblur: true, keepFocus: 'once', checkValue: function(evt, ui) { var value = ui.value; var rows = $('#DeliveryMethodsGrid').igGrid('rows'); for (var i = 0; i < rows.length; i++) { var currentRow = rows[i]; var currentValue = $('#DeliveryMethodsGrid').igGrid('getCellValue', $(currentRow).attr('data-id'), 'Name'); if (value == currentValue && $(currentRow).find('.ui-iggrid-editingcell').length == 0) { ui.message = 'This value alredy exist in the column. Duplicated values are not allowed.'; return false; } } }}"); }); features.Sorting().ColumnSettings(cs => { cs.ColumnSetting().ColumnKey("Name").AllowSorting(true); }); }) .DataSourceUrl(Url.Action("GetAllForGrid")) .UpdateUrl(Url.Action("SaveGrid")) .DataBind() .Render())
Thanks Maya, that helps a lot!
Hello kylemilner ,
The child grids are igGrid widgets so doing something like:
var rows = $("#Grid_14_Locales_child ").igGrid("rows");
Will work and will return only the rows of that child grid.
The data-level corresponds to the level in the hierarchy this particular child grid belongs to. Child grids that are on the same level of the hierarchy will have the same data-level attribute so it would be better to use their ids instead.
You could also check to which grid the currently edited cell belongs to by adding a handler for the editCellStarted event and checking the owner element:
editCellStarted: function(evt, ui){
var gridElement=ui.owner.element;
}
This will return the child grid element. You could then keep it in a global variable and use it when validation is triggered for the editor. You could use the grid element to get the related rows for this grid.
Let me know if you have any additional questions on regarding this.
Best Regards,
Maya Kirova
Developer Support Engineer II
Infragistics, Inc.
http://es.infragistics.com/support
Sorry, but one last question came up, Maya. I'm writing this with the MVC helper, and I think I may have gotten something lost in translation. Here's the code I was trying out:
colSetting.ColumnSetting().ColumnKey("Name")
.Required(true)
.EditorOptions("checkValue:validate()");
In a separate script file, I have the validate function defined like this:
function validate(evt, ui) {
...
When I set it up that way and put a breakpoint on my validate function, I see that it is only called once when the grid loads, but never again when I finish editing a row. Did I use the MVC helper properly in this case? Is it a situation where I need to define the function in-line in the MVC helper?
EDIT: I think I realized my mistake after checking Maya's example a little more closely. I needed my EditorOptions call to look like this:
.EditorOptions("validatorOptions:{checkValue:validate}");
So, another question that has come to mind is how this might work in a hierarchical grid. Say that my grid is nested 3 layers within an overall ighierarchicalgrid, but I still only want to check for unique values on the 3rd level grid (in other words, duplicates could be allowed, but only if they appear at different levels in the hierarchy). I had a couple of ideas on how to alter Maya's example, but I haven't come up with quite the right answer yet.
In the validate function, I'd like the following line to pull out only the rows from one of the nested grids:
var rows = $("#grid").igGrid("rows");
But I'm having trouble writing the correct jQuery selector. When I review the mark-up of my grid in the browser, I notice that the grid I need has an attribute called data-level=3. It also has it's own id, which looks something like ''Grid_14_Locales_child". Would either of those be the natural way to get the relevant rows for comparison?
Thanks Maya, that's just what I was looking for.