Displaying concealed fields using the Bootstrap table detail formatter

I am attempting to create a customized detail formatter for my table, but I am encountering several fields with underscores, such as:

_id: undefined
_class: undefined    
_data: [object Object]

This occurs when I click the plus button.

<head>
    <!-- Necessary meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
     <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
    <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.6.3/css/all.css" integrity="sha384-UHRtZLI+pbxtHCWp1t77Bi1L4ZtiqrqD80Kn4Z8NTSRyMA2Fd33n5dQ8lWUE00s/" crossorigin="anonymous">
    <link rel="stylesheet" href="https://unpkg.com/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="2b4944445f585f594a5b065f4a49474e6b1a051a1d051b">[email protected]</a>/dist/bootstrap-table.min.css">  
    <link rel="stylesheet" type="text/css" href="http://cdnjs.cloudflare.com/ajax/libs/bootstrap-table/1.16.0/extensions/filter-control/bootstrap-table-filter-control.css">
      </head>
      <body>
        <div class="container">
                    <table id="table"
                        data-toggle="table"
                        data-filter-control="true"
                        data-show-search-clear-button="true"
                        data-sortable="true"
                        classes="table-sm"
                        data-pagination="true"
                        data-show-columns="true"
                        data-show-columns-toggle-all="true"
                        class="table-responsive"
                        
                        data-toolbar="#toolbar"
                        data-search="true"
                        data-show-refresh="true"
                        data-show-toggle="true"
                        data-show-fullscreen="true"
                        data-show-columns="true"
                        data-show-columns-toggle-all="true"
                        data-detail-view="true"
                        data-show-export="true"
                        data-click-to-select="true"
                        data-detail-formatter="detailFormatter"
                        data-minimum-count-columns="2"
                        data-show-pagination-switch="true"
                        data-pagination="true"
                        data-id-field="id"
                        data-page-list="[5, 25, 50, 100, all]"
                        data-show-footer="true"
                        >
                        <thead>
                            <tr>
                                <!--special characters and spaces not allowed in data-field name-->
                                <th data-field="columnA" data-filter-control="input" data-sortable="true">column A</th>
                                <th data-field="columnB" data-filter-control="select" data-sortable="true">column B</th>
                                <th data-field="columnC" data-filter-control="input" data-sortable="true" data-visible="false">column C</th>
                            </tr>
                        </thead>
                        <tbody>
                          
                          <tr>
                              <!--special characters and spaces not allowed in data-field name-->
                              <td data-field="columnA">a</td>
                              <td data-field="columnB">2</td>
                              <td data-field="columnC"> f </td>
                          </tr>
                          <tr>
                            <!--special characters and spaces not allowed in data-field name-->
                            <td>f</td>
                            <td> 4</td>
                            <td>d</td>
                        </tr>
                        <tr>
                            <!--special characters and spaces not allowed in data-field name-->
                            <td>f</td>
                            <td>1</td>
                            <td>h</td>
                        </tr>
                        <tr>
                            <!--special characters and spaces not allowed in data-field name-->
                            <td>f</td>
                            <td>1</td>
                            <td>h</td>
                        </tr>
                        <tr>
                            <!--special characters and spaces not allowed in data-field name-->
                            <td>f</td>
                            <td>1</td>
                            <td>h</td>
                        </tr>
                        </tbody>
                    </table>
        </div>
        <!-- Optional JavaScript -->
        <!-- jQuery first, then Popper.js, then Bootstrap JS -->
        <script>
        function detailFormatter(index, row) {
            var html = []
            
            $.each(row, function (key, value) {
              html.push('<p><b>' + key + ':</b> ' + value + '</p>')
            })
            
            return html.join('')
          }
        </script>
        <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
        <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
        <script src="https://unpkg.com/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="53313c3c2720272132237e2732313f3613627d62657d63">[email protected]</a>/dist/bootstrap-table.min.js"></script>
        <script src="https://unpkg.com/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="e5878a8a919691978495c89184878980a5d4cbd4d3cbd5">[email protected]</a>/dist/extensions/filter-control/bootstrap-table-filter-control.min.js"></script>
    </body>
    </html>

The functionality of this specific formatter is not entirely clear to me. I desire increased control over individual fields rather than simply iterating through the entire object and displaying key-value pairs. Is there a way to achieve this?

Answer №1

To achieve the above, you can utilize the th data-field value to extract only the column name from the row and then use it to fetch the required value. Additionally, if you have set data-visible="false" for the tr element of column C, they will not be present in your DOM, making it impossible to retrieve their values using

$("th:eq(" + (indexs + 1) + ")").data("field")
. However, if you are aware of the data-field value for the hidden column, you can use
html.push('<p><b> yourcolumnfieldname :</b> ' + row['yourcolumnfieldname'] + '</p>')
.

Demo Code :

function detailFormatter(index, row) {
  var html = []
  var indexs = 0

  $.each(row, function(key, value) {
    //index +1 because first column is for `+`
    //and data-field is column a ,column b..etc
    var columns = $("th:eq(" + (indexs + 1) + ")").data("field")
    if (columns != undefined) {
      html.push('<p><b>' + columns + ':</b> ' + row[columns] + '</p>')
      indexs++;
    }
  })
  //here columnC matches with data-field value.
   html.push('<p><b> columnC :</b> ' + row['columnC'] + '</p>')
  return html;
}
<html>

<head>
  <!-- Required meta tags -->
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  <!-- Bootstrap CSS -->
  <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
  <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.6.3/css/all.css" integrity="sha384-UHRtZLI+pbxtHCWp1t77Bi1L4ZtiqrqD80Kn4Z8NTSRyMA2Fd33n5dQ8lWUE00s/" crossorigin="anonymous">
  <link rel="stylesheet" href="https://unpkg.com/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="1d7f7272696e696f7c6d30697c7f71785d2c332c2b332d">[email protected]</a>/dist/bootstrap-table.min.css">
  <link rel="stylesheet" type="text/css" href="http://cdnjs.cloudflare.com/ajax/libs/bootstrap-table/1.16.0/extensions/filter-control/bootstrap-table-filter-control.css">
</head>

<body>
  <div class="container">
    <table id="table" data-toggle="table" data-filter-control="true" data-show-search-clear-button="true" data-sortable="true" classes="table-sm" data-pagination="true" data-show-columns="true" data-show-columns-toggle-all="true" class="table-responsive"
      data-toolbar="#toolbar" data-search="true" data-show-refresh="true" data-show-toggle="true" data-show-fullscreen="true" data-show-columns="true" data-show-columns-toggle-all="true" data-detail-view="true" data-show-export="true" data-click-to-select="true"
      data-detail-formatter="detailFormatter" data-minimum-count-columns="2" data-show-pagination-switch="true" data-pagination="true" data-id-field="id" data-page-list="[5, 25, 50, 100, all]" data-show-footer="true">
      <thead>
        <tr>
          <!--special characters and spaces not allowed in data-field name-->
          <th data-field="columnA" data-filter-control="input" data-sortable="true">column A</th>
          <th data-field="columnB" data-filter-control="select" data-sortable="true">column B</th>
          <th data-field="columnC" data-filter-control="input" data-sortable="true" data-visible="false">column C</th>
        </tr>
      </thead>
      <tbody>

        <tr>
          <td data-field="columnA">a</td>
          <td data-field="columnB">2</td>
          <td data-field="columnC"> f </td>
        </tr>
        <tr>

          <td>f</td>
          <td>4</td>
          <td>d</td>
        </tr>
        <tr>
          <td>f</td>
          <td>1</td>
          <td>h</td>
        </tr>

      </tbody>
    </table>
  </div>
  <!-- Optional JavaScript -->
  <!-- jQuery first, then Popper.js, then Bootstrap JS -->
  <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
  <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
  <script src="https://unpkg.com/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="f7959898838483859687da8396959b92b7c6d9c6c1d9c7">[email protected]</a>/dist/bootstrap-table.min.js"></script>
  <script src="https://unpkg.com/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="dfbdb0b0abacabadbeaff2abbebdb3ba9feef1eee9f1ef">[email protected]</a>/dist/extensions/filter-control/bootstrap-table-filter-control.min.js"></script>
</body>

</html>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Regular expressions - For alphanumeric or numeric with a possible slash character included

I need assistance with extracting alphanumeric values from a string array using RegEx. For instance: Apartment 101/B First Villa 3324/A Second Milk 12MG/ML Third Sodium 0.00205MG/ML Fourth Water 0.00205MG Fifth Eggs 100 Sixth My goal is to retrieve the v ...

Determine the elapsed time in seconds between two specified moments

I am trying to implement two input fields in my HTML, one for a starting point and another for an end point. The user will enter two times like this: For example: [8:15] - [14:30] alert("XXXXX seconds") I want to calculate the number of seconds between 8 ...

What is the best way for my web application to interface with a serial port?

I am working on a cloud-based web application that uses ASP Web API and Angular, both hosted on Azure. I have a requirement for my Angular app to communicate with a serial port for reading and writing data. How can I achieve this functionality? I've ...

Trigger PHP function on mouse click in WordPress

On one of my PHP files for a WordPress site, I currently have this code: <div class="tabs"> <a href="#tab1" id="items_id">Items</a> </div> <div class="section" id="tab1"> <?php get_template_part('page/tab1' ...

Issue with scrolling feature in div

I am currently facing an issue with scrolling on my website. Visit this site to see the problem. When scrolling down, it causes the "hidden" part of the site to slide up. I want to achieve a similar sliding effect, but it needs to be smooth. I have attempt ...

What's the best way to manage the onClick event for individual input submit types using jQuery?

As a novice in jQuery, I am still in the learning phase. However, I urgently require a solution to set up a click event registration for each button within my table. The table below is created using GridView: <div id="gridView"> &l ...

Creating a form that utilizes both jQuery and PHP to send the results, now including the complete code for reference

Full code update included. Changing the question: What occurs when all questions are answered in endQuiz, resulting in a user score? A form is displayed for the user to complete and submit to a designated email address. However, upon completing the form a ...

What sets $(document).on apart from ($ document).on in CoffeeScript?

One of my buddies is incorporating ($ document).on into his CoffeeScript script. I'm curious to know if this differs from the typical $(document).on and, if it does, how so? ...

What is the best way to utilize a single Google Map component instance across multiple children?

Seeking a method to maintain the same Google Map instance throughout my entire app, as each map load incurs charges... Currently utilizing google-map-react. An instance of a new Map is created in ComponentDidMount, suggesting that it's important to k ...

What is the best way to reset the 3rd dynamic dropdown menu once a new selection is made in the 1st dropdown menu?

I have been working on creating a dynamic dropdown using Jquery ajax and json in js. Almost everything is working as expected, except for one issue that I am facing. The problem arises when I select an option in dropdown A, which then loads items into drop ...

Combining arrays to append to an array already in place

I have implemented the rss2json service to fetch an rss feed without pagination support. Instead of a page parameter, I can utilize the count parameter in my request. With this setup, I am successfully able to retrieve and display the feed using a service ...

To send an array of input data to JQuery

I have a requirement where students need to answer questions (retrieved from a database) using a foreach loop. Once all answers are filled, I need to pass them to jQuery and then to the controller to save in the database. <div> <tbody> ...

How can an HTML5 application be configured to enable access to intranet and local files?

It's a known fact that accessing the local path of a file added using a file input field or drag-and-drop is not possible even with the new FileAPI. Whether this limitation is good, bad, or ugly is not up for debate. The FileAPI specs clearly state th ...

The requested resource at http://localhost/Grafica/%7Bd.icon%7D/ was not found (Error 404)

Creating a tooltip in a weather chart, I want to display an image of the sky condition. Below is the HTML code: <div id="tooltip"> <table class="table table-condensed"> <tr><th>Time (local)</th><th data-text="d ...

Error: npx is unable to locate the module named 'webpack'

I'm currently experimenting with a customized Webpack setup. I recently came across the suggestion to use npx webpack instead of node ./node_modules/webpack/bin/webpack.js. However, I am encountering some difficulties getting it to function as expecte ...

Error occurs when attempting to clear the cache storage

useEffect(async () => { caches.open('my-cache') }, [token, user_id]) Upon trying to log out of the application, const logoutFunc = () => { localStorage.clear() caches.keys().then((list) => list.map((key) => { ...

Combine two or more Firebase Observables

Currently, I am working on creating an Observable using FirebaseObjectObservable. However, before I can accomplish this, I need to query a Firebase list to obtain the key IDs required for the FirebaseObjectObservable. The structure of my data is as follow ...

Vue form component triggers the submit event twice

In my current project, I am utilizing the most recent version of Vue 3 without any additional vendors. After experiencing a setback in which I invested several hours attempting to uncover why my form component was triggering the submit event twice, I am le ...

Generate a collection of LatLng coordinates to represent a worldwide grid

I'm in search of a quick and easy solution, like a simple library or similar tool, that would allow me to call a function like createGlobalGrid(1000) and automatically generate a list of points on a geospatial surface, ensuring that each point is no m ...

Markers on Google Maps are failing to display when the locations are retrieved from a database

I am currently incorporating Google Maps API into my website using Node.js and mongodb. My Node.js express app fetches locations from mongodb, and I have successfully set up the map on my website. However, I am encountering an issue where only the hardcode ...