[CLOSED] Gridview Primary Key

  1. #1

    [CLOSED] Gridview Primary Key

    Hi all,

    little question.

    There is a way to automatically check if a user inserts in a gridpanel two rows with the same content in a column?

    For example: i've a grid containing two columns. The first is "Description" the second is "Url". Users may update this grid and save grid content.

    In DataBase Url is Primary Key, so if a user inserts 2 rows with the same url, DB will not accept the data.

    I know i can manage this situation by code, but if there is a way to automatically avoid this....

    Thanks


  2. #2

    RE: [CLOSED] Gridview Primary Key

    oops... not gridview, but gridpanel
  3. #3

    RE: [CLOSED] Gridview Primary Key

    Hi,

    There is no automatic validation but you can manually checking value using ValuidateEdit listener
    <%@ Page Language="C#" %>
    
    <%@ Register Assembly="Coolite.Ext.Web" Namespace="Coolite.Ext.Web" TagPrefix="ext" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <script runat="server">
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Ext.IsAjaxRequest)
            {
                this.Store1.DataSource = new object[]
                {
                    new object[] {"Company1", "Description"},
                    new object[] {"Company2", "Description"},
                    new object[] {"Company3", "Description"},
                    new object[] {"Company4", "Description"},
                    new object[] {"Company5", "Description"}
                };
    
                this.Store1.DataBind();
            }
        }
    </script>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head id="Head1" runat="server">
        <title></title>
        <script type="text/javascript">
           function validateEdit(e){
              if(e.field == "company"){
                  if(e.grid.store.find(e.field, e.value) !== -1){
                      Ext.Msg.show({
                           title:"Warning",
                           msg: "Company with name '"+e.value+"' exists already. Please input another name.",
                           buttons: Ext.Msg.OK,                       
                           icon: Ext.MessageBox.WARNING
                      });
                      return false;
                  }
                  
                  return true;
              }
           }
        </script>
    </head>
    <body>
        <form id="form1" runat="server">
            <ext:ScriptManager ID="ScriptManager1" runat="server" />
            
            <ext:Store ID="Store1" runat="server">
                <Reader>
                    <ext:ArrayReader>
                        <Fields>
                            <ext:RecordField Name="company" />
                            <ext:RecordField Name="description" />
                        </Fields>
                    </ext:ArrayReader>
                </Reader>
            </ext:Store>
            
            <ext:GridPanel 
                ID="GridPanel1" 
                runat="server" 
                StoreID="Store1" 
                StripeRows="true"
                Title="Grid" 
                TrackMouseOver="true"
                Width="600" 
                Height="350"
                AutoExpandColumn="Company">
                <ColumnModel ID="ColumnModel1" runat="server">
                    <Columns>
                        <ext:Column ColumnID="Company" Header="Company" DataIndex="company">
                            <Editor>
                                <ext:TextField runat="server"></ext:TextField>
                            </Editor>
                        </ext:Column>
                        <ext:Column Header="Description" DataIndex="description">
                            <Editor>
                                <ext:TextField runat="server"></ext:TextField>
                            </Editor>
                        </ext:Column>
                    </Columns>
                </ColumnModel>
                
                <TopBar>
                    <ext:Toolbar runat="server">
                        <Items>
                            <ext:Button runat="server"  Text="Insert" Icon="Add">
                                <Listeners>
                                    <Click Handler="#{GridPanel1}.insertRecord(0, {company: ''});#{GridPanel1}.getView().focusRow(0);#{GridPanel1}.startEditing(0, 0);" />
                                </Listeners>
                            </ext:Button>
                        </Items>
                    </ext:Toolbar>                
                </TopBar>
                
                <Listeners>
                    <ValidateEdit Fn="validateEdit" />
                </Listeners>
            </ext:GridPanel>          
        </form>
    </body>
    </html>
  4. #4

    RE: [CLOSED] Gridview Primary Key

    Great!!


    Very nice help!

    Thanks a lot!!!!
  5. #5

    RE: [CLOSED] Gridview Primary Key

    ...i've found a problem...


    With your code, if in grid there is a url like 'http://forums.ext.net' and i try to add 'http://www.ext.net' it says that already exists url....

    I have tried to use FindExact as Extjs Api but i get a Unsupported method error by JScript

    This is the code:

    function validateEdit(e){
              if(e.field == "url"){
                  if(e.grid.store.findExact(e.field, e.value) !== -1){
                      Ext.Msg.show({
                           title:"Warning",
                           msg: "Url  '"+e.value+"' already exists. Please input another url.",
                           buttons: Ext.Msg.OK,                       
                           icon: Ext.MessageBox.WARNING
                      });
                      return false;
                                 
                  }
                  
                  if(/^http/.test(e.value)){}
                  else
                  {
                      Ext.Msg.show({
                           title:"Warning",
                           msg: "Urls may starts with http or https",
                           buttons: Ext.Msg.OK,                       
                           icon: Ext.MessageBox.WARNING
                      });
                      return false;
                                 
                  }
                  
                  return true;
              }
              
           }
    ...any ideas?
    Last edited by geoffrey.mcgill; Feb 22, 2011 at 4:33 AM.
  6. #6

    RE: [CLOSED] Gridview Primary Key

    Hi,

    Use 'findBy'
    e.grid.store.findBy(function(record){return record.get(e.field) === e.value;}) !== -1
  7. #7

    RE: [CLOSED] Gridview Primary Key

    Thanks !!
  8. #8

    RE: [CLOSED] Gridview Primary Key

    Wrong place. Sorry.

Similar Threads

  1. GridPanel.Store.Primary is always Null !!!
    By Mohammad in forum 1.x Help
    Replies: 6
    Last Post: Dec 13, 2011, 5:37 AM
  2. Replies: 2
    Last Post: Nov 04, 2011, 6:06 PM
  3. [CLOSED] Store and 3 Primary Key
    By pdcase in forum 1.x Legacy Premium Help
    Replies: 1
    Last Post: Nov 23, 2010, 11:21 AM
  4. Replies: 4
    Last Post: Oct 24, 2008, 12:31 PM
  5. [CLOSED] GridView Bug
    By Timothy in forum 1.x Legacy Premium Help
    Replies: 2
    Last Post: Oct 14, 2008, 12:14 PM

Posting Permissions