【问题标题】:How to pass id from jquery datatable to a other jquery function如何将 id 从 jquery 数据表传递到其他 jquery 函数
【发布时间】:2020-04-02 21:53:10
【问题描述】:

我想将一个 Id 从 jquery 数据表传递给另一个 jquery 函数。

这是我也想传递 Id 的 jquery 函数

   $("#makeEditable").on('mousedown.save', "i.save.material-icons", function (e) {
                    $.ajax({
                        type: 'GET',
                        url: '/gestiondeubicaciones/Editar',
                        data: { id: 3 }, /* How to pass  id here???*/
                        cache: false,
                        success: function (result) {

                        }
                    });
                    $(this).text("edit").removeClass().addClass("edit material-icons");
                    var $row = $(this).closest("tr");
                    var $tds = $row.find("td").not(':last');//.not(':first');

                    $.each($tds, function (i, el) {
                        var txt = $(this).find("input").val()
                        $(this).html(txt);
                    });
                });

更具体地说,我想用变量替换 3

                    data: { id: 3 }, /* How to pass  id here???*/

这是我的数据表代码

var table;
            table = $('#makeEditable').DataTable({
                rowReorder: {
                    selector: 'td:nth-child(2)'
                },
                responsive: true,
                ajax: "/gestiondeubicaciones/GetUbicaciones",
                columns: [
                    { data: "armario" },
                    { data: "cajon" },
                    {
                        data: "ubicacion_id", "width": "50px", "render": function (data) {
                            return '<a class="popup-edit"><i id="editSave" class="edit material-icons" title="Detalles">edit</i></a>' +
                                '<a class="popup-delete" href="#" onclick="DeleteData(' + data + ');" title="Eliminar"><i class="delete material-icons">delete</i></a>'; 
                        }
                    }
                ]

更具体地说,我想将数据传递给 jquery 函数。

 function (data) {
                                return '<a class="popup-edit"><i id="editSave" class="edit material-icons" title="Detalles">edit</i></a>'

如何将数据从 jquery 数据表传递到 jquery 函数。谢谢您的帮助。

【问题讨论】:

    标签: jquery asp.net-mvc razor datatables


    【解决方案1】:

    1.然后在您的数据表中的通行证 ID (ubicacion_id) 中-

    {
        data: "ubicacion_id", "width": "50px", "render": function (data) {
        return '<a class="popup-edit"><i id="editSave"  data-id="' + data + '" class="edit material-icons" title="Detalles">edit</i></a>'; 
      }
    

    2.在你的jquery点击事件中-

    data: { id: $(this).attr("data-id") },
    

    1.然后在您的数据表中的通行证 ID (ubicacion_id) 中-

    {
        data: "ubicacion_id", "width": "50px", "render": function (data) {
        return '<a class="popup-edit"><i id="editSave" onmousedown="EditData(' + data + ');" class="edit material-icons" title="Detalles">edit</i></a>'; 
      }// you can onclick rather than onmousedown event
    

    2.在你的jquery点击事件中-

    function EditData(id) {
        $.ajax({
            type: 'GET',
            url: '/gestiondeubicaciones/Editar',
            data: { id: id },
            cache: false,
            success: function (result) {
    
            }
        });
        $(this).text("edit").removeClass().addClass("edit material-icons");
        var $row = $(this).closest("tr");
        var $tds = $row.find("td").not(':last');//.not(':first');
    
        $.each($tds, function (i, el) {
            var txt = $(this).find("input").val()
            $(this).html(txt);
        });
    }
    

    如果你想在数据表中获取当前row中的任何或所有数据,那么-

    { "width": "50px", "render": function (data, type, row) { return '<a class="popup-edit"><i id="editSave" onmousedown="EditData(' + row.ubicacion_id + ');" class="edit material-icons" title="Detalles">edit</i></a>'; }
    

    【讨论】:

    • 您的解决方案有效并回答了我的问题,但是我意识到我应该传递完整的模型而不仅仅是 id 如何也传递 armario 和 cajon?将不胜感激。我已批准您的回答,因为它回答了我提出的问题。谢谢。
    • { "width": "50px", "render": function (data, type, row) { return "&lt;a href='#' class='popup' onclick=Delete('" + row.proveedor_id + "'); title='Eliminar'&gt;&lt;i class='material-icons'&gt;&lt;/i&gt;delete&lt;/a&gt;"; }, row= 您在数据表中的当前行。
    猜你喜欢
    • 2015-07-20
    • 2015-12-22
    • 1970-01-01
    • 2011-11-05
    • 1970-01-01
    • 2020-02-23
    • 1970-01-01
    • 2013-01-28
    • 1970-01-01
    相关资源
    最近更新 更多