ASP.net
Awesome
Learn
Forum
Buy
Demos
Sign In
☾
☀
Switch to
Dark
Light
Mode
this site works best with javascript enabled
Ask Question
Having problems with adding a conditional button in a grid row
Title:
B
I
{code}
?
I'm trying to add a button on my grid that would call a view from my controller but only if a specific value in a field on the row existed. It looks like the best way would be to use ClientFormatFunc for this in order to check for the specific value's existence, but I'm having a problem creating the HTML in the function to be returned to the view. The error I'm seeing in Firefox is "invalid regular expression flag F". Does this have to do with improper single/double quotes in the function? The definition of the column is: new Column { ClientFormatFunc = "testCol", Header = "Test", Width = 85 } The function is: function testCol(load) { if (load.ViewFuelSolutionButton == "true") { return "<input type='button' value='Test' onclick='location.href='{1}' />", @Url.Action("FuelSolution", "Home", new { legNumber = ".(Id)" }); } } How then can I hide or display a button in my grid based on the value of a field? I'm rather new to this and I'm struggling in understanding how to make this work. Also, how can I have that open in a pop-up instead of redirecting to another page?
Save Changes
Cancel
Sean Davidson
asked at 10 Aug 2020
Answers
B
I
{code}
?
your js is invalid, it seems that you're imagining some sort of c# string.format, and `.(Id)` is only to be used inside ClientFormat; --- if your struggling start simple and minimal, first try this: function testCol(load) { if (load.ViewFuelSolutionButton) { return 'a'; } return 'b'; } after you get this working, replace the `'a'` with a button, and after you get the button, add the onclick: function testCol(load) { if (load.ViewFuelSolutionButton) { return '<button type="button" class="awe-btn" onclick="openFuelSolution(' + load.Id + ')">Open</button>'; } return ''; } function openFuelSolution(id) { location.href = '@Url.Action("FuelSolution", "Home")?id=' + id; } To open a popup instead of going to another page you could have this: function condBtnPopup(load) { if (load.ViewFuelSolutionButton) { return '<button type="button" class="awe-btn o-pad" onclick="awe.open(\'details\', {params:{ id: ' + load.Id + ' }}, event)">details</button>'; } return ''; } and init the popup: @Html.Awe().InitPopup().Name("details").Url(@Url.Action("FuelSolution", "Home"))
Save Changes
Cancel
Omu
answered at 10 Aug 2020
Perfect, thanks so much!
at 10 Aug 2020
Sean Davidson
After responding I realized that I'm still having a problem with this. If I try clicking on the button a subsequent time after the first, it won't work. I see this message in the console of the browser after clicking it the first time: Uncaught TypeError: p is undefined Below that in the console, if I click on 'anonymous', it's showing 'parse' as being the problem. What would cause this?
at 11 Aug 2020
Sean Davidson
perhaps the popup content contains the `_Layout.cshtml` so it loads all the scripts again, if you can't find the problem follow the instructions here: https://www.aspnetawesome.com/learn/mvc/CommonProblems#Isolate-the-problem
at 11 Aug 2020
Omu
please
Sign In
to leave an answer
By accessing this site, you agree to store cookies on your device and disclose information in accordance with our
cookie policy
and
privacy policy
.
OK