简体   繁体   English

我可以根据返回查看的行数为表格单元格生成ID吗?

[英]Can I generate IDs for table cells based on number of rows being returned to view?

Long story short. 长话短说。 I'm attempting to implement a file upload feature in my C# MVC 3 application. 我正在尝试在C#MVC 3应用程序中实现文件上传功能。 I have a fileuploader.js file which I beleive was taken was taken from http://github.com/valums/file-uploader (this is not my application originally). 我有一个fileuploader.js文件,我认为该文件来自http://github.com/valums/file-uploader (这最初不是我的应用程序)。 I'd like to make the file upload feature available to reach row on the table in my view, number of rows will be based on the number of entries received from the database. 我想使文件上传功能可用于到达视图中表的行,行数将基于从数据库接收的条目数。 The problem I'm encountering at present is that the Upload btn is only appearing on the first row. 我目前遇到的问题是Upload btn仅出现在第一行。 I'm certain this is because this feature begins with finding the div with an id of "file-uploader-attachment" I tried changing the javascript to document.getElementsByClass and giving the div a class instead of an ID but it was unsuccessful. 我确定这是因为此功能首先要查找ID为“ file-uploader-attachment”的div,我尝试将javascript更改为document.getElementsByClass并为div提供了一个类而不是ID,但未成功。 So I need to either generate and assign ID's on the fly or of another way to id elements in each row and allow the javascript to find them. 因此,我需要即时生成和分配ID或以其他方式将ID分配给每一行中的ID元素,并允许javascript查找它们。 I will put on more code as needed, but I don't want to fill the page with code. 我将根据需要添加更多代码,但是我不想用代码填充页面。 If someone can point me in the right the direction or knows of a similar solution. 如果有人可以向我指出正确的方向或知道类似的解决方案。 Again, I need a file upload btn/feature for each row in a table whose size is changeable. 同样,我需要为表的每一行更改其大小可变的文件上传btn / feature。

cell in table 表中的单元格

  <td id="file-uploader-attachment"></td>

start of the createUploader() function in associated javascript code 关联的javascript代码中createUploader()函数的开始

     var uploader = new qq.FileUploader({
             element: document.getElementById('file-uploader-attachment'),

script in view: 视图中的脚本:

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

    // requests attachments from server, clears existing attachments and
    // replaces with most up to date attachments
    function LoadAttachments() {
        $('.loading').show();
        $.post('/CalibrationViewer/GetAttachments', { calibrationId: CAL_ID }, function (data) {
            $('#attachment').empty();
            $('#attachment').append(data);
            $('.loading').hide();
        });
    }

    function handleCheckbox() {
        if ($(this).find(':checkbox').is(':checked')) {
            //$(this).find('#file-uploader-attachment').html($('#myHTML').html());
            createUploader();
            $(this).find('file-uploader-attachment-Class').removeClass("upload-placeholder-unchecked");
            $(".qq-upload-button").css("margin-top", "-6px");
            $(".qq-upload-button").css("margin-bottom", "-20px");

        }
        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() {

        $(".file-uploader-attachment-Class").each(function (element) {
            var uploader = new qq.FileUploader({
                element: element,
                sizeLimit: 2147483647, // max size
                action: '/Controller/Action',
                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);
                    $('#resultMessage').addClass('greenText');
                    resultMessage.innerHTML = responseJson.msg;

                    $('#attachment').prepend('<p><a class="attachment-file" href="' + responseJson.fileId + '">' + responseJson.fileName + '</a>');
                    $('#removeAttachment').prepend('<p><a class="attachment-delete" href="' + responseJson.fileId + '">X</a></p>');

                    $('#no-attachments').remove();
                }

            });

        });
    }
});

The reason is that you can have an id (file-uploader-attachment) only once in your html page (they should be unique). 原因是您的html页面中只能有一个ID(文件上传器附件)(它们应该是唯一的)。 If they are not unique the first one is taken (if this is by standard or common sense, I do not know). 如果它们不是唯一的,则采用第一个(如果是标准或常识,我不知道)。

What you want to achive is something like this: 您想要达到的目标是这样的:

<td class="file-uploader-attachment"></td>

and

 var elements = document.getElementsByClassName('file-uploader-attachment');
 Array.prototype.filter.call(elements, function(element){
 var uploader = new qq.FileUploader({
         element: element, ...
 });

or even simpler with jquery (if already in use): 甚至使用jquery更简单(如果已在使用):

$(".file-uploader-attachment).each(function (element) {
   var uploader = new qq.FileUploader({
         element: element, ...
});

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

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