最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How can I add a drop down list in a table? - Stack Overflow

programmeradmin1浏览0评论

Following situation:

I'm programming a web-app, which get data from a server. I want to put these data in an table with two columns. In the first column there should be a name and in the second column should be a drop down list to choose which detailed information I want to get about the name in the first column.

My code:

<html>
<head>
<script type="text/javascript" charset="utf-8" src="cordova.js"></script>
<script src="datajs-1.0.2.min.js"></script>

<script type="text/javascript">    
function readCustomerSuccessCallback(data, response) {

     var customerTable = document.getElementById("CustomerTable");
     for (var i = 0; i < data.results.length; i++) {
            var row = customerTable.insertRow(1);
            var cell1 = row.insertCell(0);
            var cell2 = row.insertCell(1);
            cell1 = "data.results[i].CUSTOMER_NAME";
            cell2.innerHTML = '<a href="javascript:void(0);" onclick="readProducts(' + data.results[i].STATION_ID + ')">' + data.results[i].CUSTOMER_NAME + '</a>';

        }
    }
</head>
<body> 

<table id="CustomerTable">
        <thead>
        <tr>
            <th>Customer</th>
            <th>Filter the Data</th>
        </tr>
        </thead>
        <tbody>
          <tr>
             <td>EXAMPLE</td>
             <td class="dropdown">
                 <form action="" name="FILTER">
                     <select name="filter_for" size="5">
                         <option value="Druck">Druck</option>
                         <option value="Zahl">Zahl</option>
                         <option value="Temperatur">Temperatur</option>
                         <option value="Drehzahl">Drehzahl</option>
                         <option value="andere">andere</option>
                     </select>
                 </form>
             </td>
          </tr>
        </tbody>
    </table>
</body>
</html>

My Problem is, that the function does not create the drop down list in the second column. I'm new with html and Javascript and searching in the web didn't help.

Following situation:

I'm programming a web-app, which get data from a server. I want to put these data in an table with two columns. In the first column there should be a name and in the second column should be a drop down list to choose which detailed information I want to get about the name in the first column.

My code:

<html>
<head>
<script type="text/javascript" charset="utf-8" src="cordova.js"></script>
<script src="datajs-1.0.2.min.js"></script>

<script type="text/javascript">    
function readCustomerSuccessCallback(data, response) {

     var customerTable = document.getElementById("CustomerTable");
     for (var i = 0; i < data.results.length; i++) {
            var row = customerTable.insertRow(1);
            var cell1 = row.insertCell(0);
            var cell2 = row.insertCell(1);
            cell1 = "data.results[i].CUSTOMER_NAME";
            cell2.innerHTML = '<a href="javascript:void(0);" onclick="readProducts(' + data.results[i].STATION_ID + ')">' + data.results[i].CUSTOMER_NAME + '</a>';

        }
    }
</head>
<body> 

<table id="CustomerTable">
        <thead>
        <tr>
            <th>Customer</th>
            <th>Filter the Data</th>
        </tr>
        </thead>
        <tbody>
          <tr>
             <td>EXAMPLE</td>
             <td class="dropdown">
                 <form action="" name="FILTER">
                     <select name="filter_for" size="5">
                         <option value="Druck">Druck</option>
                         <option value="Zahl">Zahl</option>
                         <option value="Temperatur">Temperatur</option>
                         <option value="Drehzahl">Drehzahl</option>
                         <option value="andere">andere</option>
                     </select>
                 </form>
             </td>
          </tr>
        </tbody>
    </table>
</body>
</html>

My Problem is, that the function does not create the drop down list in the second column. I'm new with html and Javascript and searching in the web didn't help.

Share Improve this question edited Jan 8, 2018 at 9:13 Yvonne Marggraf asked Oct 2, 2015 at 12:54 Yvonne MarggrafYvonne Marggraf 4082 gold badges6 silver badges20 bronze badges 6
  • What function? I don't see any function to create the drop down menu. You are also missing a closing tag </script> and have an unfinished body tag <body. – gre_gor Commented Oct 2, 2015 at 13:10
  • my code are more thann 400 lines long, because I have also many other functions. I'm sorry for miss these two. In my code they are correct – Yvonne Marggraf Commented Oct 2, 2015 at 13:16
  • I tried many things out. Has anybody an idea how I can fill the cell2 with the drop down menu? I think this is my main problem – Yvonne Marggraf Commented Oct 2, 2015 at 13:27
  • If you want help with a function that creates a dropdown into a table cell, you will need to include that function into the question. Or is that what readCustomerSuccessCallback is supposed to be doing? Because it just adds table rows, where the second column is a link that calls a function on click. – gre_gor Commented Oct 2, 2015 at 13:58
  • Such as you said. the table is created in the <body> and the funktion readCustomerSuccessCallback ad for each Customer one row. In each row should be in the first column the name, and ich the second a drop down list. Its quite difficult to let the function this do – Yvonne Marggraf Commented Oct 5, 2015 at 7:43
 |  Show 1 more ment

3 Answers 3

Reset to default 2

you can try this one:

<body 

<table id="CustomerTable">
        <thead>
        <tr>
            <th>Customer</th>
            <th>Filter the Data</th>
        </tr>
        </thead>
        <tbody>
          <tr>
             <td>EXAMPLE</td>
             <td class="dropdown">
                 <form action="" name="FILTER">
                     <select name="filter_for" >
                         <option value="Druck">Druck</option>
                         <option value="Zahl">Zahl</option>
                         <option value="Temperatur">Temperatur</option>
                         <option value="Drehzahl">Drehzahl</option>
                         <option value="andere">andere</option>
                     </select>
                 </form>
             </td>
          </tr>
        </tbody>
    </table>
</body>

DEMO FIDDLE

Just open the form before the table opens, and close the form after the tag.

Just like this:

<html>
<head>
<script type="text/javascript" charset="utf-8" src="cordova.js"></script>
<script src="datajs-1.0.2.min.js"></script>

<script type="text/javascript">    
function readCustomerSuccessCallback(data, response) {

     var customerTable = document.getElementById("CustomerTable");
     for (var i = 0; i < data.results.length; i++) {
            var row = customerTable.insertRow(1);
            var cell1 = row.insertCell(0);
            var cell2 = row.insertCell(1);
            cell1 = "data.results[i].CUSTOMER_NAME";
            cell2.innerHTML = '<a href="javascript:void(0);" onclick="readProducts(' + data.results[i].STATION_ID + ')">' + data.results[i].CUSTOMER_NAME + '</a>';

        }
    }
    </script>
</head>
<body>
<form action="" name="FILTER">
<table id="CustomerTable">
        <thead>
        <tr>
            <th>Customer</th>
            <th>Filter the Data</th>
        </tr>
        </thead>
        <tbody>
          <tr>
             <td>EXAMPLE</td>
             <td class="dropdown">

                     <select name="filter_for" size="5">
                         <option value="Druck">Druck</option>
                         <option value="Zahl">Zahl</option>
                         <option value="Temperatur">Temperatur</option>
                         <option value="Drehzahl">Drehzahl</option>
                         <option value="andere">andere</option>
                     </select>

             </td>
          </tr>
        </tbody>
    </table>
    </form>
</body>
</html>

You need to close your body element with a > so it looks like this: <body>. That's all you need.

Also, if you want it to be an actual dropdown (right now it's a selectbox), remove the size attribute from the select tag.

<body>

<table id="CustomerTable">
        <thead>
        <tr>
            <th>Customer</th>
            <th>Filter the Data</th>
        </tr>
        </thead>
        <tbody>
          <tr>
             <td>EXAMPLE</td>
             <td class="dropdown">
                 <form action="" name="FILTER">
                     <select name="filter_for" size="5">
                         <option value="Druck">Druck</option>
                         <option value="Zahl">Zahl</option>
                         <option value="Temperatur">Temperatur</option>
                         <option value="Drehzahl">Drehzahl</option>
                         <option value="andere">andere</option>
                     </select>
                 </form>
             </td>
          </tr>
        </tbody>
    </table>
</body>

发布评论

评论列表(0)

  1. 暂无评论
ok 不同模板 switch ($forum['model']) { /*case '0': include _include(APP_PATH . 'view/htm/read.htm'); break;*/ default: include _include(theme_load('read', $fid)); break; } } break; case '10': // 主题外链 / thread external link http_location(htmlspecialchars_decode(trim($thread['description']))); break; case '11': // 单页 / single page $attachlist = array(); $imagelist = array(); $thread['filelist'] = array(); $threadlist = NULL; $thread['files'] > 0 and list($attachlist, $imagelist, $thread['filelist']) = well_attach_find_by_tid($tid); $data = data_read_cache($tid); empty($data) and message(-1, lang('data_malformation')); $tidlist = $forum['threads'] ? page_find_by_fid($fid, $page, $pagesize) : NULL; if ($tidlist) { $tidarr = arrlist_values($tidlist, 'tid'); $threadlist = well_thread_find($tidarr, $pagesize); // 按之前tidlist排序 $threadlist = array2_sort_key($threadlist, $tidlist, 'tid'); } $allowpost = forum_access_user($fid, $gid, 'allowpost'); $allowupdate = forum_access_mod($fid, $gid, 'allowupdate'); $allowdelete = forum_access_mod($fid, $gid, 'allowdelete'); $access = array('allowpost' => $allowpost, 'allowupdate' => $allowupdate, 'allowdelete' => $allowdelete); $header['title'] = $thread['subject']; $header['mobile_link'] = $thread['url']; $header['keywords'] = $thread['keyword'] ? $thread['keyword'] : $thread['subject']; $header['description'] = $thread['description'] ? $thread['description'] : $thread['brief']; $_SESSION['fid'] = $fid; if ($ajax) { empty($conf['api_on']) and message(0, lang('closed')); $apilist['header'] = $header; $apilist['extra'] = $extra; $apilist['access'] = $access; $apilist['thread'] = well_thread_safe_info($thread); $apilist['thread_data'] = $data; $apilist['forum'] = $forum; $apilist['imagelist'] = $imagelist; $apilist['filelist'] = $thread['filelist']; $apilist['threadlist'] = $threadlist; message(0, $apilist); } else { include _include(theme_load('single_page', $fid)); } break; default: message(-1, lang('data_malformation')); break; } ?>