简体   繁体   English

使用Javascript切换语句在DIV中显示HTML表

[英]Display HTML table inside a DIV using Javascript switch statement

I am working on displaying a table that lists things based on what state was selected in HighMaps during drill down: 我正在显示一个表,该表根据在向下钻取过程中在HighMaps中选择的状态列出了一些东西:

Code Snippet: 代码段:

                switch (e.point.name) {
                    case 'California':
                        $('#table').html('');
                        break;
                    case 'Virginia':
                        $('#table').html('');
                        break;
                    default:
                        $('#table').html('');
                        break;
                }
<div id="table"></div>

The table is dynamically generated by taking data from DB for each state using PHP. 该表是通过使用PHP从数据库获取每种状态的数据来动态生成的。 Table structure looks like this: 表结构如下所示:

 <div class="container">
            <div class="row">
                <div class="col-md-12">
                    <div class="panel panel-primary">
                        <div class="panel-heading">
                            <h3 class="panel-title text-center">Company List - State</h3>
                        </div>
                        <table class="table table-hover" id="dev-table">
                            <thead>
                                <tr>
                                    <th>Company Name</th>
                                    <th>County</th>
                                    <th>Details</th>
                                </tr>
                            </thead>
                            <tbody>
                                <tr>
                                    <td>Company 1</td>
                                    <td>Test County</td>
                                    <td>Company details.</td>
                                </tr>
                                <tr>
                                    <td>Company 2</td>
                                    <td>Test County</td>
                                    <td>Company details.</td>
                                </tr>
                                    <td>Company 3</td>
                                    <td>Test County</td>
                                    <td>Company details.</td>
                                </tr>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>

The generated table output that does inside .html('') looks like this: 在.html('')内部执行的生成的表输出如下所示:

 $('#table').html('<div class="container"> <div class="row"><div class="col-md-12"><div class="panel panel-primary"><div class="panel-heading"><h3 class="panel-title text-center">Company List - California</h3></div><table class="table table-hover" id="dev-table"><thead><tr><th>Company Name</th><th>County</th><th>Details</th></tr></thead><tbody><tr><td>Company 1</td><td>Test County</td><td>Company details are available to ACMA members only.</td></tr><tr><td>Company 2</td><td>Test County</td><td>Company details are available to ACMA members only.</td></tr><td>Company 3</td><td>Test County</td><td>Company details.</td></tr></tbody></table></div></div></div></div>');

Everything works well when the list of companies is small. 当公司的名单很小时,一切都会很好。 Once The number of companies per state gets bigger and the generated table code becomes longer things stop working. 一旦每个州的公司数量变大,并且生成的表代码变长,则停止工作。 I tested it out. 我测试了一下。 If I take the part of generated code that is too long and delete half of it then everything starts working again. 如果我将生成的代码部分太长并删除其中一半,那么一切都会重新开始。

Is there a limit on how many characters can go inside .html(); .html()中可以输入多少个字符有限制? ? Is there a more efficient way to display these tables? 有没有更有效的方式显示这些表?

What about using the append function within some specific selector like the one you are using currently $("#table").append(); 在某些特定的选择器(例如您当前正在使用的选择器)中使用append函数$(“#table”)。append(); check the link mentioned below: 检查以下链接:

http://api.jquery.com/append/ http://api.jquery.com/append/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM