我正在使用 CodeIgniter 3 和 Twitter Bootstrap 开发注册和登录应用程序。
我有一个“users”MySQL 表和一个相应的“users.php” View ,该 View 以 HTML 格式呈现“users”表,如下图所示:
Bootstrap 表中的“操作”列的每一行都有一个“启用”或“禁用”按钮,具体取决于用户的状态。这部分 View 的代码是:
// Status column
<td>
<?php if ($user->active == 1) {
echo '<span class="text-success">' . 'Enabled' . '</span>';
} else {
echo '<span class="text-danger">' . 'Disabled' . '</span>';
}
?>
</td>
// Enable/Disable buttons
<?php if ($user->active == 1) { ?>
<a href="<?php echo base_url(); ?>users/deactivate/<?php echo $user->id ?>" title="Deactivate" class="btn btn-success btn-xs state-change" data-role="deactivate" data-id="<?php echo $user->id ?>"><span class="glyphicon glyphicon-ban-circle"></span> Disable</a>
<?php } else { ?>
<a href="<?php echo base_url(); ?>users/activate/<?php echo $user->id ?>" title="Activate" class="btn btn-success btn-xs state-change" data-role="activate" data-id="<?php echo $user->id ?>"><span class="glyphicon glyphicon-ok"></span> Enable</a>
<?php } ?>
我通过 AJAX 激活/停用用户,无需刷新页面:
$('.state-change').on('click', function(evt) {
evt.preventDefault();
var id = $(this).data('id');
var role = $(this).data('role');
if (role == "activate") {
var stateUrl = 'users/activate/';
} else {
var stateUrl = 'users/deactivate/';
}
$.ajax({
url: stateUrl + id,
method: 'GET',
dataType: 'php',
success: function(){
console.log(id);
console.log(role);
}
});
});
问题在于有关用户状态的数据不会返回到 View 中,并且“状态”和“操作”列无法正确呈现。
我希望我不必使用 jQuery 的 html()
方法或类似方法从成功回调中“静态”更新 View 。
function(){
console.log(id);
console.log(role);
//change columns html here
}
我该怎么做才能“动态”更新 View ?
最佳答案
以上答案已经足够了 - 根据您的情况,您必须使用 JavaScript。看来麻烦在于从动态网页到动态 HTML 的跨越。
请阅读此处摘要的最后一行: https://en.wikipedia.org/wiki/Dynamic_HTML
关于php - Codeigniter 3应用程序: update MySQL table column and the corresponding view through Ajax,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48290823/