简体   繁体   English

如何使用jQuery将HTML和CSS应用于表中的选定行?

[英]How to apply html and css to a selected row in table using jquery?

In my MVC 3 application, my problem is, I have a table with rows generated by the number of records returned to the view. 在我的MVC 3应用程序中,我的问题是,我有一个表,该表的行是由返回视图的记录数生成的。 These rows have a checkbox and an upload btn feature if the record does not contain a file. 如果记录不包含文件,则这些行具有一个复选框和一个上载btn功能。 The idea being, that if there is no file associated with the record "No Cert" is displayed under the Cert column. 想法是,如果没有与“ No Cert”记录关联的文件,则在Cert列下显示。 Only when the checkbox is checked will this cell display a "upload file" btn. 仅当选中此复选框时,此单元格才会显示“上传文件” btn。 Once this btn is clicked and window is opened to select a file File is selected, a pop-up message appears notifying the user that the file was successfully uploaded. 单击此btn并打开窗口以选择文件选中文件后,会出现一条弹出消息,通知用户文件已成功上传。 However the cell under Cert for that selected row will now be blank. 但是,该选定行的“证书”下的单元格现在为空白。 Only with a page refresh will the file name appear. 只有页面刷新才会显示文件名。

I did try to add a div with an associated class to display "Uploaded" and it worked for the first one. 我确实尝试添加一个具有关联类的div来显示“已上传”,并且它适用于第一个。 But when I repeat the process on another selected row , the "uploaded" div is applied to the first row again and the cell in the row I just selected is left blank. 但是,当我在另一个选定的行上重复该过程时,“上载的” div将再次应用于第一行,而刚选择的行中的单元格将保留为空白。 I believe this is because the code is using the td with the first ID "attBtn" used in the onComplete function below. 我相信这是因为代码使用的td具有以下onComplete函数中使用的第一个ID“ attBtn”。

                      onComplete: function (id, fileName, responseJson) {
                    var resultMessage = document.getElementById('resultMessage');
                    alert(responseJson.msg);
                    $('.qq-uploader').remove();
                    $('#attBtn').prepend('<div class="uploadedTag"><p>Uploaded</p></div>');

Section of the table dealing with the upload feature: 表中处理上传功能的部分:

      @if (Model.ElementAt(index).CertName != null)
             {
                 <td>@Html.DisplayFor(m => Model.ElementAt(index).CertName)</td>
             }
             else
             {                    
                <td id ="attBtn" class="file-uploader-attachment-Class"></td>
             }

Script used in view: 视图中使用的脚本:

   <script type="text/javascript">
$(document).ready(function () {

    function handleCheckbox() {
        if ($(this).find(':checkbox').is(':checked')) {
            createUploader($(this));

            $(this).find('.file-uploader-attachment-Class').removeClass("upload-placeholder-unchecked");
        }
        else {
            $(this).find('.file-uploader-attachment-Class').addClass("upload-placeholder-unchecked");
            $(this).find('.file-uploader-attachment-Class').html($('#myHTML2').html());
        }
    }

    $('tr').each(handleCheckbox);
    $('tr').on('click', handleCheckbox);


    function createUploader(container) {

        var elements = container.find('.file-uploader-attachment-Class');

        var CAL_ID = container.find(':checkbox').val()

        Array.prototype.filter.call(elements, function (element) {

            var uploader = new qq.FileUploader({
                element: element,
                sizeLimit: 2147483647, // max size
                action: '/CalibrationViewer/AttachmentUpload',
                allowedExtensions: ['xls', 'xlsx', 'pdf', 'doc', 'docx', 'csv', 'txt', 'rtf', 'zip', 'zipx', '7z'],
                params: {
                    customer: CUST_NAME,
                    calibrationId: CAL_ID
                },
                multiple: false,
                debug: false,

                onComplete: function (id, fileName, responseJson) {
                    var resultMessage = document.getElementById('resultMessage');
                    alert(responseJson.msg);
                    $('.qq-uploader').remove();
                    $('#attBtn').prepend('<div class="uploadedTag"><p>Uploaded</p></div>');

                }

            });

        });
    }
});

证书栏ScreenShot

How do identify the current row I'm working with. 如何确定我正在使用的当前行。 ? It would be great if I could get the name of the File name just inserted into the cell instead of the "uploadded" 如果能将文件名插入到单元格中而不是“上载”,那就太好了

I'm not going to try to figure out your specific element classes but simply point out a general approach that is a very common pattern for isolating instances in repeated blocks of html. 我不会尝试找出您的特定元素类,而只是指出一种通用方法,该方法是隔离html重复块中的实例的非常常见的模式。

Since you are using table , row will be a <tr> 由于您正在使用table,因此row将为<tr>

$('.someButtonClass').click(function(){

   /* "this" is the instance of the elements 
       represented by selector that event triggered on
    */
    var $row = $(this).closest('tr'); /* isolate row instance*/

    /* now look within row instance to manipulate descendant elements  */

    $row.find('.someOtherClass').doSomething();
});

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

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