Trying to open a modal from a datatable rowclick

Trying to open a modal from a datatable rowclick

saritha248saritha248 Posts: 3Questions: 1Answers: 0

Iam working with datatables example and getting an error like this when trying to open a modal from the datatable rowclick: Datatables warning(table id = 'example'): cannot reinitialise data table.
i'm using jquery to open a modal.
Here is my code, I have 2 hyperlink columns in a row when i click on edit i want to open a form in modal, The modal opens but along with datatbale warning alert

$(document).ready(function () {

$('#pendingTab tfoot th').each(function () {
    $(this).html('<input type="text" />');
}); 
$('#demo2').html('<table width="100%" class="table table-hover table-condensed stripe compact order-column" id="pendingTab" cellspacing="0"></table>');

var oTable = $('#pendingTab').DataTable({       
    "processing": true,
    "paging": true,
    "deferRender": true,
    "ajax": {
        "type": "GET",           
        "url": '/Owner/p_Move',
        "contentType": 'application/json; charset=utf-8',
         "dataSrc": ""
    },
    "columns": [
         {
             "data": "prop_id",
             "className": 'propid'
         },

   {
       "data": "name",
       "className": 'b_name'
   },
   {
       "data": "bed_id",
        "className": 'bed'
   },
   { "data": "unit_id" },
   {
       "data": "email",
       "className": 'b_email'
   },
   {
       "data": "move_in_date"                
   },
    {
        "data": "bed_id",
        "sortable": false,
        "render": function (data, type, full, meta) {
            return '<a class="cancel">  Cancel  </a>';
        }
    },
    {
        "data": "bed_id",
    "sortable": false,
    "render": function (data, type, full, meta) {
        return '<a class="edit">  Edit  </a>';
    }
}   

    ],
    "order": [0, "asc"]

});

oTable.columns().every(function () {
    var that = this;

    $('input', this.footer()).on('keyup change', function () {
        that
            .search(this.value)
            .draw();
    });
});


$('#pendingTab tbody').on('click', 'a.edit', function () {        
    var tr = $(this).closest('tr');
    var Bid = tr.find(".bed").html()
    var name = tr.find(".b_name").html()
    var email=tr.find(".b_email").html()
    $.ajax({
        url: '/Owner/Edit_mIn/',
        data: {
            'Bid': Bid,
            'name': name,
            'email':email,
            format: 'json'
        },
        dataType: 'html',
        type: 'GET',
        success: function (data) {
            $('#Pending').modal("hide");
            $('#editModal').html(data);
            $('#edit_mIN').modal("show");
        },
    });

});

});

This question has an accepted answers - jump to answer

Answers

  • bindridbindrid Posts: 730Questions: 0Answers: 119

    The first problem I see is

    $('#demo2').html('<table width="100%" class="table table-hover table-condensed stripe compact order-column" id="pendingTab" cellspacing="0"></table>');
    

    you need to add <thead></thead><tbody></tbody>

    $('#demo2').html('<table width="100%" class="table table-hover table-condensed stripe compact order-column" id="pendingTab" cellspacing="0"><thead></thead><tbody></tbody></table>');
    

    next: since the table has not <th> tags, you need to add title to your columns


       "columns": [          {              "data": "prop_id",              "className": 'propid', "title":Prop Id"          },      {        "data": "name",        "className": 'b_name', "title":"Name"    },    {        "data": "bed_id",         "className": 'bed', "title":"Bed"    },    { "data": "unit_id" },    {        "data": "email",        "className": 'b_email', "title":"Email"    },    {        "data": "move_in_date"  , "title":"Move In Date"             },     {         "data": null,         "sortable": false, "title":"Cancel",         "render": function (data, type, full, meta) {             return '<a class="cancel">  Cancel  </a>';         }     },     {         "data": null,     "sortable": false, "title":"Edit",     "render": function (data, type, full, meta) {         return '<a class="edit">  Edit  </a>';     } }         ],

    Then next issue you are going to run into is that you are creating the table while it is still hidden instead of after the modal is popped up. Many things in html and jquery end up looking funny when they are not visible when created. But try it before you change it. Sometimes it is an issue, sometimes not.

    $('#pendingTab tbody').on('click', 'a.edit', function () {        
        var tr = $(this).closest('tr');
       var rowData = $("#pendingTab").DataTable.rows(tr).data()[0];
    
        $.ajax({
            url: '/Owner/Edit_mIn/',
            data: {
                'Bid': rowData.bed_id,
                'name': rowData.name,
                'email':rowData.email,
                 // what is this next line for?
                format: 'json'
            },
    // data type is what is the type of data being sent to the server, not what is being returned.
    // use contentType  to set type being returned from server
            dataType: 'json',
            type: 'GET',
            success: function (data) {
                $('#Pending').modal("hide");
                $('#editModal').html(data);
                $('#edit_mIN').modal("show");
            },
        });
     
    });
    

    The first time the modal with the table is popped up, you create the table.

    After that, you just have to clear, the reload the data.

    or
    recreate the table each time the modal opens
    but you have to destroy the table when its closed or reopened to stop that error.

  • bindridbindrid Posts: 730Questions: 0Answers: 119

    I don't see the html this runs on, I have to make some guesses

  • saritha248saritha248 Posts: 3Questions: 1Answers: 0
    edited May 2017

    I have <thead><tbody> and <th> tags in html, so i did'nt give it in the script file.
    Can ypu please let me know how to destroy the table when it is closed??

  • bindridbindrid Posts: 730Questions: 0Answers: 119
    Answer ✓

    var table = $("#example").DataTable();

    table.destroy(true);

    True causes the DataTable to be remove along with the table html.
    False will remove the DataTable but leave behind the html

    https://datatables.net/reference/api/destroy()

  • saritha248saritha248 Posts: 3Questions: 1Answers: 0

    Thank you so much @bindrid , That solved the issue

This discussion has been closed.