Hey,
I have a column config like this:
HTML<dxi-column
dataField="objectId"
caption="Object"
calculateDisplayValue="object.displayText">
<dxo-lookup
[dataSource]="objects"
valueExpr="id"
displayExpr="displayText"
[allowClearing]="true">
</dxo-lookup>
</dxi-column>
The data source of the DataGrid looks like this:
TypeScript[
{
"id": 123,
"objectId": 321,
"object": {
"id": 321,
"displayText": "example"
}
},
...
]
And the data source of the lookup:
TypeScript[
{
"id": 321,
"displayText": "example"
},
...
]
Displaying the column works fine for existing rows, i.e. it shows object.displayText
property. But when a new row is inserted (I am using "cell" editing mode) and a value for the lookup column is selected, then the cell will be empty afterwards.
If I remove the calculateDisplayValue
option, then it works as expected for new rows as well, but then of course all of the lookup values are being loaded in the first place.
Is there any way to solve this without the need to set the nested object
manually for new rows after selecting a value from the lookup?
Thanks!
Hello,
We need to inspect your calculateDisplayValue implementation. In this case, could you provide us with a simple working sample illustrating the issue in action? You can use our Cell Editing demo to illustrate your recent progress. Click the "Copy to CodeSandBox" button to extract it into a stand-alone example, modify it and post it back here for further examination.
We'll do our best to put you on the right track.
Hey Lena,
Thanks for your reply.
Actually, there is no further implementation of
calculateDisplayValue
. I am just using the stringobject.displayText
there to display this property.I could not reproduce my issue using the simple sample.
So I assume it must have to do sth with the way my data sources are set up. In my real application, those are connected to a REST API, so it is a bit difficult to reproduce this in a CodeSandBox, but I will try…
Hey Lena,
I managed to reproduce my issue.
It seems to occurs when the type of the display value attribute is numeric.
Please see this example: https://4lfv2.codesandbox.io/
If you create a new row and select a value in the state column, the cell is empty afterwards.
If you change the type of
State.Number
tostring
(and of course also the data in theemployees
andstates
arrays), it will work as expected.Thanks,
Jan.
Hi Jan,
Thank you for the update. The "https://4lfv2.codesandbox.io/" link doesn't allow me to review dxDataGrid configuration and apply any changes. Could you post a link that contains the full code. It should look like this one: https://codesandbox.io/s/custom-data-source-7ym1y
Hey Artem,
here is the link: https://codesandbox.io/s/4lfv2
Thanks,
Jan.
Hi Jan,
Thank you for the example. The problem occurs because the
lookup
column adds values only to theStateID
field. You need to add values for both theState
andStateID
fields.It's possible to use the setCellValue callback for this purpose:
setStateValue = (newData, currentValue, rowData) => { newData.StateID = currentValue; newData.State = this.states.find(i => i.ID === currentValue); };
I've updated your example to demonstrate this solution in action - https://codesandbox.io/s/datagrid-assign-a-value-to-a-lookup-column-that-is-connected-with-another-field-lev6m?file=/src/app/app.component.ts:934-1096
Please test this approach and let us know if it works on your side.
Hey Artem,
Thanks for your reply. I suppose this approach will work, but I was hoping to not require sth like that.
I just cannot wrap my head around why it is working just by changing the type of the
Number
property tostring
… please see my modified example here: https://codesandbox.io/s/u0b4sCould you please explain why it works in this case without the manual setting of the
State
value? In my application, I only have the problem when the display value for the lookup column is a numeric value, as I discovered lately and mentioned above.Thanks again,
Jan.
Hi Jan,
Thank you for the update. I checked this example and see that a lookup column has a caching mechanism that works when values are strings. The behavior is indeed inconsistent and we need more time to research it. Please bear with us.
Note that the
State
object is not created in both cases after inserting a new row. So, I would still recommend you usesetCellValue
if you need to apply changes to theState
column in your real application.Hey Artem,
Thanks for the update!
Actually, I don't want to set the
State
object, thus I setvalueExpr="ID"
in the lookup. And in the column I havedataField="StateID"
… so in this example it actually does what I need, a new entry gets theStateID
attribute set correctly and the selected state is shown in the cell … see this example again: https://codesandbox.io/s/u0b4sThanks,
Jan.
Thank you for the clarification. We'll make an update here once we have any news.