php - 从 mysql 查询中显示 bool 值引导表复选框列

标签 php jquery mysql checkbox

我有 mysql 表:

CREATE TABLE bd_grid.tbl_datatable (
`id`         int(10) NOT NULL auto_increment,
`name`       varchar(250) NOT NULL default '',
`email`      varchar(250) NOT NULL default '',
`mobile`     varchar(250) NOT NULL default '',
`start_date` timestamp DEFAULT CURRENT_TIMESTAMP,
`status`     TINYINT(1),
PRIMARY KEY  (`id`));

“状态”列用于插入 bool 值 (0,1),这是我如何插入 bool 值的一个示例。

INSERT INTO bd_grid.tbl_datatable(`name`,`email`,`mobile`,`status`) VALUES('Ricardo Alejandro Lopez Reyes','lopezr853@gmail.com','8443853097',true);

所有这些值,我使用 bootstrap 表来显示它们,但我想要显示“STATUS”列中的值的方式是一个复选框列。如果“STATUS”的值为 0,则复选框未选中,如果 status 的值为 1,则复选框已选中。

        <script type="text/javascript">

            var $table = $('#table');

                $table.bootstrapTable({
                    url:'list-user.php',
                    search:true,
                    pagination: true,
                    buttonsClass: 'primary',
                    showFooter: true,
                    minimumCountColumns: 2,

                    columns: [{
                        field: 'id',
                        title: '#',
                        sortable: true
                    },{
                        field: 'name',
                        title: 'Nombre',
                        sortable: true
                    },{
                        field: 'email',
                        title: 'E-mail',
                        sortable: true
                    },{
                        field: 'mobile',
                        title: 'Celular',
                        sortable: true
                    },{
                        field: 'start_date',
                        title: 'Fecha Inicio',
                        sortable: true
                    },{
                        field: 'status',
                        title: 'Estatus',
                        checkbox: true,
                        checkboxEnabled: true
                    }]
                });
        </script>
        </BODY>
</HTML>

当我加载我的 bootstrap 表时,所有复选框都未选中,我使用此文件迭代 mysql 表中的所有数据

请检查下面的 list-user.php 代码。我正在使用此代码将信息加载到 BOOTSRAP 表中。条件 if ($rowList['status'] == 1),我想将复选框的状态更改为未选中,但我真的不知道如何操作“状态”列更改复选框。

list-user.php

<?php
require 'db.php';

$sqltran = mysqli_query($con, "SELECT * FROM tbl_datatable ")or die(mysqli_error($con));
$arrVal = array();

$i=1;
while ($rowList = mysqli_fetch_array($sqltran)) {

    $name = array(
        'id' => $i,
        'name'=> $rowList['name'],
        'email'=> $rowList['email'],
        'mobile'=> $rowList['mobile'],
        'start_date'=> $rowList['start_date'],
        'status'=> $rowList['status']


    );

    if ($rowList['status'] == 1) {


        // CODE TO CHANGE THE STATUS OF THE CHECKBOX TO checked

        array_push($arrVal, $name);
        $i++;
    }

    else {

        // CODE TO CHANGE THE STATUS OF THE CHECKBOX TO unchecked
        array_push($arrVal, $name);
        $i++;
    }

}
echo  json_encode($arrVal);


mysqli_close($con);
?>

最佳答案

你可以尝试这样的事情:

在您的专栏中:

columns: [{
   field: 'id',
   title: '#',
   sortable: true
},{
   ...
},{
   field: 'check',
   title: 'Estatus',
   checkbox: true
},{
   field: 'status',
   visible: false
}]

加载数据表后:

$table.on('load-success.bs.table', function() {
    $table.bootstrapTable("checkBy", {
        field: "status", 
        values: [1]
    });
});

如果列状态的值为 1,则选中相应的复选框(来自列检查)。 checkBy 方法的文档:

Check a row by array of values, the params contains:

field: name of the field used to find records

values: array of values for rows to check

示例:$("#table").bootstrapTable("checkBy", {field:"field_name", values:["value1","value2","value3"]})

关于php - 从 mysql 查询中显示 bool 值引导表复选框列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43529256/

相关文章:

mysql - [MySQL] : DELETE rows from two dependent tables

php - 带有 PHP CURL 的 SSL 证书

php - 将变量传递给另一个 PHP 文件并执行它

javascript - Swiper - 初始化时宽度错误,调整大小后宽度正确

php - 使用 AJAX 将表单值提交到数据库

ios - 无法将类型 'NSNull' 的值转换为 'NSNumber'

php - 在 PHP 中通过 session 设置登录系统

php - laravel 4 文件名后备只能包含 ASCII 字符

jQuery 不会通过 ID 选择器找到元素,但会仅通过 ID 找到它

javascript - 在 jQuery 动画 div 的结束位置改变 element.style