This is a migrated thread and some comments may be shown as answers.

duplicate checking for the grid

5 Answers 80 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Sudhakar
Top achievements
Rank 1
Sudhakar asked on 13 Jan 2015, 10:21 AM
Hi Team,

I have a grid and it has some rows in it. I am implementing batch editing for this grid. I am using the DLL Version of 2014.3.1216.45

I need to implement duplicate record checking for this grid.

we have compartment index column in the grid. When the user clicks on add new record and he enter one for compartment index and second time also if he enter one for the compartment index and click on save changes button that time we need to check the duplicate record validation.


currently this duplicate checking I am doing it on the "OnBatchEditCellValueChanging" event of the grid.
But I need this implementation  when I click on save changes button.

Please see my code below.

<script type="text/javascript">

function valueChanging(sender, args) {

if (args.get_columnUniqueName() == "CompNo") {
var tableView = sender.get_masterTableView();
var batchManager = sender.get_batchEditingManager();
var items = tableView.get_dataItems();
var newValue = args.get_editorValue();
for (var i = 0; i < items.length; i++) {
var idCell = items[i].get_cell("CompNo");
var id = batchManager.getCellValue(idCell);
if (args.get_row() != items[i].get_element() && id == newValue) {
radconfirm(transMessages.SDP_ALERT_WHILE_UPDATE_LOAD_DETAIL, confirmCallBackFn);
}
}
}
}
</script>
<telerik:RadGrid ID="CompartmentLevelLoadRadGrid" runat="server" AutoGenerateColumns="false" CssClass="TransferGridPanel"
Height="233px" Width="475px" OnItemDataBound="CompartmentLevelLoadRadGrid_ItemDataBound" OnNeedDataSource="CompartmentLevelLoadingRadGrid_NeedDataSource"
OnBatchEditCommand="CompartmentLevelLoadRadGrid_BatchEditCommand" OnItemCreated="CompartmentLevelLoadRadGrid_ItemCreated">

<MasterTableView ShowHeadersWhenNoRecords="true" CommandItemDisplay="Top" EditMode="Batch" DataKeyNames="CompartmentIndex">
<CommandItemSettings ShowRefreshButton="false" AddNewRecordText="Add LineItem" />
<BatchEditingSettings EditType="Cell" />

<Columns>
<telerik:GridTemplateColumn HeaderText="Comp No." HeaderStyle-Width="30px" UniqueName="CompNo" DataField="CompartmentIndex">
<ItemTemplate>
<%# Eval("CompartmentIndex") %>
</ItemTemplate>
<EditItemTemplate>
<telerik:RadComboBox runat="server" ID="ddlCompartment" OnLoad="ddlLoadCompartments" Width="50px" EmptyMessage="-Select-">
</telerik:RadComboBox>
<asp:RequiredFieldValidator ID="compartmentRequiredFieldValidator"
ControlToValidate="ddlCompartment" ErrorMessage="Compartment Required" runat="server" Display="Dynamic">
</asp:RequiredFieldValidator>
</EditItemTemplate>
</telerik:GridTemplateColumn>

<telerik:GridTemplateColumn HeaderText="PriorTo Load" HeaderStyle-Width="30px" UniqueName="PriorToLoad" DataField="PTL">
<ItemTemplate>
<%# Eval("PTL") %>
</ItemTemplate>
<EditItemTemplate>
<telerik:RadNumerictextbox runat="server" ID="txtPTL" Width="50px" NumberFormat-DecimalDigits="0" NumberFormat-GroupSeparator="" MinValue="0" MaxValue="999999999">
</telerik:RadNumerictextbox>
<asp:RequiredFieldValidator ID="PTLRequiredFieldValidator"
ControlToValidate="txtPTL" ErrorMessage="PTL is Required" runat="server" Display="Dynamic">
</asp:RequiredFieldValidator>
</EditItemTemplate>
</telerik:GridTemplateColumn>

<telerik:GridTemplateColumn HeaderText="Planned Product" HeaderStyle-Width="50px" UniqueName="PlannedLoadProd" DataField="PlannedProductName" ReadOnly="true" InsertVisiblityMode="AlwaysVisible">
<ItemTemplate>
<%# Eval("PlannedProductName") %>
</ItemTemplate>
<EditItemTemplate>
<telerik:RadComboBox runat="server" ID="ddlPlannedProduct" OnLoad="ddlPlannedProductLoad" Width="70px" EmptyMessage="-Select-">
</telerik:RadComboBox>
</EditItemTemplate>
</telerik:GridTemplateColumn>
<telerik:GridTemplateColumn DataField="ActualProductName" HeaderText="Actual Product" UniqueName="ActualLoadProd" HeaderStyle-Width="50px" >
<ItemTemplate>
<%# Eval("ActualProductName") %>
</ItemTemplate>
<EditItemTemplate>
<telerik:RadComboBox runat="server" ID="ddlActualProduct" OnLoad="ddlActualProductLoad" Width="70px">
</telerik:RadComboBox>
<asp:RequiredFieldValidator ID="actualProductValidator"
ControlToValidate="ddlActualProduct" ErrorMessage="ActualProd is Required" runat="server" Display="Dynamic">
</asp:RequiredFieldValidator>
</EditItemTemplate>
</telerik:GridTemplateColumn>

<telerik:GridTemplateColumn DataField="PlannedQuantity" HeaderText="Planned Quantity" UniqueName="PlannedLoadQty" HeaderStyle-Width="50px" ReadOnly="true" InsertVisiblityMode="AlwaysVisible">
<ItemTemplate>
<%# Eval("PlannedQuantity") %>
</ItemTemplate>
<EditItemTemplate>
<telerik:RadNumerictextbox runat="server" ID="txtPlannedQuantity" Width="60px" NumberFormat-DecimalDigits="0" NumberFormat-GroupSeparator="" MinValue="0" MaxValue="999999999">
</telerik:RadNumerictextbox>
</EditItemTemplate>
</telerik:GridTemplateColumn>

<telerik:GridTemplateColumn DataField="ActualQuantity" HeaderText="Actual Quantity" UniqueName="ActualLoadQty" HeaderStyle-Width="50px">
<ItemTemplate>
<%# Eval("ActualQuantity") %>
</ItemTemplate>
<EditItemTemplate>
<telerik:RadNumerictextbox runat="server" ID="txtActualQuantity" Width="60px" NumberFormat-DecimalDigits="0" NumberFormat-GroupSeparator="" MinValue="0" MaxValue="999999999">
</telerik:RadNumerictextbox>
<asp:RequiredFieldValidator ID="actualQuantityValidator"
ControlToValidate="txtActualQuantity" ErrorMessage="ActualQty is Required" runat="server" Display="Dynamic">
</asp:RequiredFieldValidator>
</EditItemTemplate>
</telerik:GridTemplateColumn>

</Columns>
</MasterTableView>
<ClientSettings>
<ClientEvents OnBatchEditCellValueChanging="valueChanging" />
</ClientSettings>

</telerik:RadGrid>         
                    
                    
Regards,
Sudhakar.
        
 


    


          

5 Answers, 1 is accepted

Sort by
0
Angel Petrov
Telerik team
answered on 16 Jan 2015, 07:47 AM
Hi Sudhakar,

Executing the validation logic when the save button is clicked is possible by subscribing to the OnCommand event of the grid and checking whether the command name is BatchEdit.

JavaScript:
function gridCommand(sender, args)
            {
                if (args.get_commandName() === "BatchEdit") {
                    //check the items values
 
                    //call the below to cancel the request to the server.
                    args.set_cancel(true);
                }
            }

Bear in mind that if you move the logic inside this event you should compare each item value with the rest of the grid items values. That said as a performance optimization you can store newly added values in an array once the OnBatchEditCellValueChanged event is fired and later traverse the array and gird items collection to perform the validation.

Regards,
Angel Petrov
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
0
Sudhakar
Top achievements
Rank 1
answered on 19 Jan 2015, 10:14 AM
Hi Petrov,

Can you please send me the code for both events.

While click on save changes button event and OnBatchEditCellValueChanged event(with the approach you suggested in the mail).

Regards,
Sudhakar.
‚Äč
0
Angel Petrov
Telerik team
answered on 22 Jan 2015, 07:53 AM
Hello Sudhakar,

If we refer to the previously provided code you can do something like this.

ASPX:
<ClientSettings>
               <ClientEvents OnBatchEditCellValueChanged="cellValueChanged" OnCommand="onCommand" />
           </ClientSettings>

JavaScript:
var compartmentIndexes = [];
            function cellValueChanged(sender,args) {
                if (args.get_columnUniqueName() === "CompNo") {
                    var newValue = args.get_editorValue();
                    compartmentIndexes.push(newValue);
                }
            }
 
            function pageLoad() {
                var gridItems = $find('<%=CompartmentLevelLoadRadGrid.ClientID%>').get_masterTableView().get_dataItems();
                for (var i = 0; i < gridItems.length; i++) {
                    compartmentIndexes.push(gridItems[i].get_cell("CompNo").children[0].innerHTML.trim());
                }
            }
 
            function onCommand(sender,args) {
                if (args.get_commandName() === "BatchEdit") {
                    var shouldCancel = false;
                    var sorted_arr = compartmentIndexes.sort();
                    var results = [];
 
                    for (var i = 0; i < compartmentIndexes.length - 1; i++) {
                        if (sorted_arr[i + 1] == sorted_arr[i]) {
                            shouldCancel = true;
                        }
                    }
                    if (shouldCancel) {
                        alert("There are duplicate items");
                        args.set_cancel(true);
                    }
                }
            }


Regards,
Angel Petrov
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
0
Sudhakar
Top achievements
Rank 1
answered on 22 Jan 2015, 09:36 AM
Hi Petrov,

The below provide approach is not working.

Let me explain the scenario once again.

I have a grid, when I click on the add new record button the grid enters in to the edit mode and allow me to insert values in to the grid.  I entered the values (1,100) for the compartment no: and prior to load columns in the first row and (1,200) in the second row.
When I click on save changes button I need to display the duplicate compartment number validation message.

Currently I am doing that in OnBatchEditCellValueChanging event it is working fine (I need to move this code to the event when I click on save changes.)
 
But this is not working in the below scenario.

If my grid is having two rows with values: (1,100) and (2,200) for the compartment no and prior to load columns. While editing if I try to change compartment no 1 to 2 in the first row  it is not allowing  me to change that. Because we are writing this functionality in  OnBatchEditCellValueChanging event.whenever i change the 1 to 2  and came out of the cell it will show the duplicate validation becoz 2 is already there in the second row.So how can we change the 1 to 2 in the first row 2 to 1 in the second row while editing.

The code is already attached in this post .Please provide me the implementation for this issue ASAP.

Regards,
Sudhakar.




 
 
 
  
  




0
Angel Petrov
Telerik team
answered on 26 Jan 2015, 04:04 PM
Hello Sudhakar,

The approach I have suggested should have helped you resolve the matter. When applying the previously provided logic you should remove the OnBatchEditCellValueChanging handler thus allowing the user to edit the grid contents. Later when one tries to save the data, the validation will trigger and alert the user that a duplicate value is entered.

Using the code, provided so far in the thread, I have assembled a sample web site that illustrates a possible realization. Please test it on your end and let us know if it behaves as expected.

Regards,
Angel Petrov
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
Tags
Grid
Asked by
Sudhakar
Top achievements
Rank 1
Answers by
Angel Petrov
Telerik team
Sudhakar
Top achievements
Rank 1
Share this question
or