MVC 3 Webgrid make entire row clickable

≯℡__Kan透↙ 提交于 2019-11-30 05:03:07

问题


I am using webgrid in my razor view in MVC 3. Below is how my webGrid looks, I want to make the entire row clickable and on click pass values to the javascript method also.

I am able to achieve calling my javascript method on text of all columns. I want the same to happen for click of anywhere on the entire row.

Please guide me on this. Thanks

           @grid.GetHtml(

            columns: grid.Columns(

            grid.Column("CompanyName", format: @<text><a href="javascript:SubmitValues('@item.Col1','@item.Col2','@item.Col3');">@item.CompanyName</a></text>, header: "ABC"),

            grid.Column("CompanyAddress", format: @<text><a href="javascript:SubmitValues('@item.Col1','@item.Col2','@item.Col3');">@item.CompanyName</a></text>, header: "DEF"),

            ))      

         }

回答1:


You have to use JQuery to add row click functionality

Add htmlAttributes: new { id="MainTable" } in your webgrid.

<script type="text/javascript">
   $(function () {
        var tr = $('#MainTable').find('tr');
        tr.bind('click', function (event) {
            var values = '';
            var tds = $(this).find('td');


            $.each(tds, function (index, item) {
                values = values + 'td' + (index + 1) + ':' + item.innerHTML + '<br/>';
            });
            alert(values);


        });
    }); 


</script>



回答2:


I have done this in my project with adding a class style: "click_able" on specific column like.

grid.Column("CompanyName", style: "click_able", format: @<text><a href="javascript:SubmitValues('@item.Col1','@item.Col2','@item.Col3');">@item.CompanyName</a></text>, header: "ABC"),

and then add click function like.

<script type="text/javascript">
$(".click_able").click(function () {
    // Do something 
});

It working fine in my case.



来源:https://stackoverflow.com/questions/10445690/mvc-3-webgrid-make-entire-row-clickable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!