samedi 30 juillet 2016

How to sort tables based on checked row?

I have a questions regarding how to sort the table by checkbox column. Meaning that if the user check the checkbox, it will automatically put the checked one on top of the table (first row and so on). Seriously i never do this sorting based on checkbox column before. Also i search for it but all i saw was sorting the table when clicking the table header. I don't want that. I want to it to be dynamically update the table whenever user check it and bring the checked row to the top. I have this pic hereenter image description here

So i am using tablesorter and for search is ajax livesearch. Thus, if only i can make the checked row to be on first row, it would make it easier for the user to identify its checked row right.

Here is my code for ajax(index.php)

function ajaxSearchUpdater(p){
        $("#result").show();

        var x = $("#search").val();
        var y = $("#edulevel").val();
        var pagelim = $("#pagefpe").val();
        var pagenumber = p;
        var checkb = $(".sBorrow").val()
        $.ajax({
            type:'POST',
            url:'userres.php',
            data:'q='+x+'&e='+y+'&pagelim='+pagelim+'&pageno='+pagenumber+'&checkb='+checkb,
            cache:false,
            success:function(data){
                $("#result").html(data)
            }
        });
    }


    function setsession(sessionid,action){
        $("#totalselection").show();
        $.ajax({
            type:'POST',
            url:'test.php',
            data:'sBorrow='+sessionid+'&action='+action,
            cache:false,
            success:function(data){
                var out = "<p align='center' style='text-decoration:none;color:white;'>Total Selection: "+data+"<br/>Click here to submit your request<a href='borrowform.php' style='text-align:center;'><input type='button' value='REQUEST'></a>&nbsp;&nbsp;&nbsp;||&nbsp;&nbsp;&nbsp;Click here to clear the selection <a href='#' style='text-align:center;'><input type='button' value='CLEAR'></a></p>";
                $("#totalselection").html(out)
            }
        });
    }



    $(document).ready(function(e) {
        ajaxSearchUpdater(1);           // fires on document.ready
        $("#search").keyup(function() {
            ajaxSearchUpdater(1);           // your function call
        });
        $("#edulevel").click(function() {
            ajaxSearchUpdater(1);           // your function call
        });
        $("#pagefpe").click(function() {
            ajaxSearchUpdater(1);           // your function call
        });
        $(document).delegate('.sBorrow', 'change', function(){
            var sBorrowClass = $(this).attr('class');
            var sBorrowValue = $(this).attr('value');
            var sBorrowName = $(this).attr('name');

            if(this.checked){
                setsession(sBorrowValue, "SET");
            }
            else{
                setsession(sBorrowValue, "UNSET");
            }

            //$("#name_" + cont_det_id).hide(); //hide span
            //$("#input_name_" + cont_det_id).show(); //show editable box

        })/*.delegate('.sBorrow', 'keypress', function (e) {
            if(e.which == 13) { //if statement for pressing enter button
                var cont_det_id = $(this).attr('id');
                //alert cont_det_id good here
            }
        });*/

Here is my code for echoing the table from database (userres.php)

if($stmt->rowCount() > 0){
    $r=$stmt->fetchAll();
    echo "<table class='tablesorter-blackice' id='myTable' style='width:97%; table-border: 1'>";
        echo "<thead>";
        echo "<tr>";
        echo "<th>No.</th>";
        echo "<th>No.Matric</th>";
        echo "<th>Name</th>";
        echo "<th>Programme</th>";
        echo "<th>Title</th>";
        echo "<th>Thesis Level</th>";
        echo "<th>Serial Number</th>";
        echo "<th>Availability</th>";
        echo "<th>Select book (Max 3)</th>";
        echo "</tr>";
        echo "</thead>";
        echo "<tbody>";

        if(isset($_SESSION['sBorrow']))
            $arraynosiri = $_SESSION['sBorrow'];
        else
            $arraynosiri = array();



    foreach($r as $row){

            echo "<tr align='center'><td>". ($startrow+1) ."</td><td>". $row['matricno'] ."</td><td>". $row['studentname'] ."</td><td>". $row['programme'] ."</td><td>". $row['title'] ."</td><td>". $row['thesis_level'] ."</td><td>". $row['serialno'] ."</td><td>". $row['bavailable'] ."</td><td>
            <form method='post'>";
            if($key = array_search($row['serialno'], $arraynosiri) !== false) {
                $checkbox = "checked";
            }
            else{
                $checkbox = "";
            }
            echo "<input type='checkbox' name='sBorrow' id='sBorrow' class='sBorrow' value='". $row['serialno'] ."' ".$checkbox.">
            </form></td></tr>";
            $startrow++;
            //echo $row['education_level'];


    }
    echo "</tbody>";
    echo "</table>";
}
else{
    echo "<p align='center'>Nothing to show you :( I am really sorry for this T_T </p>";
}

?>

<script type="text/javascript">
    $(document).ready(function() 
    { 
        $("#myTable").tablesorter();
        setsession(null,null);

    } 
    ); 
</script>

I use session and array to store the value of checked row so that it can be called to other php page.

<?php

session_start();
if(isset($_POST['sBorrow']) && $_POST['action']){



    if(isset($_SESSION['sBorrow']) && is_array($_SESSION['sBorrow'])){
        $sborrow = $_POST['sBorrow'];
        $set = $_SESSION['sBorrow'];
    }
    else{
        $set = array();
    }

    if($_POST['action'] == "SET"){
        array_push($set, $_POST['sBorrow']);
        $_SESSION['sBorrow'] = $set;

        //QUERY FOR SORTING TABLE????
    }
    else if($_POST['action'] == "UNSET"){
            $unset = $_SESSION['sBorrow'];
            if(($key = array_search($_POST['sBorrow'], $unset)) !== false) {
                unset($unset[$key]);
                $_SESSION['sBorrow'] = $unset;
            }
    }

}
//session_destroy();
if(isset($_SESSION['sBorrow']))
    echo count($_SESSION['sBorrow']);



?>




Aucun commentaire:

Enregistrer un commentaire