twitter-bootstrap - Codeigniter + dataTable 使用 Bootstrap 模型自定义删除

标签 twitter-bootstrap codeigniter datatables jquery-datatables

到目前为止我有什么:

$(文档).ready(函数() {

        table = $('#users').DataTable( {
            "processing": true,
            "ajax": "<?php echo site_url('main/ajax_request'); ?>",
            "deferRender": true,
            "columns": [ 
                            { "data": "id", "width": "6%", },
                            { "data": "description" },
                            { "data": "name" },
                            { "data": "relation2" },

                            {
                                "data": null,
                                "width": "6%",
                                "className": "center",
                                "defaultContent": '<a href="<?php echo site_url("model/delete/"); ?>" class="editor_remove" data-toggle="modal" data-target="#myModal"><i class="fa fa-trash"></i></a>'
                            },

                       ],

            "dom": 'Tlfrtip',
            "aaSorting": [],
            "iDisplayLength": 25,
            "bStateSave": false,

            // table tools
            "tableTools": {
                sSwfPath: "<?php echo base_url(); ?>assets/plugins/datatable/TableTools/swf/copy_csv_xls_pdf.swf",
                aButtons: [
                { sExtends :'pdf',
                  oSelectorOpts: { filter: 'applied', 
                                   order: 'current', 
                                 },
                  sPdfOrientation: "landscape",
                  sPdfMessage: "Export Aplicatie",
                  bFooter: false,
                },

                { sExtends :'xls',
                  oSelectorOpts: { filter: 'applied', 
                                   order: 'current',
                                 },
                  bFooter: false,
                },

                { sExtends :'print',
                  oSelectorOpts: { filter: 'applied', 
                                   order: 'current',
                                 },
                  bFooter: false,
                },
                ],

                //"sRowSelect": "single",
            },
        } );

        // Setup - add a text input to each footer cell
        $('#users tfoot th').each( function () {
            var title = $('#users thead th').eq( $(this).index() ).text();
            $(this).html( '<input type="text" style="width:100%" class="form-control" />' );
        } );

        // Apply the search
        table.columns().eq( 0 ).each( function ( colIdx ) {
            $( 'input', table.column( colIdx ).footer() ).on( 'keyup change', function () {
                table
                    .column( colIdx )
                    .search( this.value )
                    .draw();
            } );
        } );
    } );

Controller :

// ajax request
public function ajax_request()  {
    $response = json_encode(array("data" => $this->Misc_model->getRecords() ));

    echo $response;
}

型号:

public function getRecords()    {
    $data = array();

    $this->db->select("records.id, records.description, relation_1.name, records.relation2")
             ->from('records')
             ->join('relation_1', "relation_1.id = records.relation", 'LEFT')
             ->where_not_in("deleted", '1');

    $query = $this->db->get();
    if($query->num_rows() > 0)  {
        foreach ($query->result() as $row) {
            $data[] = $row;
        }
    }

    return $data;
}

HTML:

        <table id="users" class="table table-bordered" cellspacing="0" width="100%">
            <thead>
                <tr>
                    <th>ID</th>
                    <th>Description</th>
                    <th>Single 1</th>
                    <th>Single 2</th>
                    <th>Delete</th>
                </tr>
            </thead>

            <tfoot>
                <tr>
                    <th>ID</th>
                    <th>Description</th>
                    <th>Single 1</th>
                    <th>Single 2</th>
                    <th>Delete</th>
                </tr>
            </tfoot>

        </table>

现在我的问题是:

如何获取 ID 值以便在以下位置使用它:

<?php echo site_url("model/delete/$id"); ?>

如果那是不可能的,是否有不同的方法来完成它?

最佳答案

在您定义数据的 getRecords 模型中:

public function getRecords()    {

    $data = $this->db->select("records.id, records.description, relation_1.name, records.relation2")
             ->from('records')
             ->join('relation_1', "relation_1.id = records.relation", 'LEFT')
             ->where_not_in("deleted", '1')
            ->get()->result();

    foreach($data as $d) {
        $d->href = '<a href="' . site_url("model/delete/" . $d->id) . '" class="editor_remove" data-toggle="modal" data-target="#myModal"><i class="fa fa-trash"></i></a>';
    }

    return $data;
}

在你的 jquery 数据表中替换这个:

{
"data": null,
"width": "6%",
"className": "center",
"defaultContent": '<a href="<?php echo site_url("model/delete/"); ?>" class="editor_remove" data-toggle="modal" data-target="#myModal"><i class="fa fa-trash"></i></a>'
},

像这样:

{ "data": "href" },

关于twitter-bootstrap - Codeigniter + dataTable 使用 Bootstrap 模型自定义删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29139201/

相关文章:

php - 是否可以使用相同的 URL 模式来访问 Controller 上的 2 个方法

php - 带有 codeigniter 外键插入的 doctrine2

jquery - 数据表:忽略嵌套向下钻取表时的 CSS 奇数/偶数样式

html - 使用 bootstrap 还是主题作弊?

php - 为什么我的 Bootstrap 代码没有响应?

c# - 如何在我的 ASP.NET 菜单中使用 BreadCrumb 的 Bootstrap 风格?

css - 如何使 Twitter Bootstrap 模式全屏

php - 在未来的 PHP 版本中,与类同名的方法将不再是构造函数; PasswordHash 有一个已弃用的构造函数

Jquery - 动态更改表的行高。

数据表分页下一个按钮不起作用