You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将DataTable行数据展示在模态框的非表格区域?

How to Display Extra Row Data in Non-DataTable Areas of Your Modal

Hey there! Nice work getting the row click modal and DataTable functionality up and running. Let's walk through how to show those extra fields (like item_name) in the non-DataTable parts of your modal—it's simpler than you might think!

Step 1: Add Placeholders in Your Modal HTML

First, you'll need to set up elements in your modal's markup where you want the extra data to appear. Give each element a unique ID so you can target it with JavaScript. For example:

<!-- Example Modal Structure -->
<div class="modal fade" id="modal-content" tabindex="-1">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title">Item Details</h5>
        <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
      </div>
      <div class="modal-body">
        <!-- Non-DataTable Area: Extra Field Display -->
        <div class="mb-3">
          <strong>Item Name:</strong>
          <span id="display-item-name"></span>
        </div>
        <div class="mb-3">
          <strong>Any Other Field:</strong>
          <span id="display-other-field"></span>
        </div>

        <!-- Your Existing DataTable -->
        <table id="your-item-datatable" class="table table-striped">
          <!-- DataTable Columns Go Here -->
        </table>
      </div>
    </div>
  </div>
</div>

Step 2: Update Your Click Event to Populate the Placeholders

Now, in your existing click handler, after you grab the row data, you can use jQuery's .text() (or .val() for inputs, .html() for formatted content) to inject the extra field values into your modal elements.

Modify your code like this:

$('#stock-enquiry-table tbody').on('click', 'tr', function () { 
  var data = table.row(this).data(); 
  console.log(data); 

  // Populate non-DataTable fields with row data
  $('#display-item-name').text(data.item_name); // Show item_name
  $('#display-other-field').text(data.your_other_field || 'N/A'); // Fallback if field is empty

  // Your existing code
  $('#selected-item-id').val(data.item_id); 
  table_item.ajax.reload(); 
  $('#modal-content').modal({ show: true }); 
});

Quick Tips

  • If your field contains HTML content (like formatted text), use .html() instead of .text()—just be cautious of XSS risks if the data comes from user input.
  • Add fallback text (like 'N/A') using the || operator to handle cases where the field might be null or empty.
  • If you're using input fields instead of static text, use .val(data.your_field) instead of .text().

That's it! Now when you click a row, your modal will display both the DataTable content and any extra fields you've set up in the non-DataTable area.

内容的提问来源于stack exchange,提问作者Brantley Ong

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.13 07:18:52