weixin_33695450 2013-06-30 20:40 采纳率: 0%
浏览 27

Ajax调用HTML表

I already have a function where I am getting data from database and fill it in HTML table on combo-box change event basically I am doing this using ajax.

But every time I am calling the database on combo-box change which is not efficient. So, now I decided I fill the data in the html table initially on page load and then filter out from the HTML table instead of every time going to the database and filter it.

So is there any way of filter out data from the html table on combo box change event? or it is possible to call ajax on html table?

Thanks.

  • 写回答

2条回答 默认 最新

  • weixin_33724046 2013-06-30 21:21
    关注

    You could put classes on each <tr> which match values on the combo-box items. When an option is selected, show any rows that have a class that matches a value of the selected options.

    <select id="options" multiple="multiple">
        <option value="red">Red</option>
        <option value="green">Green</option>
        <option value="blue">Blue</option>
        <option value="yellow">Yellow</option>
        <option value="black">Black</option>
    </select>
    
    <table>
        <tr class="red green">
            <td>red or green</td>
        </tr>
        <tr class="blue">
            <td>blue</td>
        </tr>
        <tr class="yellow black">
            <td>yellow or black</td>
        </tr>
        <tr class="blue yellow">
            <td>blue or yellow</td>
        </tr>
    </table>
    
    <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
    <script type="text/javascript">
        $("select#options").change(function () {
            var selections = [];
    
            $(this).children(':selected').each(function (index, option) {
                selections.push(option.value);
            });
    
            console.log(selections);
            $("table tr").each(function (index, row) {
                $(row).hide();
                for(var i = 0; i < selections.length; i++) {
                    if($(row).hasClass(selections[i])) {
                        $(row).show();
                        break;
                    }
                }
            });
        });
    </script>
    

    That was fun. jsfiddle

    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog