簡體   English   中英

數據表 asp.net 核心剃刀頁面 IActionResult 新 JsonResult

[英]datatables asp.net core razor pages IActionResult new JsonResult

如何在選擇更改時設置表格內容我正在談論 datatables.js 庫,通過 jquery ajax 帖子。 我如何返回新的 JsonResult 並使用處理程序的結果重繪表的內容? 謝謝

這是我使用的過程:摘要:

  1. 在頂級范圍聲明一個 js 變量

  2. 在 document.ready 函數中,將數據表實例化為你的全局 js 變量

  3. 在 ajax post 上通過存儲的對象清除表,然后迭代結果並添加行

更深入:

//declare this high in scope so you can access it in your functions
var dt;

$(document).ready(function () {
   //Create the datatable and assign it to variable for later reference
   dt = $('#MyTable').DataTable({dom: 'Bfrtip'});
});

$("#SomeBtn").click(function () {
  //clear current table rows
  dt.clear().draw();
  $.ajax({
         url: whatever Path,
         data: whatever Data,
            dataType: "json",
            type: "POST",
            cache: false,
            contentType: "application/json; charset=utf-8",
            success: function (data) {
                $.each(data, function (i, n) {
                    //Iterate results and add each to the table. This is why we stored the datatable in a highr scope so we can operate it on it here :)
                    dt.row.add([n.prop1, n.prop2, n.prop3]).node().id = n.propID + '_Row';
                    dt.draw(false);

                });
            },
            error: function (response) {
            },
            failure: function (response) {

            }
        });
 });

如您所見,通過在實例化時將數據表存儲在對象中,您可以根據其 api 在以后的函數中控制它

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM