【问题标题】:Linking a file upload to a button in a table, using an id passed from the database使用从数据库传递的 id 将文件上传链接到表中的按钮
【发布时间】:2019-04-09 19:30:02
【问题描述】:

使用一些遗留代码,并尝试添加一些功能。该页面用于处置上传。页面底部是与处置相关的文件表。这些文件(附件)保存在数据库的表中,每个文件都有自己的 ID。

客户希望在表格中添加一个“替换”按钮,在每个条目旁边的每一行上(已经有一个下载和删除按钮)。单击后,将显示文件上传表单。应该发生的是客户端上传的文件应该用 ID 替换表中的附件。但是,当我单击“替换”按钮时,它会在表格顶部显示替换附件的表单。

如何按 ID(通过数据库表)将按钮链接到表单?

这是桌子... '''

<table class="table table-striped table-bordered table-hover table-hover table-full-width">
    <thead>
        <tr>
            <th class="center hidden-xs"></th>
            <th style="display:none;">ID</th>
            <th>File Name</th>
            <th>Figure Name</th>
            <th>Date Uploaded</th>
            <th>Rearrange Order</th>
        </tr>
        </thead>
    <tbody>
    <cfset loopCount = 1 />
    <cfset ids = '' />
    <cfset allowDown = #qAttachments.recordCount# />
    <cfloop query = "qAttachments">
    <cfset ID = "#qAttachments.id#">
    <cfset fileName="#qAttachments.filename#">
    <cfset fileExt=ListLast(fileName,".")>
    <cfset filePath = "/secure/edFiles/edAttachments/ED_#session.module.id#/#url.edID#/#fileName#"><!---removed.pdf--->
        <tr>
            <div id="replaceAtt" style="display: none" >
                <div class="col-md-6">
                  <div class="fileupload fileupload-new" data-provides="fileupload">
                            <div class="input-group">
                                <div class="form-control uneditable-input">
                                    <i class="fa fa-file fileupload-exists"></i>
                                    <span class="fileupload-preview"></span>
                                </div>
                                <div class="input-group-btn">
                                    <div class="btn btn-blue btn-file">
                                        <span class="fileupload-new"><i class="fa fa-folder-open-o"></i> Select file</span>
                                        <span class="fileupload-exists"><i class="fa fa-folder-open-o"></i> Change</span>
                                        <input type="file" id="replaceEDFile" name="replaceEDFile" title="Select File to Replace #ID#">
                                    </div>
                                    <a href="" class="btn btn-blue fileupload-exists" data-dismiss="fileupload">
                                        <i class="fa fa-times"></i> Remove
                                    </a>
                                </div>
                            </div>
                        </div>
                </div>

                <div class="col-md-2">
                    <div class = "btn btn-blue btn-block" value="#ID#" type = "submit" name ="replaceFile"  onClick="location.href='edFormData.cfm?replaceFile=#ID#&m=#url.m#&edID=#url.edID#&#r#&ai=#url.ai#'">
                          Upload File <i class = "fa fa-arrow-circle-right" ></i>                                
                    </div>
                </div>
            </div>
        </tr>
        <tr>

            <td class="center hidden-xs">
                <a href="#filePath#"><button type = "button" class="fa" name="download" id="download" value="#ID#" onClick="location.href='edFormData.cfm?download=#ID#&m=#url.m#&edID=#url.edID##r#&ai=#url.ai#'">           <img src="../assets/Icons/viewdoc.png"></button></a>   

                <cfif readonly NEQ "readonly">

                <button type = "button"  class="fa" name="Delete" id="Delete" value ="#ID#" onClick="location.href='edFormData.cfm?del=#ID#&m=#url.m#&edID=#url.edID##r#&ai=#url.ai#'">
                <img src="../assets/Icons/trash-o_ff0400_20.png"></button>

                <button id="replace" type = "button" class ="replace" name="replace" value="#ID#" title="Replace attachment #ID#" >
                 <img src="../assets/Icons/file_replace_000000.png">
                </button>
                </cfif>
            </td>
            <td style="display:none;">#ID#</td>
            <td id="file_#ID#">#qAttachments.filename#</td>
            <td id="figure_#ID#">#qAttachments.figureName#</td>
            <td id="uploaded_#ID#">#qAttachments.uploaded#</td>
            <td>
                <cfif loopCount NEQ 1>
                    <div class = "btn btn-green btn-block" id="moveUP_#ID#">Move Up</div><br />
                </cfif>
                <cfif loopCount NEQ allowDown>
                    <div class = "btn btn-blue btn-block" id="moveDown_#ID#">Move Down</div>
                </cfif>
            </td>
        </tr>

        <cfif loopCount NEQ allowDown>
            <cfset ids = #ids#&"#ID#," />
        <cfelse>
            <cfset ids = #ids#&"#ID#" />
        </cfif>
        <cfset loopCount=(#loopCount#+1) />
    </cfloop>
    <input type="hidden" id="possibleIDs" value="#ids#" />
</tbody>
</table> 

'''

这里是 JavaScript.... '''

<script>
    $(document).ready(function(){
        $('.replace').click(function(e){        
            e.preventDefault();
            $("#replaceAtt").slideToggle('fast');
        });
});

</script>

'''

【问题讨论】:

  • 1.您将表格、div 和 Bootstrap 混合在一起。我真的把它翻了一番。 2. loopCount=(#loopCount#+1) 是一大堆代码来做 loopcount++
  • 这实际上是我开始访问此页面之前的代码。这是用于在桌子上上下移动附件的位置变换器。这很好用,不会影响我添加的内容。
  • 解决这个问题的方法有很多...您可以在替换按钮中添加一个 data-id 属性,然后使用 jquery .data() 方法或 .attr() 方法检索该属性.这会给你你的 id,然后你可以在你的表单中设置一个隐藏字段。只有一个想法。
  • @Redtopia,谢谢你的建议。我使用 JavaScript 的时间不长,并且仍在学习它的来龙去脉。我明天试试这个。
  • 当您说,旧版时,您使用的是哪些版本?

标签: javascript sql coldfusion coldfusion-11 jasny-bootstrap


【解决方案1】:

这是更多的代码审查/评论,而不是完整的答案。在这段代码中发生了很多事情。正如詹姆斯所说,您的代码混合了表格和 div 并抛出了一些引导程序。有很多变量在其中浮动,但不清楚它们在哪个范围内(例如readonly)和很多实际上并不需要的变量(例如loopcountallowDown)。还有一些变量不需要引号和磅(如&lt;cfset ID = "#qAttachments.filename#"&gt;,可以只是&lt;cfset ID = qAttachments.ID&gt;),还有一些地方直接在代码中使用了URL 变量。还有其他几件事。

这是遗留代码,所以我完全理解。它就是这样,如果它是 CF11 代码,自它首次编写以来可能已经改进了很多东西。在这里很容易成为周一早上的四分卫。

也就是说,您可以在tbody 标记之间显着减少您在此页面中所做的事情。

为了简化一点,我省略了你循环的大部分 HTML。

由于您使用的是查询循环,因此您不需要跟踪循环计数,因为它已经是currentRow 中查询结果的一部分。而且您不需要设置allowDown,因为您只引用它一次。您真正需要的唯一事情是初始化ids,以便您可以ListAppend(),而不是试图弄清楚如何处理尾随逗号。

<cfset ids="">

<cfoutput>

<cfloop query="qAttachments">
    <!--- HTML Display Code In This Block --->
    EXAMPLE: WE ARE ON ID = #id# 
    <!--- Move Buttons --->        
    <cfif currentRow NEQ 1> MOVE UP </cfif>
    <cfif currentRow NEQ qAttachments.recordcount> MOVE DOWN </cfif>
    <br>
    <!--- --->
    <!--- Build ID list for hidden form. --->
    <cfset ids = ListAppend(ids,id)>
</cfloop>

<br>
< hiddenFormInput > possibleIDs = "#ids#" < /hiddenFormInput >

</cfoutput>

https://cffiddle.org/app/file?filepath=870efd11-b974-4905-8d47-9afb41fa2a10/e47a5d00-1a86-4cd9-8996-f256ad72dff5/49648087-5075-48c1-9a96-23d20b6e2d82.cfm

同样,这只是用于更好地构建循环的概念代码。在这方面,由于这是 CF11,因此使用 cfscript 而不是 cftags 循环会好得多。而且我建议将您的 CFML 代码与显示代码分开,也许使用 CF 函数可以返回一组数据以循环显示您的显示。

我要评论的最后一件事是使用ID 作为通用名称。在您的循环中,您使用了 3 个不同的 ID 变量:1) session.module.id 2) ID 来自您的查询 3) 和一个 ID 变量,基于循环的查询 ID。在这种特定情况下,您将获得想要的值,但在一个页面上拥有多个具有相同名称的变量通常不是一个好主意,而ID 很容易做到。只需要改变评估的顺序,它会让你头疼,很难调试。

【讨论】:

  • 另外,如果隐藏列表包含查询中的每个 ID 值,则无需手动构建列表。只需在循环外使用 valueList() 即可。
  • 感谢您的建议。在我发布的代码之外引用了向上和向下移动按钮。我相信,写这篇文章的人正在围绕这个文件的现有混乱进行编码。 ID问题也是一个奇怪的cookie。这不是站点范围内唯一使用 session.module.id 的地方,查询中的 ID 和 ID 变量应该是相同的值。在新页面上前进,我将使用这些笔记来更好地构建循环。谢谢。
  • @Clark id 特别引起了我的注意,因为我曾经在一个使用id 作为每个数据库表的键的系统上工作,没有正确使用外键(阅读 全部)并在整个代码中引用“id”。我不记得它是哪个软件版本更改,但它颠倒了id 的评估顺序。我们正在处理多个表,每个表的数量以百万计,并开始获取非常旧的记录。我们花了很长时间才弄清楚那些是因为,当我们认为我们在看t2.id时,我们真的在看t1.id。这有点让人抓狂。 :-/
【解决方案2】:

我要感谢所有对此发表评论和提供帮助的人。我知道代码充其量是 janky,但是当我开始弄乱循环中已经存在的内容时,其他地方的东西就会中断。但我确实发现了问题(在比我聪明得多的同事的帮助下)为什么我无法将按钮链接到 div 并为表格的该行显示正确的上传表单!这是图像……是的,图像。我决定让 JS 在按下按钮时发出 ID 名称的警报。经过一些黑客攻击后,我有时让它工作,而其他时候只是输出 NaN。好吧,通过一些“检查元素”魔术,发现它根据我单击按钮的位置输出 ID 或 NaN。这让我们意识到图像本身需要一个 id,因为它试图在点击时被拉取。

所以,这就是我对上面发布的代码所做的更改:

首先,我将保存上传表单的 div 拉出循环,因此我没有填充多个表单,并且我可以切换附件替换,而无需重新加载在屏幕上有一堆移动内容的页面。

第二,实际的按钮本身:

<button type = "button" class ="replace" name="replace" id="replace_#ID#" value="#ID#" title="Replace #qAttachments.figureName#" >
    <!--- Gave the image an ID, so if it is clicked it can still split properly. --->   
    <img id="replaceImg_#ID#" src="../assets/Icons/file_replace_000000.png">
</button>

第三,JavaScript。

<script>

    $('[id^="replace_"], [id^="replaceImg_"]').click(function(e){

        // Split the ID to get the number (lines up with DB ID).
        var replaceID = e.target.id.split("_")[1];

        // Set this to let the user know which file is being replaced (by figure name).
        var figNameRep = $('#figure_'+replaceID).text();

        // If the upload replacement is not showing yet, make it show now.
        if ($('#replaceAtt').css("display") == 'none'){
            $('#replaceAtt').slideToggle("fast");
        }

        // Don't worry about hiding and unhiding, just switch the IDs around as needed.
        $('#figNameReplace').text("Replacing file for Figure Name: " + figNameRep);
        $('#replaceFile').val(replaceID);
    });

    </script>

再次感谢大家的意见。你们都建议我尝试过的想法,虽然我无法让它们中的任何一个发挥作用(这更像是对我自己的技能的评论),但它确实教会了我一些新的工具和方法。你们真棒!

【讨论】:

    猜你喜欢
    • 2021-02-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多