【问题标题】:I want a button beneath my datatable to show up if I have only 1 record. How can I pass the ID of the record in table when button is clicked?如果我只有 1 条记录,我希望在我的数据表下方显示一个按钮。单击按钮时如何传递表中记录的ID?
【发布时间】:2020-04-22 09:05:35
【问题描述】:

这是带有我想要它们的按钮的表格的 chtml 代码。我还包括了表格的 javascript 和绘图按钮的 .cs 控制器。如果我在表格行中放置一个按钮并在单击时传递记录 ID,则控制器可以工作,但我不希望该按钮位于该行中。这是因为该按钮有意仅在数据表被过滤为剩余一条记录时才可见。

<br />
<div class="container row p-0 m-0">
    <div class="col-6">
        <h2 class="text-info">Device List</h2>
    </div>
    <div class="col-12 border p-3">
        <table id="DT_load" class="table table-striped table-bordered">
            <thead>
                <tr>
                    <th>Drawing File</th>
                    <th>Barcode</th>
                    <th>Date Modified</th>
                    <th>Location</th>
                    <th></th>
                </tr>
            </thead>
        </table>

    </div >
    <div class="row" >
        <a asp-action="Drawing" class="btn-primary" id="btn1" style="width: 100px" value="0">
            View Drawing
        </a>
        <a asp-action="Location" class="btn-primary" id="btn2" style="width: 100px">
            View Location
        </a>
    </div>
</div>

这是我的javascript

$(document).ready(function () {
    loadDataTable();
});


function loadDataTable()
{
    dataTable = $('#DT_load').DataTable
    ({
        "ajax":
        {
            "url": "/devices/getall/",
            "type": "GET",
            "datatype": "json"
        },
        "columns":
        [
            { "data": "dwgfilename", "width": "20%" },
            { "data": "barcode", "width": "20%" },
            { "data": "moddate", "width": "20%" },
            { "data": "locationdwg", "width": "20%" },
            {
                "data": "id",
                "render": function (data)
                {
                    return `<div class="text-center">
                        /*ignore*/
                        </div>`;
                }, "width": "60%"
            }
        ],

        'infoCallback': function (settings, start, end, max, total, pre)
            {
                if (total == 1)
                {    
                    $("#btn1").show();
                    $("#btn2").show();
                    return 'Showing your record';
                }
                else 
                {
                    $("#btn1").hide();
                    $("#btn2").hide();
                    return 'Showing ' + total + ' of ' + max + ' records';
                }
            },

        "width": "100%"
    });
}

这里是按钮的控制器

public IActionResult Drawing(int id)
        {
            //draw
            Device = _db.Devices.FirstOrDefault(u => u.Id == id);
            if (Device == null)
            {
                return NotFound();
            }
            var filename = Device.Dwgfilename;
                var stream = new FileStream(@"c:\users\boxworks\documents\" + filename +".pdf", FileMode.Open);
                return new FileStreamResult(stream, "application/pdf");

        }

【问题讨论】:

    标签: javascript c# button asp.net-core-mvc parameter-passing


    【解决方案1】:

    参考下面的工作演示并修改你的javascript:

    function loadDataTable() {
            dataTable = $('#DT_load').DataTable
                ({
                    "ajax":
                    {
                        "url": "/home/getall/",
                        "type": "GET",
                        "datatype": "json",
                        "dataSrc":""
                    },
                    "columns":
                        [
                            { "data": "orderNumber" },
                            { "data": "orderDate" },
                            { "data": "description" },
                            {
                                "data": "orderId",
                                "render": function (data) {
                                    return `<lable hidden>`+data+'</lable>';
                                }, "width": "60%"
                            }
                        ],
    
                    'infoCallback': function (settings, start, end, max, total, pre) {
                        if (total == 1) {
                            var id = $("#DT_load tbody tr").find("lable").text();
    
                            $("#btn1").attr("href",$("#btn1").attr("href") + "?id=" + id);
                            $("#btn1").show();
                            $("#btn2").show();
                            return 'Showing your record';
                        }
                        else {
                            $("#btn1").hide();
                            $("#btn2").hide();
                            return 'Showing ' + total + ' of ' + max + ' records';
                        }
                    },
    
                    "width": "100%"
                });
        }
    

    结果:

    【讨论】:

    • 好吧,你太棒了。非常感谢!你是怎么想出来的?为了不必在论坛上提问,我应该用谷歌搜索什么?顺便说一句,它现在仅在调试模式下对我有用,并且如果我在 infoCallback 中设置了一个断点。我认为在从数据库中读取 id 之前它正在向前推进,因此它将 null 发送到 IActionResult。我需要弄清楚这一点,但你让我提前了光年。再次感谢!
    • 如果我的解决方法有效,您能否将我的回复标记为答案?这将帮助面临相同或类似问题的其他人快速找到问题。您可以为新问题发布一个新主题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-18
    相关资源
    最近更新 更多