简体   繁体   English

检查jQuery数据表中的预选行

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

I have a jQuery DataTable below: 我在下面有一个jQuery DataTable:

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

And here's my HTML: 这是我的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>

What I want to do: 我想做的事:

I need to load all the registered users from my Users table and check each user in my datatable whose ID is in my Examinees table. 我需要从“用户”表中加载所有注册用户,并检查数据表中ID在“检查表”中的每个用户。

I am updating a list of examinees, so the user can remove(uncheck) existing examinees and add more examinees. 我正在更新应试者列表,因此用户可以删除(取消选中)现有应试者并添加更多应试者。 I can get the checked rows before submitting the form, but I don't have a way to determine which of the existing examinees were unchecked so I could remove there ID in the Examinees table. 我可以在提交表单之前获取已检查的行,但是我无法确定未检查哪些现有应试者,因此我可以在“应试者”表中删除ID。

What could be a best approach for this? 什么是最好的方法呢? I don't feel that what I did in my HTML is the best solution. 我不认为我在HTML中所做的事情是最好的解决方案。

There are two ways of doing it: 有两种方法:

  1. Clear every examinee id from the database before adding the ones that come with the POST data. 在添加POST数据附带的ID之前,请从数据库中清除每个应试ID。
  2. Implode the POST id array, delete the ones NOT IN (id1, id2, ...) and add the new examinees. 放大POST id数组,删除NOT IN (id1, id2, ...)数组NOT IN (id1, id2, ...)然后添加新的考生。

PS. PS。 To avoid duplicate code move the examinee == user condition to the checked attr. 为了避免重复的代码,将应试者==用户条件移动到已检查的attr。

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

PPS. PPS。 the examinee foreach seems weird, maybe you should do a subselect. 应试者的foreach似乎很奇怪,也许您应该进行子选择。

I have found a way to get my desired output, in my datatable I used the fnCreatedRow function to check each created row if it's in the EXISTING EXAMINEES array: 我找到了一种获取所需输出的方法,在我的数据表中,我使用了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);
                        }
                    }
                }
            });
        }
    });

I also added an onClick listener to add existing examinees that are unchecked which will be submitted to the server when the form is submitted. 我还添加了一个onClick侦听器,以添加未选中的现有应试者,当提交表单时,这些应试者将被提交到服务器。

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

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