ListGrids and Checkbox

I have a listGrid with a number of checkboxes.

I want the user to be able to toggle a checkbox with out having to first select the cell so I use: canToggle:true, editEvent:“click” for each field with a checkbox.

The problem is I have a changed function for the same checkboxes.

The changed function should be called every time the checkbox value changes.

The problem is unless the cell has been selected the changed event does not work.

For the first toggle no changed event, for the second and there after it does fire.

If I remove canToggle:true the changed event works as per-normal. Every time the checkbox is toggled the changed event is fired.

How can I make the canToggle fire the changed event?

I tried putting a cellClick on the listGrid to trigger startEditing() but the toggle happens before cellClick.

I think canToggle needs to work like this:

  • select cell for editing
  • toggle checkbox value

Where as at the moment it’s more like this:

  • toogle checkbox value
  • select cell for editing

canToggle can be used independently of editing, that’s why the value is toggled immediately - entering editing is independent.

You can trigger editing manually via cellClick, but if you do this you should set editEvent:“none” so that the grid no longer enters editing automatically.

This is what I have now, I’ve set the values as instructed. It still toggles before it enters editing mode though.

CellClick: startEditing() is working the same as editEvent: click.

Field

{name: "FIELD", canToggle:true, width:78, title: "Field", editorType: "checkbox", valueMap: {"0" : false, 
"1" : true}, canEdit: true, changed: function(form, item) {isc.say(\'x\');}}

ListGrid

isc.ListGrid.create({
ID: "listgrid",
waitForSave: true,
width:"100%", height:"100%", alternateRecordStyles:true, saveByCell:true ,showAllRecords:true,
dataSource:gridDataDS
, autoSaveEdits:false
, editEvent: "none"
, canEdit: true
, cellClick:function (record, rowNum, colNum) { listgrid.startEditing(rowNum, colNum); }
, editByCell: true
})

Hi Nick,

So to clarify, the suggestion was that from cellClick(), you call your changed() logic before calling startEditing().

The problem with triggering my changed function on cell click is the form/item elements are not accessible.

cellclick function in listgrid:

listgrid.startEditing(rowNum, colNum);mycell=listgrid.getField(colNum);mycell.changed();

changed function in field:

changed: function(form, item) {this.myfunc(form,false);}

//also tried item.myfunc()

I get the error ‘item is undefined’ with this method or ‘this is undefined’.

Ah I see your dilemma. At the time of the toggling the item does not exist (again, toggling can be used without editing as such).

What information are you trying to get from the item? It’s likely it’s available another way.

myfunc() only uses the form to check if values have changed and to trigger some additional events.

if (form.valuesHaveChanged()) {
 dosomething();
}

Hi Nick,

You can use listGrid.cellHasChanges(), listGrid.rowHasChanges() etc to detect changes.

In general, look for a ListGrid API for any given task related to the current values of the editors, current errors, etc, rather than trying to use the “form” made available in certain event handlers within the inline editors. That “form” exists only temporarily, during editing, and contains at any given time only the editors that are actually active.

I probably should give some more background as to how I am using it. It could be the case that the entire approach is wrong and I am only patching up one problem to encounter a further problem down the line.

The reason for my approach is I have added myfunc() as a property of formItem.

isc.FormItem.addProperties({
				myfunc: function(form) {}
)}

Changed then calls this function

changed: function(form, item) {this.myfunc(form);}

The main reason was to save writing the same large function in every changed event by making it accessible to all formitems.

The same function is needed by both dynamicsForm and listGrids.

Using my function myfunc() it has no way to detect if it’s a listGrid or a form, so if treats everything like a form.

I can see why cellHasChanges is a better approach to valuesHaveChanged because the form is only temporary in a listGrid and it is unaware of the other cells in the grid and cannot detect changes in them.

I guess what I am really after then is for myfunc() to be able to tell if it’s being used in the context of a form or a listGrid and then depending on this do an if else to choose between cellHasChanges and valuesHaveChanged before continuing with the rest of the function.

Is this possible? or would the only way be to supply the listGird id as a parameter of myfunc()?

Quite wise to step back and give more background, but even better, what does myfunc() actually do? Maybe it’s something that can be done more simply..

If not, passing either the grid or form to myfunc() and having it detect which is which (eg isc.isA.ListGrid()) is a reasonable approach.

Thank you, that has helped to simplify the function from 5 to 3 parameters.

The function basically detects if a dynamicForm or listGird has changes and sets a global array[‘variable’] to true and enables a save button. I know I could use formItem showIf to enable/disable a save button but it seems simpler to use the changed event and it also allows me to use an iButton instead of a formItem of type button.

The reason for this global array is external to the function, if a user tries to navigate away they get a warning about unsaved data only if the data will be destroyed as a result of the action.

In addition the other two optional parameters take a tabsetid and a icon url. Then when a change is detected within a tabSet the icon indicates unsaved data. So it’s very easy to see at a glance any unsaved tabs.

The original toggle problem however still exists but listGrids are now much better at detecting changes.

That makes sense.

Global “dirtiness detection” makes sense as a future enhancement to SmartClient - we’ll track this.

Is there anything I can do in the mean time with the toggle problem?

Eh? Isn’t it solved? Write your function such that it can use ListGrid rather than form APIs as needed, and pass the ListGrid from cellClick as previously indicated.

The problem still exists.

I have changed myfunc() which is called by changed to be listGrid specific. It now looks like:

changed: function() {listGrid.myfunc();}

myfunc() has now been added as a property of listGrid instead of formItem.

However the problem of toggle still happening before entering editor still occurs.

editEvent: "none" , 
canEdit: true , 
cellClick:function (record, rowNum, colNum) { 
	listgrid.startEditing(rowNum, colNum);
	mycell=listgrid.getField(colNum);
	mycell.changed(); 
},
editByCell: true

Even calling myfunc directly in cellClick does not work.

editEvent: "none" , 
canEdit: true , 
cellClick:function (record, rowNum, colNum) { 
	listgrid.startEditing(rowNum, colNum);
	listgrid.myfunc();
},
editByCell: true

The value has been changed by toggle but myfunc does not see it. The only way it sees the change is if you toggle a value and then untoggle it - changed event is then triggered.

Hi nickc,

OK, there was some confusion as our understanding of your requirements evolved.

Since what you’re trying to do is change detection, not just reacting to a toggle, you actually want to fire your code after toggling has occurred. cellClick is too early.

Here’s code that will work:

    canEdit:true,
    editEvent:"click",
    rowClick : function (record, rowNum, colNum) {
        isc.logWarn("has changes: " + this.cellHasChanges(rowNum, colNum));
        this.Super("rowClick", arguments);
        isc.logWarn("after: " + this.cellHasChanges(rowNum, colNum));
    }

You will see false logged, then true, indicating the toggled cell. Note that editEvent is “click” again, there’s no need to trigger editing manually.

Thank you that has done the trick. How does it work though?

Is rowClick simply calling the super rowClick which allows enough time for the toggle to happen so the change is detected?

It’s not timing-related. The default implementation of rowClick is what performs the toggling when canToggle:true is set.