首页 > 解决方案 > 如果 {{ value[#] }} 为空,则隐藏模态按钮

问题描述

我目前正在使用 Flask 开发与仓库库存相关的 Excel 报告的 Web 界面。我已经使用 DataTables 构建了一个表格,该表格从 csv 文件中提取数据并且它正在工作。我被要求在每行末尾添加一个额外的数据单元格,如果该库存项目的 csv 文件中存在注释,则该单元格有一个可单击的按钮来打开注释。我正在尝试使用 Font Awesome 图标让用户单击以打开显示注释的模式,但是我无法仅在有注释要查看时才显示该图标。

这是我尝试过的

<tbody>                     
    {% for value in stocklist %}
    <tr>
       <td value="{{ value[12] }}"><p class="muted hidden">{{ value[12] }}</p></td>                        
       <td>{{ value[0] }}</td>
       <td>{{ value[2] }}</td>
       <td class="over-25">{{ value[4] }}</td>
       <td>{{ value[6] }}</td>
       <td>{{ value[7] }}</td>
       <td>{{ value[8] }}</td>
       <td>{{ value[9] }}</td>
       <td>{{ value[10] }}</td>
       <td>{{ value[11] }}</td>
       <td id="notes"><button type="button" class="btn btn-warning hide_show" data-toggle="modal" data-target="#myModal"><i class="fas fa-exclamation"></i></button></td>
     </tr>                                       
     {% endfor %}

     {% for value in stocklist %}
        <div class="modal fade" id="myModal" role="dialog">
            <div class="modal-dialog">
                <!-- Modal content-->
                <div class="modal-content">
                    <div class="modal-header ml-2">                                    
                       <h4 class="modal-title">Notes</h4>
                    </div>
                    <div class="modal-body">
                       <p>{{ value[13] }}</p>                                    
                    </div>
                    <div class="modal-footer ml-2">
                       <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                    </div>
                 </div>
              </div>
           </div>
     {% endfor %}                       
</tbody>

这是我尝试过的 jQuery -

$("#notes").find('hide_show').filter(function() {
return !this.firstChild; 
}).hide();

我的尝试基于此答案->如果 div 为空,则隐藏它们

我遇到的另一个问题是,当模式打开时,不管怎样,它都会为每个库存项目显示相同的注释(库存项目编号 1 的注释)。

如何让图标仅在有注释时出现,然后在打开时让正确的注释出现在模式中?

任何帮助将不胜感激!谢谢

标签: jqueryif-statementflaskdatatablesjinja2

解决方案


您可以在提取的值中添加注释数据。我不知道你是如何提取数据的,但这是一个提示

{% if value.notes %}

<td id="notes"><button type="button" class="btn btn-warning hide_show" data-toggle="modal" data-target="#myModal"><i class="fas fa-exclamation"></i></button>
</td>

{% endif %}

推荐阅读