【问题标题】:How to organize code in CodeIgniter's templates?如何在 CodeIgniter 模板中组织代码?
【发布时间】:2018-06-06 20:16:30
【问题描述】:

我真的不知道如何在 CodeIgniter 项目中组织我的代码。在一个视图中,我有一个表单允许在提交后获得信息。但信息显示在同一视图上。信息以多种方式显示(例如使用 PHP 或控制器部分中的 Ajax 和 JSON 编码)。

我的看法:

<form action="#" method="post">
 <p>Your name : <input type="text" name="name" /></p>
 <p><input type="submit" value="OK"></p>
</form>

我可以在视图中使用它来验证用户是否按下了按钮提交?

if (isset($_POST['submit'])) {// the code I want to to show after submit}

点击提交按钮后我想显示的内容:

<select class="mylist">
    <?php foreach($groups as $each){ ?>
        <option value="<?php echo $each->groupname; ?>"><?php echo $each->groupname; ?></option>';
    <?php } ?>
</select>

  <table id="table" class="display" style="width:80%">
        <thead>
            <tr>
                <th>Name</th>
                <th>SurName</th>
                <th>ID</th>
            </tr>
        </thead>
        <tfoot>
            <tr> 
                <th>Name</th>
                <th>SurName</th>
                <th>ID</th>
            </tr>
        </tfoot>
</table>

控制器:

public function index()
{
    $this->load->view("myview.php");
}

public function getlist()
{
    $this->load->model('mymodel');
// Method to get the values of the list in the database

}
public function get_test_datatables()
{
// Method to fill the datatable part
echo json_encode($output);
}

JS Functions :

    $(document).ready( function () {
        $('#table').DataTable({
    //Get the data with ajax
    })

    )}

在我按下提交按钮后,我还想检查一个复选框是否被选中(如果它在数据库中等于true,我必须选中该框,如果它等于false,我不必检查)。我要不要在视图中做类似的事情:

if(checkbox->value == true)
{
<input type="checkbox" name="vehicle" value="Bike" checked> I have a bike<br>
}
else {
<input type="checkbox" name="vehicle" value="Bike"> I have a bike<br>
}

谢谢

【问题讨论】:

  • 显示完整的 ajax 函数和所有代码。有一个你可能想要为 CI 研究的插件,称为 ignited datatables,它可能会帮助你。否则,数据表文档非常适合这类事情。多尝试/充实一下,然后回来。就目前而言,我相信您要求我们为您做太多事情。

标签: javascript php codeigniter templates


【解决方案1】:

在我的项目中,我总是通过 ajax ($.post()) 发送数据并将视图划分为两个 div:一个带有列表,可见,另一个带有表单,不可见。 在列表部分,有一个按钮可以切换到表单。 当我点击提交时,发送数据,如果成功,显示成功消息并返回到列表 div。

HTML:

<div id="div-form" style="display: none;">
    <form>
     <p>Your name : <input type="text" name="name" /></p>
     <button type="button" id="save">OK</button>
     <button type="button" id="cancel">Cancel</button>
    </form>
</div>
<div id="div-list">
    <button type="button" id="add">Add</button>
    <select class="mylist">
        <?php foreach($groups as $each): ?>
            <option value="<?= $each->groupname; ?>">
                <?= $each->groupname; ?>
            </option>';
        <?php endif; ?>
    </select>

      <table id="table" class="display" style="width:80%">
            <thead>
                <tr>
                    <th>Name</th>
                    <th>SurName</th>
                    <th>ID</th>
                </tr>
            </thead>
            <tfoot>
                <tr> 
                    <th>Name</th>
                    <th>SurName</th>
                    <th>ID</th>
                </tr>
            </tfoot>
    </table>
</div>

Javascript:

$(document).ready( function () {
    $(document).on('click', "#add", function() {
        $("#div-form").show();
        $("#div-list").hide();
    });

    $(document).on('click', "#save", function() {
        $.post('service_link', {
            name: $("#name").val()
        }, function(data) {
            $("#div-list").show();
            $("#div-form").hide();
            $('#table').DataTable().ajax.reload();
        }, 'json');
    });

    $(document).on('click', "#cancel", function() {
        $("#div-list").show();
        $("#div-form").hide();
    });

    $('#table').DataTable({
    //Get the data with ajax
    })
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-08
    • 1970-01-01
    • 2019-09-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多