简体   繁体   English

ASP.NET MVC部分视图jquery数据表

[英]ASP.NET MVC partial view jquery datatables

I have a problem with jQuery Datatables plugin in mvc. 我在mvc中的jQuery Datatables插件有问题。

I have a view with ajax-form whitch load a partial view with table on sumbit. 我有一个用ajax形式的视图加载sumbit表的局部视图。

View 视图

...
    @using (Ajax.BeginForm("MyAction", "MyController", new AjaxOptions { UpdateTargetId = "myList" }, new { @id = "searchForm", @class = "form-horizontal" }))
    {
                                // some html  
    }
    <div id="myList">

    </div>

Partial View with table 带表的局部视图

@model IEnumerable<Item>
<table id="productsTable">
    <thead>
    <tr>
        <th>Name</th>
        <th>Description</th>
        <th></th>
    </tr>
    </thead>
    <tbody>
    @foreach (var item in Model)
    {
        <tr>
            <td>@Html.DisplayFor(x => item.Name)</td>
            <td>@Html.DisplayFor(x => item.Description)</td>
            <td>
                <div>
                    <a href="@Url.Action("Edit", "MyController", new {id =            item.Id})">
                    </a>
                    <a href="@Url.Action("Delete", "MyController", new {id = item.Id})" >
                    </a>
                </div>
            </td>
        </tr>
    }
    </tbody>
</table>

The problem is that if I put script 问题是如果我放脚本

$(document).ready(function() {
   $('#productsTable').DataTable();
});

in main View it will be not work because partial view load by ajax. 在主视图中,它将无法正常工作,因为ajax会加载部分视图。 And if I put this script in partial view it will be work with delay: at first user will see simple html table and than it will be convert to datatable. 而且,如果我将此脚本放在局部视图中,它将延迟工作:最初,用户将看到简单的html表,然后它将转换为datatable。

Is there any way to correct load partial view with datatable? 有什么办法可以用数据表更正加载局部视图吗? Thanks for advices. 感谢您的建议。

You can't do anything on the server to "preload" the datatable, since it can only be run on the client. 您无法在服务器上执行任何操作来“预加载”数据表,因为它只能在客户端上运行。 To run DataTable() , the browser needs to have loaded both jQuery and HTML before you can transform it, which is why there is a "blink" of ugly. 要运行DataTable() ,浏览器需要先加载jQuery和HTML才能进行转换,这就是为什么丑陋的原因。

I suggest you load it (with Ajax if you wish) into a div with CSS position:absolute; 我建议您将其加载(如果需要,使用Ajax)到具有CSS position:absolute;的div中position:absolute; and visibility:hidden; visibility:hidden; (not display:none; and position it somewhere off screen (for example with left: -3000em; or similar). After everything is loaded and DataTable() has run, you can move the div to where it should be with JS and toggle position and visibility back to normal. (不display:none;然后将其放置在屏幕外的某个位置(例如, left: -3000em;或类似位置)。加载left: -3000em;所有内容并运行DataTable() ,您可以使用JS将div移至应位于的位置并切换positionvisibility恢复正常。

You can even do this with a sweet fade-in effect, if you want. 如果需要,您甚至可以使用甜美的淡入效果进行此操作。 :) :)

$(document).ready(function() {
   $('#productsTable').DataTable();
});

you should place this code inside the success of ajax call. 您应该将这段代码放在ajax调用成功的内部。 I think this may solve your problem :) 我认为这可以解决您的问题:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM