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

[Solved] How to add custom button in toolbar that calls Javascript function to run an Action?

1 Answer 309 Views
Grid
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Max
Top achievements
Rank 1
Max asked on 14 Feb 2012, 06:23 PM
Hi all
Been looking at forum, so many variations and ways to do things. It gets overwhelming.
I am trying to add a custom button which will call an action on my controller but the action needs to be called from a Javascript function because I need to pass a Javascript value as a parameter. Here is what I got...just pieces of ideas but nothing concrete yet:

@(Html.Telerik().Grid((IEnumerable<ViewModels.NormalMode.FullWindowsStats>)ViewData["FullWindowsStats"])
        .Name("TopGrid")
        .NoRecordsTemplate("No records to display.")
        //.ToolBar(commands => commands.Custom().Text("Export to CSV").Action("ExportCsv", "NormalMode")) //this works but I need to pass a javascript variable as param
         
        //.ToolBar(commands => commands.Custom().ButtonType(GridButtonType.ImageAndText)
        //    .ImageHtmlAttributes(new { @class = "t-icon t-edit", onclick = "runAction('test')" }).Text("CSV"))
 
        .ToolBar(toolBar => toolBar.Template(@<text><a class="t-button t-grid-add">Export CSV</a> <span id="my-status-message"/></text>))
...
 
    function runAction(name) {
        alert(name);
        // ajaxRequest('@Url.Action("Action", "Controller")', { param1: variable1 });
    }


Any ideas? Thank you!

1 Answer, 1 is accepted

Sort by
0
bbeatty
Top achievements
Rank 1
answered on 20 Jul 2012, 05:10 PM
 @{ Html.Telerik().Menu()
                    .Name("ToolbarMenu")
                    .Items(menu =>
                    {                       
                            menu.Add()
                                .Text("Edit")
                                .Action("Method","Controller")
                                .ImageUrl("edit.png")
                                .LinkHtmlAttributes(new { rel = "delete" });
;                         )                     .ClientEvents(events => events.OnSelect(
"toolBarOnSelect"))                     .Render(); }
<script type="text/javascript">
  function toolBarOnSelect(e) {
        var link = $(e.item).find('a');
 
        if (link.attr('rel') == 'delete') {
            if (!confirm("Are you sure you want to delete this school?")) {
                e.preventDefault();
            }
        }
    }
</script>
OR
@(Html.Telerik().Grid<GridItem>().Name("Grid")
     .ToolBar(commands =>                 {
commands.Custom().Text("Import").Url("#").HtmlAttributes(new { @class = "ImportButton" });
 })
)
    $(document).ready(function () {
        $(".ImportButton").click(function ()
            {
                alert("hi");
            });
    });

Tags
Grid
Asked by
Max
Top achievements
Rank 1
Answers by
bbeatty
Top achievements
Rank 1
Share this question
or