简体   繁体   English

单击链接后如何重绘jQuery DataTable而不从页面导航?

[英]How to redraw jQuery DataTable after link is clicked without navigating from page?

I have a column in my jQuery datatable that renders a green checkmark link if data == true or a red X if data == false:我的 jQuery 数据表中有一个列,如果 data == true 呈现绿色复选标记链接,如果 data == false 呈现红色 X:

{
    data: "HasPayment",
    render: function (data, type, row) {
        var paymentSet = '@Url.Action("Set", "Payment")?applicationId=' + row.Id + '&year=' + row.Year + '&month=' + row.Month + '&hasPayment=' + data;
        if (data) {
            return '<a href=\"' + paymentSet + '\" class="fas fa-solid fa-check" style="color: green"></a>';
        }
        return '<a href=\"' + paymentSet + '\" class="fas fa-solid fa-times" style="color: red"></a>';
    }
},

The problem is that when I click one of the links (either green checkmark or red X), it navigates to another page.问题是当我单击其中一个链接(绿色复选标记或红色 X)时,它会导航到另一个页面。 I know that this is because I am using href and Url.Action .我知道这是因为我使用的是hrefUrl.Action

When a user clicks one of the links, I want to call the /Payment/Set method to update the data (green checkmark to red X and vice versa) and then I want to redraw my datatable (ie dataTable.draw() ) without navigating from the current page (ie Index view).当用户单击其中一个链接时,我想调用/Payment/Set方法来更新数据(绿色复选标记为红色 X,反之亦然),然后我想重绘我的数据表(即dataTable.draw() )而不从当前页面导航(即索引视图)。 /Payment/Set method updates the data without returning anything (ie void). /Payment/Set方法更新数据而不返回任何内容(即无效)。

Update : I tried the following and it almost works, meaning that when I click one of the links, the data is updated and the datatable is refreshed, except it still tries to navigate to another page.更新:我尝试了以下方法,它几乎可以工作,这意味着当我单击其中一个链接时,数据会更新并且数据表也会刷新,但它仍会尝试导航到另一个页面。

{
    data: "HasPayment",
    render: function (data, type, row) {
        var paymentSet = '@Url.Action("Set", "Payment")?applicationId=' + row.Id + '&year=' + row.Year + '&month=' + row.Month + '&hasPayment=' + data;
        if (data) {
            return '<a href=\"' + paymentSet + '\" onclick="return onclickFunction()" class="fas fa-solid fa-check" style="color: green"></a>';
        }
        return '<a href=\"' + paymentSet + '\" onclick="return onclickFunction()" class="fas fa-solid fa-times" style="color: red"></a>';
    }
},


<script>
    function onclickFunction() {
        $.ajax({
            type: "GET",
            url: $(this).attr("href"),
            success: function () {
                paymentDataTable.ajax.reload();
            }
        });
    }
</script>

If you use an anchor <a> it is obvious that the browser will navigate to another page.如果您使用锚点<a> ,很明显浏览器将导航到另一个页面。 That's the actual purpose of the anchor tag.这就是锚标签的实际用途。

You should use an AJAX function to call when your button is pressed and at the callback call the reload of the table.您应该使用 AJAX 函数在按下按钮时调用,并在回调调用表的重新加载。

This can be a way:这可以是一种方式:

{
    data: "HasPayment",
    render: function (data, type, row) {            
        if (data) {
            return '<button class="fas fa-solid fa-check" style="color: green" onclick="setPayment(row.Id, row.Year, row.Month, data)"></button>';
        }
        return '<button class="fas fa-solid fa-times" style="color: red" onclick="doSomethingElseMaybe()"></button>';
    }
}

Then you should create two more functions, one with the AJAX call and one with the table reload to be called as callback.然后您应该再创建两个函数,一个使用 AJAX 调用,另一个使用表重新加载作为回调调用。

function setPayment(id, month, year, data){
var url = `Payment/Set?applicationId=${id}&year=${year}&month=${month}&hasPayment=${data}`;
$.ajax({
    type: "POST", //OR GET? it depends on your backend
    url: url,
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function (result) {
        reloadTable();
    },
    error: () => {
        console.error("500 Internal Server Error.");}
});
}

Then in the reloadTable function just call the ajax.reload() method of the DataTable api.然后在 reloadTable 函数中调用 DataTable api 的 ajax.reload() 方法。

for example例如

function reloadTable(){
datatable.ajax.reload();
}

Here is the documentation of DataTables reload method.这是 DataTables reload 方法的文档

Here is the documentation of JQuery AJAX.这是 JQuery AJAX 的文档

You may have to adapt the example code to your specific backend purpose.您可能必须根据您的特定后端目的调整示例代码。

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

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