read checkboxes value as id user

read checkboxes value as id user

JCR1951JCR1951 Posts: 34Questions: 6Answers: 0
edited June 2016 in Free community support

I need an advice.
I am using checkboxes.
Have a lot of members.
They can choose a max. of 25 members they like to follow. It is saved in a string in the database.
Now I want the value of the checkboxes as the user_id's.
So I can check a string from a member in the database with numbers of id's.
If the number of the checkbox is in the string it has to be checked.
Otherwise it has to be unchecked.
I think it has to be done in the render function.

I can use a hidden column with the id's.
But maybe with rendering, the ID column can be rendered too.

Till now I am using this link (but works with on.click)

http://pctraverse.nl/DataTables-1.10.11/examples/server_side/simpleZ.html

Answers

  • JCR1951JCR1951 Posts: 34Questions: 6Answers: 0
    edited June 2016

    I have one answer already.
    But I still needed a simple script to sort on checked and not checked.
    It sorts now on the ID.
    Even if I tried 'full.id = '1' for checked and full.id = '0' for unchecked.

    FROM MY DATABASE

          <?php $my_var = '3-10-6-'; ?>
    

    IN MY SCRIPT (the class and value is not needed)

          { "render": function (data, type, full, meta){                    
                        var my_var = <?php echo json_encode($my_var); ?>;
                        var ide = full.id+"-";  
                        var n = my_var.search(ide);
                        if (n>-1)
                            {
                            return '<a href= "#"><input type="checkbox" name = "test" class= "chb" value = "1" checked/></a>';
                            }
                            else
                            {       
                                return '<a href="#"><input type="checkbox" name = "test" class= "chb" value = "0" /></a>';
                        }           
                        }
                        },          
    
  • jr42.gordonjr42.gordon Posts: 305Questions: 2Answers: 49

    Why not add user_id to the tr element as an identifier? You can then save this in your database along with value of checkbox. So you save only user_id's that a user has checked and mark these accordingly within DataTable.

    For sorting, try this trick.

    span.full_val {
       display: none;
    }
    
    <span class="full_val">(value)</span>
    

    --- or ---
    Add https://datatables.net/reference/option/columns.type to this column with your own custom corting function

    // in DataTable config
    "type"    : "single-text-input"
    
    // custom sort function
            $.fn.dataTable.ext.type.order['single-text-input-pre'] = function ( a ) {
                // This is for columns that contain a single HTML <input> that need to utilize padRight
                // user for inputs with letters
                return $(a).val().padRight($(a).attr('maxlength'));
            };
    
  • JCR1951JCR1951 Posts: 34Questions: 6Answers: 0

    Thanks for the reply Gordon.
    I already make a string on the user_id in my database as I showed with $my_var.
    The given function works oke.
    And I'm using the costum_id's. The rendering is in the column of the ID's.
    But with the normal sorting it takes the given ID's.
    I will try your options, thank you.

This discussion has been closed.