We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 3632
    • 22 Posts
    I have been working through building a CMP over the past week or so. Basically I am using it to display and modify data in a custom DB table holding store locations.

    I have a grid which works fine for listing, paging and inline updates of the displayed fields (a limited subset of the ones in my database table). I have a attached a tollbar to the grid with buttons to add a new record, edit a selected record from the grid and delete a selected record.

    The issue i am seeing is in the delete record functionality. There were two ways I could have done this.
    1. I could use the listener function for the delete button to pop up a confirmation dialog and then used an AJAX request to communicate directly with the processor.
    2. I could use the listener function to spawn a new window and set a field in the new window to the id for posting to my processor.

    As I was trying to get my head around how ExtJS windows work I chose the latter option. I instantiate my delete window object and use ExtJS’s setValue function to set the field value in the window, taking account of whether the window object already exists or not.

    When I attempt to delete the first record, things go fine, the field value is successfully set but if I attempt to delete a second record without a page refresh, the value of the field in the Window object is not updated even though I can see via firebug that the new id is being sent to the setValue function.

    I attach the relevant code below:
    First the toolbar delete button code:
    {text:'Delete Selected Shop Location'
        			,xtype: 'button'
        	            ,id: 'locations-delete'
        	            ,listeners: {'click': {fn: this.deleteIt, scope: this}}	
        		
        		}
    


    function extending grid functions to return the id of the currently selected record:
        ,getSelectedId: function() {
            var sels = this.getSelectionModel().getSelections();
            if (sels.length <= 0) 
            {
            	Ext.Msg.alert('McCabe\'s Pharmacy','Click on a location to select it');
            	return false;
            }
            else
            {
            	return sels[0].data.id;
            }
            
        }
    


    Next the Listener function for the button:
    deleteIt: function(btn,e) {
            var id = this.getSelectedId();
            if(id != false)
            {
            	if (!this.deleteWindow) {
            		this.deleteWindow = MODx.load({
            			xtype: 'locations-window-delete'
            			,listeners: {
                        	'success': {fn:function(r) {
                           	this.refresh();
                        	},scope:this}
            			}
            		});
            		Ext.getCmp('lid').setValue(id);
            	}
            	else
            	{
            		this.deleteWindow.reset();
    
            		Ext.getCmp('lid').setValue(id);
            	}
            	this.deleteWindow.show(e.target);
            	return true;
            }
        }
    


    And finally the delete window object code:
    Locations.window.Delete = function(config) {
        config = config || {};
    
        
        Ext.applyIf(config,{
            title:'Delete Location',
            	url: Locations.config.connector_url
    			,baseParams: 
    			{
    				action: 'delete'
    				,thread: config.thread
    			}
        	,blankValues:true
            ,width: 350
            ,fields:[{html: '<span style="padding:10px">Are you sure you want to delete this location?</span>'},{xtype:'hidden',id:'lid',name:'lid',value:''}]
        })
        Locations.window.Delete.superclass.constructor.call(this,config);
    };
    Ext.extend(Locations.window.Delete,MODx.Window);
    Ext.reg('locations-window-delete',Locations.window.Delete);
    


    This occurs consistently across IE, Chrome, FF and Opera on Windows and Chrome, FF and Opera on Ubuntu and to be honest, it is unclear to me if this is a bug in ModExt, ExtJS or my own code that I am too dumb to spot.

    Finally, my relevant specs
    Running ModX 2.0.4-pl2
    ExtJS 3.2.1
    Smarty 2.6.19
      • 28215
      • 4,149 Posts
      A few things.

      1. You can pass in an object of key-value pairs into a MODx.Window object instantiation, in the parameter ’record’, that will set the fields of the window.
      2. MODx.* grids store the currently selected row data into this.menu.record.
      3. You should name your hidden field in the window ’id’, not ’lid’. This would match the key in the data record.
      4. I’ve rewritten your deleteIt code to be a bit more streamlined:

      deleteIt: function(btn,e) {
          var r = this.menu.record || {};
          if (Ext.isEmpty(r.id)) return;
      
          if (!this.deleteWindow) {
              this.deleteWindow = MODx.load({
                  xtype: 'locations-window-delete'
                  ,record: r
                  ,listeners: {
                      'success': {fn:function(r) {
                          this.refresh();
                      },scope:this}
                  }
              });
          }
          this.deleteWindow.reset();
          this.deleteWindow.setValues(r);
          this.deleteWindow.show(e.target);
      }
      


      Note the use of this.menu.record, the record: property, and the setValues method. Hope that helps, feel free to ask follow-up questions.
        shaun mccormick | bigcommerce mgr of software engineering, former modx co-architect | github | splittingred.com
        • 3632
        • 22 Posts
        That’s brilliant thanks. Subsequently figured out how to fill the fields from the record after looking at existing pages with Firebug.
          • 3632
          • 22 Posts
          Right, still seeing this problem. Implemented the deleteIt function as you suggested but could not use this.menu.record to retrieve the selected record as the record is not being chosen by a right click menu option. Instead I left click the row to select it and then use the getSelectedId() function from my first post to retrieve the id from the selected record. I then construct an object r using that id as:

          var id = this.getSelectedId();
                  var r = new Object();
                  r.id = id;
          


          Each time I select a record in the grid and call the deleteIt function, both the value for id and r.id update successfully - I can see this in firebug. However, only on the first call to my delete window’s setValues() function does the value of the id field update when the window is shown. Subsequent calls to the setValues() function on the existing window seem to fail to update the field. Even calling this.deleteWindow.config.record = r; to update the window’s config with the updated record object before using setValues(r) does not seem to change it.

          Any thoughts?





            • 28215
            • 4,149 Posts
            Did you change it from:

            {xtype:'hidden',id:'lid',name:'lid',value:''}


            to:
            {xtype:'hidden',id:'locations-delete-id',name:'id',value:''}


            ? The ’name’ field needs to be ’id’.
              shaun mccormick | bigcommerce mgr of software engineering, former modx co-architect | github | splittingred.com
              • 3632
              • 22 Posts
              I did and on the first delete the field value is populated fine. Problem is the field is not updated for subsequent deletes. It continues to hold the initially set value.

              I’m going to throw a few console log lines into modx.window.js to confirm the record is updated there.
                • 3632
                • 22 Posts
                Just for laughs I stuck a console.log line in the setValues function in modx.window.js and oddly it never appears - guess it is out of scope
                  • 28215
                  • 4,149 Posts
                  I’d need to see your whole code again to help further.
                    shaun mccormick | bigcommerce mgr of software engineering, former modx co-architect | github | splittingred.com
                    • 3632
                    • 22 Posts
                    Hey Shaun,

                    Have taken enough of your time on this and thanks for the very prompt replies. Might just rework the code to use a confirm with ModX AJAX instead for this. I’m spawning an update window exactly the same way and its fields are updated fine so the window code seems sound. Will put this down to the almost full moon.