3

I am having a jquery data table in which there is a description field, I want to limit the words to 30 and add a show more link. when the user clicks on the show more the comment of that particular id will open in the bootstrap modal. Till now I am able to do this much, but the comment is not coming in the bootstrap modal.

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

        $(document).on("click", ".opencomment", function () {
            var mycomment = $(this).data('FeedbackID');
            $(".modal-body #commentdesc").val(mycomment);
        });

        $('#FeedbackDetails').DataTable({
            "processing": true,

            "ajax": {
                "url": "/ViewFeedback/LoadData",
                "type": "GET",
                "datatype": "json"
            },
            "lengthMenu": [
                [5, 10, 25, 50, 100, -1],
                [5, 10, 25, 50, 100, "All"]
            ],
            "autoWidth": true,
            "responsive": true,
            "lengthChange": true,
            "ordering": true,
             "fnRowCallback" : function(nRow, aData, iDisplayIndex){      
                      var oSettings = this.fnSettings();
                       $("td:first", nRow).html(oSettings._iDisplayStart+iDisplayIndex +1);
                       return nRow;
            },
            "columns": [
                { "data": null, "autoWidth": true },
                { "data": "FeedbackUserName", "name": "User Name", "autoWidth": true },
                { "data": "FeedBackUserEmailID", "name": "Email ID", "autoWidth": true },
                { "data": "FeedBackComment", "name": "Comment", "autoWidth": true },
                { "data": "Designation", "name": "Designation", "autoWidth": true },
                { "data": "Organization", "name": "Organization", "autoWidth": true },
                { "data": "ContactNo", "name": "Contact No", "autoWidth": true },
                { "data": "City", "name": "City", "autoWidth": true },
                {
                    "data": "FeedBackDate", "name": "Feedback Date", "autoWidth": true,
                    'render': function (jsonDate) {
                        var date = new Date(parseInt(jsonDate.substr(6)));
                        var month = date.getMonth();
                        return date.getDate() + "/" + month + "/" + date.getFullYear();
                    }
                },


            ],


            columnDefs: [{

                targets: 3,
                data:"FeedbackID",
                render: function (data, type, row, meta) {
                    if (type === 'display' && data.length > 40) {
                       return '<span title="' + data + '">' + data.substr(0, 38) + '...<a href="" data-id="'+data+'" data-toggle="modal" class="opencomment" data-target="#myModal">Show More</a>';

                    }
                    else {
                        return data;
                    }


                }

            }],

            "language": {
                "emptyTable": "No Events Found Related To This User"
            }
        });
    });
</script>

<div class="container" style="margin-top:10px">
    <table id="FeedbackDetails" class="ui celled table">
        <thead>
           <tr>
             <th>S.No</th>
             <th>User Name</th>
             <th>Email ID</th>
             <th>Comment</th>
             <th>Designation</th>
             <th>Organization</th>
             <th>Contact No</th>
             <th>City</th>
             <th>Feedback Date</th>
            </tr>
          </thead>
     </table>
  </div>
  <div id="myModal" class="modal fade" role="dialog">
      <div class="modal-dialog">
           <div class="modal-content">
                <div class="modal-header">
                     <h4 class="modal-title">Feedback Comment</h4>
                </div>
                <div class="modal-body">
                     <p id="commentdesc"></p>
                </div>
                <div class="modal-footer">
                     <button type="button" class="btn btn-danger" data-dismiss="modal">Close</button>
                </div>
            </div>
      </div>
  </div>


public ActionResult LoadData()
{           
    using (DHIFeedbackEntities2 Ms = new DHIFeedbackEntities2())
    {
         var feedbacklist = Ms.FeedBacks.ToList<FeedBack>();
         return Json(new { data = feedbacklist }, JsonRequestBehavior.AllowGet);
    }
}

1 Answers1

0

I cannot find the comment div in your modal with id #commentdesc.

So change your modal HTML to:

<div id="myModal" class="modal fade" role="dialog">
           <div class="modal-dialog">
               <div class="modal-content">
                   <div class="modal-header">
                       <h4 class="modal-title">Feedback Comment</h4>
                   </div>
                   <div class="modal-body">
                       <p id="commentdesc"></p>
                   </div>
                   <div class="modal-footer">
                       <button type="button" class="btn btn-danger" data-dismiss="modal">Close</button>
                   </div>
               </div>
           </div>
       </div>

Then show the modal when the link is clicked. Also to get your comment change to $(this).data('id'); and since you are using paragraph tag set its html and not val properties

    $(document).on("click", ".opencomment", function () {
        var mycomment = $(this).data('id');
        $(".modal-body #commentdesc").html(mycomment);
        $('#myModal').modal('show');
    });

Also if your jquery selector is by ID, there is no need to search it within a class: $(".modal-body #commentdesc").val(mycomment); change that to $("#commentdesc").val(mycomment);

JustLearning
  • 3,164
  • 3
  • 35
  • 52
  • sorry the

    this was actually there, i will just update the code

    – sunny kumar Feb 27 '19 at 09:52
  • Could you console log this line: var mycomment = $(this).data('id'); Do you get the correct comment information? Because if it is, and your modal is showing then the above should work – JustLearning Feb 27 '19 at 09:54
  • actually, 'id' is not a field in my table. In columns def, target is:3 but data : feedbackID, – sunny kumar Feb 27 '19 at 10:01
  • ya i have seen that, in column defs you set `` hence in the click handler you must check for that `data` attribute and not `$(this).data('FeedbackID');` because you did not set it – JustLearning Feb 27 '19 at 10:04
  • according to this(https://stackoverflow.com/questions/1496096/is-there-a-limit-to-the-length-of-html-attributes) there is no character limit on data attribute, so i dont this that is the issue. The screen goes black probably because the modal is not shown and just the overlay is shown so i think you need this line: `$('#myModal').modal('show');` – JustLearning Feb 27 '19 at 10:08
  • also what you can do is to inspect your page and check that the paragraph has the comment in the modal even though the screen goes black – JustLearning Feb 27 '19 at 10:09
  • yeah, you are right the screen goes black due to this $('#myModal').modal('show'); – sunny kumar Feb 27 '19 at 10:10
  • but still the main issues is not resolved, complete comment for that particular row is not coming in popup – sunny kumar Feb 27 '19 at 10:11
  • can you log this line in the console and what the data is: console.log(mycomment). So in the click handler after `var mycomment = $(this).data('id');` write `console.log(mycomment);` – JustLearning Feb 27 '19 at 10:13
  • If the data is shown correctly in the console, make sure to display it like this:`$("#commentdesc").html(mycomment);` – JustLearning Feb 27 '19 at 10:16
  • sorry for the trouble bro, but your above ans is right, it worked, it was my silly mistake in the code – sunny kumar Feb 27 '19 at 10:18
  • no worries debuggin is the way to go when you have above issues, just log your data to the console and make sure each step/ function has the correct data, then you can pin point the source of your issue – JustLearning Feb 27 '19 at 10:19