This is a migrated thread and some comments may be shown as answers.

Run ActionResult AND javascript function from Custom ToolBar button

1 Answer 14 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Alex
Top achievements
Rank 1
Alex asked on 07 Apr 2016, 09:21 PM

I am using an MVC grid that has a toolbar, with a custom button

toolBar.Custom().Text("Recalculate Assets").Action("Edit", "AssetPlanning", new { recalculate = true }).HtmlAttributes(new { title = "Warning: This could take multiple minutes to fully complete", data_toggle = "tooltip", id = ("calcButton" + uniqueName), onClientClick = "alert('reclacing');" });

 

This actionresult can take upto a minute to run. I would at the very least like to update the button to say calculating with a little spinner and whatnot

 

var buttonID = "#calcButton" & String(@uniqueName.ToString());
 
$(buttonID).click(function () { recalculating(); });
 
 
function recalculating() {
 
    alert("reclacing");
 
    $(buttonID).addClass("btn btn-lg btn-warning");
    $(buttonID).html('<span class="glyphicon glyphicon-refresh glyphicon-refresh-animate"></span> Recalculating');
}

 

I would think that somewhere along the way the function would get called or an alert box would show, but nothing appears on the client side. I'm guessing that the action gets called first and the client click event just is never called. What can I do to make the client code called first or run together?

1 Answer, 1 is accepted

Sort by
0
Dimiter Topalov
Telerik team
answered on 11 Apr 2016, 12:15 PM

Hello Alex,

The code sample suggests that there is a JavaScript error thrown, and the click event handler is not attached at all. In the following line:

var buttonID = "#calcButton" & String(@uniqueName.ToString());

... a bitwise operator (&) is incorrectly used on string operands, resulting in a 0 (zero) being assigned to the buttonID variable. This causes a JavaScript error on the next line, as $(0) is not a valid selector.

You can find more information about operators in JavaScript and their corresponding operands in the following reference:

http://www.w3schools.com/jsref/jsref_operators.asp

Please check the developers tools console for JavaScript errors, and fix them.

I hope this helps, but let us know, if I am missing something, or you need further assistance.

Regards,
Dimiter Topalov
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Tags
Grid
Asked by
Alex
Top achievements
Rank 1
Answers by
Dimiter Topalov
Telerik team
Share this question
or