JavaScript UI Libraries & Blazor Components Suite – Smart UI › Forums › Data Grid › Change cell value based on other cell value
Tagged: grid-cell onLoad
- This topic has 28 replies, 3 voices, and was last updated 1 year, 8 months ago by svetoslav_borislavov.
-
AuthorPosts
-
March 20, 2023 at 8:42 am #104580svetoslav_borislavovParticipant
Hi,
Can you please see the following example: https://codepen.io/svetoslavjqwidgets/pen/MWqqpKR
In it, if there isn’t a template, a new one is set.
If there is already a template, only the element is being modified.
May you do the same thing without render or refresh?Best Regards,
Svetoslav BorislavovSmart UI Team
https://www.htmlelements.com/March 20, 2023 at 11:38 am #104586PeterParticipantI will try to do the same thing, as it does what I need.
About the multiple renderings, I’ll have to see if the poses an actual problem once I get some real data into the grid.
March 21, 2023 at 4:51 am #104587svetoslav_borislavovParticipantHi,
Sure, if you face any further difficulties, do not hesitate to contact us!
Best Regards,
Svetoslav BorislavovSmart UI Team
https://www.htmlelements.com/- This reply was modified 1 year, 8 months ago by svetoslav_borislavov.
March 21, 2023 at 9:27 am #104589PeterParticipantI’m still having difficulties making this work.
If I use the approach with creating a template (if it doesn’t exist) with DIVs and then setting textContent, then I get the font-awesome icon markup (<i>…</i>) as text in the field, which is not the desired result.
If I use empty string ” as template (if no icon should be shown) and font-awesome icon markup if a icon should be shown, then it works the first time at least, going from no icon to an icon.
But when the value is changed so there should be no icon again, then it doesn’t work – the icon stays.In the sample below ImageAvailable == false should show no icon in File, while ImageAvailable == true should show an icon in File.
So what I’m trying to switch between is either ” or ‘<i class=”some-font-awesome-font”></i>’.`{
label: ”, dataField: ‘File’, cellsVerticalAlign: ‘middle’, verticalAlign: ‘middle’, align: ‘center’, cellsAlign: ‘center’, width: 120, freeze: ‘far’,
allowEdit: false, template: function (formatObject)
{
const data = formatObject.row.data;console.log(‘data.ImageAvailable: ‘ + data.ImageAvailable);
if (!formatObject.template)
{
console.log(‘formatObject.template == null’);
formatObject.template = ”;
}if (data.ImageAvailable == false)
{
formatObject.template = ”;
}
else if (data.ImageAvailable == true)
{
formatObject.template = ‘<i class=”some-font-awesome-icon”></i>’;
}
}
},
{
label: ”, dataField: ‘ImageAvailable’, width: 70, allowEdit: true, visible: true
}`March 21, 2023 at 9:53 am #104594svetoslav_borislavovParticipantHi,
If you are setting the icon with the <i> tag. You shouldn’t change the textContent property, but the innerHTML.
The difference is that the textContent does not parse the HTML tags to elements.
With the textContent you are setting the text of the element, but with the innerHTML you are setting the HTML content.Please see the demo below:
Here is the modified example: https://codepen.io/svetoslavjqwidgets/pen/MWqqpKR
Best Regards,
Svetoslav BorislavovSmart UI Team
https://www.htmlelements.com/March 21, 2023 at 11:00 am #104595PeterParticipantThank you! that fixed that issue.
Then I have another grid where I’m trying to do the same (example from earlier) where the value to act on is either 0, 1 or > 1.
The issue here though is, that at first when the grid is loaded the icons don’t show – but when any cell value is changed or even the grid is scrolled, all the icons appear, as if the column with the icons isn’t rendered initially.
I even tried calling grid.render(); but it doesn’t help. The grid is loaded the same way at the previous described one.
Actually, since all cells in the previous grid starts with ImageAvailable == false, the issue might be the same here – I wounld’t know because showing no icon is the default in this grid.
March 22, 2023 at 4:39 am #104600svetoslav_borislavovParticipantHi,
What is your code for the template function?
Can you share it with us?
Best Regards,
Svetoslav BorislavovSmart UI Team
https://www.htmlelements.com/March 22, 2023 at 8:08 am #104602PeterParticipantYes. This almost “works” – but the cells that should have icons from the start, don’t show their icon until I either scroll the grid, change window size or change the value of a cell – or similar change – then the icon appear right away as were they always there.
{ label: '', dataField: 'Attachments', cellsVerticalAlign: 'middle', cellsAlign: 'center', width: 50, freeze: 'far', allowEdit: false, template: function (formatObject) { const data = formatObject.row.data; if (!formatObject.template) { formatObject.template = '<div></div>'; } if (data.attachment_count != null) { if (data.attachment_count < 1) { formatObject.template.innerHTML = ''; } else if (data.attachment_count == 1) { formatObject.template.innerHTML = '<some fa icon A>'; } else if (data.attachment_count > 1) { formatObject.template.innerHTML = '<some fa icon B>'; } } else { formatObject.template.innerHTML = ''; } } }
- This reply was modified 1 year, 8 months ago by Peter.
March 22, 2023 at 8:25 am #104604svetoslav_borislavovParticipantHi,
This is happening because you initially set the template to an empty div element.
You should have your conditionals initially also.
The correct code is this below:const data = formatObject.row.data;
if (!formatObject.template) {
if (data.attachment_count != null) {
if (data.attachment_count < 1) {
formatObject.template = ‘<div></div>’;
}
else if (data.attachment_count == 1) {
formatObject.template = ‘<div><some fa icon A></div>’;
}
else if (data.attachment_count > 1) {
formatObject.template = ‘<div><some fa icon B></div>’;
}
}
else {
formatObject.template = ‘<div></div>’;
}
} else {
if (data.attachment_count != null) {
if (data.attachment_count < 1) {
formatObject.template.innerHTML = ”;
}
else if (data.attachment_count == 1) {
formatObject.template.innerHTML = ‘<some fa icon A>’;
}
else if (data.attachment_count > 1) {
formatObject.template.innerHTML = ‘<some fa icon B>’;
}
}
else {
formatObject.template.innerHTML = ”;
}
}Best Regards,
Svetoslav BorislavovSmart UI Team
https://www.htmlelements.com/- This reply was modified 1 year, 8 months ago by svetoslav_borislavov.
- This reply was modified 1 year, 8 months ago by svetoslav_borislavov.
March 22, 2023 at 8:30 am #104607PeterParticipantI’m only setting an empty div if there is no template object, just like your online demo showed.
The conditions are checked just after this to determine what the content (innerHTML) of the div should be if any.
There are no occasion where the conditions are not checked. In all and any case, the innerHTML is being set to either: ”, ‘<some fa icon A>’ or ‘<some fa icon B>’.
The lines:
if (!formatObject.template) { formatObject.template = '<div></div>'; }
Are not part of the conditions checks, they are standalone before any conditions are checked.
As far as I can tell, the code does the exact same thing, with half the lines of code more or less, or am I missing something?
March 22, 2023 at 8:35 am #104611svetoslav_borislavovParticipantHi,
You are setting only a string. The string is internally parsed to an element. The correct way is as my previous reply.
You may see in this demo also: https://www.htmlelements.com/demos/grid/column-dynamic-template-with-components/Best Regards,
Svetoslav BorislavovSmart UI Team
https://www.htmlelements.com/March 22, 2023 at 9:10 am #104612PeterParticipantThere is something I’m missing I think. I have now added what I see to be redundant code lines to make it look like the sample, but it still doesn’t work.
Isn’t the idea to do the following:
Check if there is a template (a div) and if there isn’t one, then add it and set it’s content (innerHTML) to something: ”, ‘<_A_>’ or ‘<_B_>’.
Or if there is one, just set it’s content (innerHTML) to something: ”, ‘<_A_>’ or ‘<_B_>’.This not understood correctly?
March 22, 2023 at 9:30 am #104615PeterParticipantMarch 23, 2023 at 4:45 am #104618svetoslav_borislavovParticipantHi,
Your code did not work because when you assign a string to the template, the string is parsed to an element after the function.
In other words, you were trying to set the innerHTML of a string.Best Regards,
Svetoslav BorislavovSmart UI Team
https://www.htmlelements.com/ -
AuthorPosts
- You must be logged in to reply to this topic.