Click here to Skip to main content
15,892,298 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a datatable i loaded with records, i want to be able to click on a row button, the row button will display a modal dialog, populate the modal text fields with corresponding row values using Ajax object.

i want if a user clicks on the edit button on the datatable, a modal dialog will display with the corresponding row record, i have tried a few research online, but i have not been able to implement this, a section in my ajax function shows what i have tried that is not working. Any assistance will be appreciated

What I have tried:

My Ajax Function:

JavaScript
<script type="text/javascript">
$(document).ready(function () {

    var table = $("#tblAppointment").DataTable();
    $("#UpdateButton").click(function () {

        $.ajax({
            url: '/Appointment/EditPatientAppointment/',
            type: "POST",
            data: JSON.stringify({ AppointmentIdEdit: $("#AppointmentIdEdit").val(), appointmentDate: $(".datetimepickerEdit").val(), patientRegNo: $("#patientRegNoEdit").val(), reasons: $("#reasonsEdit").val() }),
            cache: false,
            dataType: "json",
            contentType: "application/json; charset=utf-8",
            success: function (_data) {
                $(".spina").hide();
                //$("#patientRegNo").text(_data.MatricRegNo + " " + _data.PatientName)
                if (_data.f !== undefined) {
                    swal({
                        title: "Failed!",
                        text: _data.f,
                        type: "info"
                    });
                    table = $("#tblAppointment").DataTable();
                    return false;
                }
                else {
                    swal({
                        title: "Success!",
                        text: _data.s,
                        type: "success"
                    });
                }
                table = $("#tblAppointment").DataTable();
               // return false;

                var arr = $.map(JSON.parse(_data.data), function (el) { return el });

                if (arr.length === 0) {
                    swal({
                        title: "No Record Found!",
                        text: _data.f,
                        type: "info"
                    });
                    table = $("#tblAppointment").DataTable();
                    return false;
                }
                table.clear();
                table.destroy();
                $('#tblAppointment').dataTable({
                    data: arr,
                    columns: [
                        { "data": "MatricRegNo" },
                        { "data": "PatientName" },
                        { "data": "EntryDate" },
                        { "data": "AppointmentDate" },
                        { "data": "Reasons" },
                        {
                            "data": function (data, type, row, meta) {
                                return '<span class="btn btn-info btn-sm edit" data-toggle="modal" data-target="#modal-Edit"></span>';

                                //My code to retrieve and display on modal text fields starts here

                                $(".editA[data-target='#modal-Edit']").click(function () {
                                    var columnHeadings = $("thead th").map(function () {
                                        return $(this).text();
                                    }).get();
                                    columnHeadings.pop();
                                    var columnValues = $(this).parent().siblings().map(function () {
                                        return $(this).text();
                                    }).get();
                                    var myModal = $('#modal-Edit');
                                    $('#AppointmentIdEdit', myModal).val(data.MatricRegNo);
                                    $('#patientRegNoEdit', myModal).val(data.MatricRegNo);
                                    $('.appointmentDateEdit', myModal).val(data.AppointmentDate);
                                    $('#reasonsEdit', myModal).val(data.Reasons);
                                    console.log("Column Values: " + data.Reasons);

                                    myModal.modal({ show: true });
                                    return false;
                                });

                                //My code to retrieve and display on modal text fields ends here

                            }
                        }
                    ],
                    dom: 'Bfrtip',
                    buttons: [
                        'copy', 'csv', 'excel',
                        {
                            extend: 'pdfHtml5',
                            orientation: 'Portriat',
                            pageSize: 'A4'
                        }
                    ]
                });
                table = $("#tblAppointment").DataTable();
            }
        });
    });
});


My Modal Code:

HTML
<div class="modal fade" id="modal-Edit">
                    <div class="modal-dialog">
                        <div class="modal-content">
                            <div class="modal-header">
                                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                                    <span aria-hidden="true">×</span>
                                </button>
                                <h4 class="modal-title">Edit Appointments</h4>
                            </div>
                            @using (Html.BeginForm())
                            {                                
                                <div class="modal-body">

                                    <div class="form-horizontal">
                                        <div class="form-group">
                                            <label id="lblAppointmentIdEdit" class="control-label col-md-2" style="display:none;">AppointmentId:</label>
                                            <div class="col-md-10">
                                                <input type="text" value="" id="AppointmentIdEdit" name="AppointmentIdEdit" class="form-control appointmentIdEdit" style="display:none;" />
                                            </div>
                                        </div>

                                        <div class="form-group">
                                            <label id="lblPatientRegNoEdit" class="control-label col-md-2">RegNo:</label>
                                            <div class="col-md-10">
                                                <input type="text" value="" id="patientRegNoEdit" name="patientRegNoEdit" class="form-control patientRegNoEdit" readonly/>

                                            </div>
                                        </div>

                                        <div class="form-group">
                                            <label id="appointmentDateEdit" class="control-label col-md-2">Date:</label>
                                            <div class="col-md-10">
                                                <div class='input-group date' id='datetimepickerEdit'>
                                                    <input type='text' class="form-control datetimepickerEdit" id="appointmentDateEdit" name="appointmentDateEdit" value="" />
                                                    <span class="input-group-addon datetimepicker-addonEdit">
                                                        <span class="glyphicon glyphicon-calendar"></span>
                                                    </span>
                                                </div>
                                            </div>
                                        </div>

                                        <div class="form-group">
                                            <label id="lblReasonsEdit" class="control-label col-md-2">Reason(s):</label>
                                            <div class="col-md-10">
                                                <textarea rows="4" cols="50" id="reasonsEdit" name="reasonsEdit" class="form-control reasonsEdit"></textarea>
                                            </div>
                                        </div>

                                    </div>

                                </div>
                            }
                            <div class="modal-footer">
                                <button type="button" class="btn btn-default pull-left" data-dismiss="modal">Close</button>
                                <button type="submit" id="UpdateButton" name="UpdateButton" class="btn btn-primary update">Update</button>
                            </div>
                        </div>
                        <!-- /.modal-content -->
                    </div>
                    <!-- /.modal-dialog -->
                </div>
Posted
Comments
F-ES Sitecore 16-Nov-18 11:58am    
"Not working" doesn't give anyone enough information to help you. Would you phone a mechanic and say "My car isn't working, how do I fix it?"

Use the debugger to step through your code to work out how far it is getting, what it is doing you don't want it to, or not doing that you do. Look at the network tab if your browser tools to see if the urls are being called or not, and look at what is being sent and returned. Those steps will give you a better idea about what is happening and what isn't.
Uwakpeter 16-Nov-18 13:17pm    
Thank you sir for your comment, I thought that was explicable enough.
I have a datatable edit button for each row, I want if I click on the edit button, a modal show with the corresponding row record for edit, the way it works now, the modal will display quite well if I click on the edit button, but I have not been able to populate the modal text fields with the corresponding row record for edit. Hope I have been able to explain well this time.
Richard Deeming 16-Nov-18 13:57pm    
Uwakpeter 16-Nov-18 15:49pm    
No, the one I solved was different, initially when the datatable is loaded, if I click on the edit button, the modal displays with the row record populated on the modal input fields, at this point, the insert and update modal works perfectly fine, this was the one I solve. I have another button for insert, and I am using ajax for the insert and edit, I wanted to able insert, update and display the new record in real time without having to refresh the page to see the new changes, I was able to achieve this, the issue I am having is that, after that first insert or update is done, I rebound the datatable using ajax so that the new inserted or updated record should reflect on the datatable immediately. It is after this first insert or update has been done, that if I clicked on the edit button that the modal will display with empty fields. I have tried to resolve this, but I couldn't get it to work.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900