繁体   English   中英

检查jQuery数据表中的预选行

[英]Check pre-selected rows in a jQuery Datatable

我在下面有一个jQuery DataTable:

examinees_table = $('#examinees_table').dataTable({
        "bJQueryUI": true,
        "sPaginationType": "full_numbers",
        "aoColumns": [
            /*select*/      null,
            /*id*/          {"bVisible": false},
            /*name*/    null,
            /*course*/  null
        ]
    });

这是我的HTML:

<table id="examinees_table">
                        <thead>
                            <tr>
                                <th>Select</th>
                                <th>ID</th>
                                <th>Full Name</th>
                                <th>Degree</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php foreach($users as $user) {?>
                                <tr>
                                    <?php foreach ($examinees as $examinee) {?>
                                        <?php if($examinee->examinee_id == $user->id) {?>
                                            <td class="center"><?= Form::checkbox('is_examinee-'.$user->id, null, (bool) true, array('id' => $user->id)); ?></td>
                                        <?php } else {?>
                                            <td class="center"><?= Form::checkbox('is_examinee-'.$user->id, null, (bool) false, array('id' => $user->id)); ?></td>
                                        <?php } ?>
                                    <td class="center"><?= $user->id; ?></td>
                                    <td class="center"><?= $user->first_name.' '.$user->last_name; ?></td>
                                    <td class="center"><?= $user->courses->description; ?></td>
                                    <?php }?>
                                </tr>
                            <?php }?>
                        </tbody>
                    </table>

我想做的事:

我需要从“用户”表中加载所有注册用户,并检查数据表中ID在“检查表”中的每个用户。

我正在更新应试者列表,因此用户可以删除(取消选中)现有应试者并添加更多应试者。 我可以在提交表单之前获取已检查的行,但是我无法确定未检查哪些现有应试者,因此我可以在“应试者”表中删除ID。

什么是最好的方法呢? 我不认为我在HTML中所做的事情是最好的解决方案。

有两种方法:

  1. 在添加POST数据附带的ID之前,请从数据库中清除每个应试ID。
  2. 放大POST id数组,删除NOT IN (id1, id2, ...)数组NOT IN (id1, id2, ...)然后添加新的考生。

PS。 为了避免重复的代码,将应试者==用户条件移动到已检查的attr。

<?= Form::checkbox('is_examinee-'.$user->id, null, ($examinee->examinee_id == $user->id), array('id' => $user->id)); ?>

PPS。 应试者的foreach似乎很奇怪,也许您应该进行子选择。

我找到了一种获取所需输出的方法,在我的数据表中,我使用了fnCreatedRow函数来检查每个创建的行是否在EXISTING EXAMINEES数组中:

examinees_table = $('#examinees_table').dataTable({
        "bJQueryUI": true,
        "sPaginationType": "full_numbers",
        "aoColumns": [
            /*select*/      null,
            /*id*/          {"bVisible": false},
            /*name*/    null,
            /*course*/  null
        ],
        "fnCreatedRow": function( nRow, aData, iDataIndex ) {
            var eID = $('#existing_examinees').val();
            if(eID === undefined) {
                return;
            }

            var eIDArray = eID.split('/');

            var deletedID = $('#removed_examinees').val();
            var deletedIDArray = deletedID.split('/');

            if(eIDArray.length > 0) {
                $.each(eIDArray, function(index, value) {
                    var id = $("input:checkbox", nRow).attr('id');                      
                    // alert('index: ' + index + ' ' + 'value: ' + value + ' ' + 'id: ' + id);
                    if(id == value) {
                        $("input:checkbox", nRow).prop('checked', true);
                    }
                });
            }
            $("input:checkbox", nRow).click(function() {
                var selected_id = $("input:checkbox", nRow).attr('id');
                var checked = $("input:checkbox", nRow).prop('checked');
                if(checked == false) {
                    var deleted_index = $.inArray(selected_id, eIDArray);
                    var inDeletedArr = $.inArray(eIDArray[deleted_index], deletedIDArray);
                    if(inDeletedArr < 0) {
                        if(deleted_index > -1) {
                            var removed = $('#removed_examinees').val();
                            if(removed == '') {
                                removed = eIDArray[deleted_index];
                            } else {
                                removed = removed + '/' + eIDArray[deleted_index];
                            }
                            $('#removed_examinees').val(removed);
                        }
                    }
                }
            });
        }
    });

我还添加了一个onClick侦听器,以添加未选中的现有应试者,当提交表单时,这些应试者将被提交到服务器。

暂无
暂无

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

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