简体   繁体   English

显示所有选中的表行

[英]Display All Table Rows that are Checked

I have a multiple column table with one column being checkboxes. 我有一个多列表,其中一列为复选框。 If you check a checkbox then press the "Checkout" button, it will take the specified rows and display them in the body of an email. 如果您选中一个复选框,然后按“签出”按钮,它将接受指定的行并将其显示在电子邮件正文中。

I originally bring in the top 100 rows to keep the info light for the user. 我最初进入前100行,以使用户保持信息畅通。 I also have a search functionality where the user can search for specific rows. 我还具有搜索功能,用户可以在其中搜索特定的行。 While you can maneuver throughout different searches and still keep all of the checkboxes checked with session storage, when you hit "Checkout" it only displays the table rows that are checked and currently visible on the page . 虽然您可以在不同的搜索中进行操作,并且仍然使所有复选框与会话存储保持选中状态,但是当您单击“ Checkout”时, 它仅显示已选中且当前在页面上可见的表行 So, if I searched for a table row and checked it, but then went back to the original top 100 rows by clicking home, then that row would not display on the email. 因此,如果我搜索了一个表格行并进行了检查,然后单击“首页”返回到原来的前100行,则该行将不会显示在电子邮件中。

How can I fix this and show ALL rows that have been checked, whether they are visible on the page or not? 如何解决此问题并显示所有已检查的行,无论它们在页面上是否可见?

HTML: HTML:

<section id="checkout-btn"> 
<button id="checkout" name="order" onclick="sendMail(); return false">Checkout</button>
</section>

<br>

<table id="merchTable" cellspacing="5" class="sortable">
    <thead>
        <tr class="ui-widget-header">
            <th class="sorttable_nosort"></th>
            <th class="sorttable_nosort">Loc</th>
            <th class="merchRow">Report Code</th>
            <th class="merchRow">SKU</th>
            <th class="merchRow">Special ID</th>
            <th class="merchRow">Description</th>
            <th class="merchRow">Quantity</th>
            <th class="sorttable_nosort">Unit</th>
            <th style="display: none;" class="num">Quantity #</th>
        </tr>
    </thead>
    <tbody>

        <?php foreach ($dbh->query($query) as $row) {?>

        <tr>
            <td class="ui-widget-content"><input type="checkbox" class="check" name="check" id="checkid-<?php echo intval ($row['ID'])?>"></td>
            <td class="loc ui-widget-content" data-loc="<?php echo $row['Loc'] ?>"><input type="hidden"><?php echo $row['Loc'];?></td>
            <td class="rp-code ui-widget-content" align="center" data-rp-code="<?php echo $row['Rp-Code'] ?>" id="rp-code-<?php echo intval ($row['Rp-Code'])?>"><?php echo $row['Rp-Code'];?></td>
            <td class="sku ui-widget-content" data-sku="<?php echo $row['SKU'] ?>" id="sku-<?php echo intval ($row['SKU'])?>"><?php echo $row['SKU'];?></td>
            <td class="special-id ui-widget-content" data-special-id="<?php echo $row['Special-ID'] ?>" align="center" id="special-id-<?php echo intval ($row['Special-ID'])?>"><?php echo $row['Special-ID'];?></td>
            <td class="description ui-widget-content" data-description="<?php echo htmlspecialchars($row['Description']) ?>" id="description-<?php echo intval ($row['Description'])?>"><?php echo $row['Description'];?></td>
            <td class="quantity ui-widget-content" data-quantity="<?php echo $row['Quantity'] ?>" align="center" id="quantity-<?php echo intval ($row['Quantity'])?>"><?php echo $row['Quantity'];?></td>
            <td class="unit ui-widget-content" data-unit="<?php echo $row['Unit'] ?>" id="unit-<?php echo intval ($row['Unit'])?>"><?php echo $row['Unit'];?></td>
            <td style="display: none;" class="quantity_num ui-widget-content"><input type="textbox" style="width: 100px;" class="spinner" id="spin-<?php echo intval ($row['ID'])?>"></td>
        </tr>

    <?php } ?>

    </tbody>
</table>

JavaScript for keeping checked checkboxes, checked, throughout the session: 用于在整个会话过程中保持选中状态的复选框的JavaScript:

$(function(){
    $(':checkbox').each(function() {
        var $el = $(this);
        $el.prop('checked', sessionStorage[$el.prop('id')] === 'true');
    });

    $('input:checkbox').on('change', function() { 
        var $el = $(this);
        sessionStorage[$el.prop('id')] = $el.is(':checked');
    });
});

JavaScript that brings in data from table to email: 从表到电子邮件引入数据的JavaScript:

function sendMail() {
    var link = "mailto:me@example.com"
             + "?subject=" + encodeURIComponent("Order")
             + "&body=";

    var body = '';

  $('table tr input[name="check"]:checked').each(function(){
    var current_tr = $(this).parent().parent();

    var data = current_tr.find('.loc').data('loc');
    body +=  encodeURIComponent(data) + '\xa0\xa0';

    var data =current_tr.find('.rp-code').data('rp-code');
    body +=  encodeURIComponent(data) + '\xa0\xa0';

    var data =current_tr.find('.sku').data('sku');
    body +=  encodeURIComponent(data) + '\xa0\xa0';

    var data =current_tr.find('.special-id').data('special-id');
    body +=  encodeURIComponent(data) + '\xa0\xa0';

    var data =current_tr.find('.description').data('description');
    body +=  encodeURIComponent(data) + '\xa0\xa0';

    var data =current_tr.find('.quantity').data('quantity');
    body +=  encodeURIComponent(data) + '\xa0\xa0';

    var data =current_tr.find('.unit').data('unit');
    body +=  encodeURIComponent(data) + '\xa0\xa0';

    var data =current_tr.find('.spinner').data('spinner');
    body +=  encodeURIComponent(data) + '\xa0\xa0';

    body += '%0D%0A';

  });

  body += '';
  link += body;
  console.log(link);

  window.location.href = link;
}

Revised based your comments/new code: 根据您的评论/新代码进行了修订:

Even with your new pasted code, the issue still remains that you can only output whats currently available on screen due looping over the table rows. 即使使用新粘贴的代码,问题仍然存在,由于循环遍历表行,您只能输出屏幕上当前可用的内容。 Instead you need to take it a step further and build objects to house the information you need for building your email, then place those objects in storage for later retrieval. 相反,您需要更进一步,并构建对象来容纳构建电子邮件所需的信息,然后将这些对象存储在存储中以供以后检索。

Again looking at your code your checkboxes have a prefix of "check-id". 再次查看您的代码,您的复选框的前缀为“ check-id”。

$('input:checkbox').on('change', function() {
    var $el = $(this);
    var key = $el.prop('id');
    var rowObject = {};

    //check if already in storage
    if(sessionStorage.getItem(key) !== null){
     rowObject = JSON.parse(sessionStorage.getItem(key));
     rowObject.checkedVal = $el.is(':checked'); //update it's check value
    }else{
     //build entire row object
     var current_tr = $(this).parent().parent();

     rowObject.loc = current_tr.find('.loc').data('loc');
     rowObject.rpCode = current_tr.find('.loc').data('rp-code');
     rowObject.sku = current_tr.find('.loc').data('sku');
     //etc. as many pieces as you need
    }

    //set to session
    sessionStorage.setItem(key, JSON.stringify(rowObject));

});

//then later for your send email method
function sendEmail(){
  var link = "mailto:me@example.com"
            + "?subject=" + encodeURIComponent("Order")
            + "&body=";

   var body = '';

  //loop over the row objects instead
  $.each(sessionStorage, function(key, value){
    //sort out the ones we want
    if(key.indexOf("checkid-") > -1){
      var rowObject = JSON.parse(sessionStorage.getItem(key));
      body +=  encodeURIComponent(rowObject.loc + '\xa0\xa0');
      body +=  encodeURIComponent(rowObject.rpCode + '\xa0\xa0');
      body +=  encodeURIComponent(rowObject.sku + '\xa0\xa0');
      //etc. as many pieces as you need

      body += '%0D%0A';
    }
  });

  //whatever other logic you have
}

I haven't fully tested this code for your purposes but you get the idea, build your objects then use those during your iteration. 我尚未针对您的目的完全测试此代码,但您明白了,构建对象,然后在迭代过程中使用它们。

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

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