如何使用CodeIgniter进行x-editable?

Ahm*_*Els 2 codeigniter x-editable

我想了解在我的CodeIgniter第一个项目中使用x-editable.我试图阅读x-editable docs,但我也是JavaScript的初学者,所以我无法理解

我制作简单的控制器来从JavaScript收集数据,但我没有完成它或数据库中没有更新的数据.

$('#username').editable({
    type: 'text',
    pk: 1,
    url: '/post',
    title: 'Enter username'
});
Run Code Online (Sandbox Code Playgroud)

如何在控制器或模型中提交数据以处理数据库更新查询

我想将从x-editable提交的数据传递给模型以在数据库中更新它.

San*_*kar 7

你可以按照这个简单的步骤假设$ userId = 5; $ username ="admin"; 考虑你的HTML看起来像这样

<a type="text" name="username" onclick="setEditable(this);" data-pk="<?php echo $userId ;?>" data-placeholder="Enter Username" data-name="username" data-type="text" data-url="<?php echo site_url();?>user/updateUserName" data-value="<?php echo $username ;?>" data-prev="admin"  data-title="Enter Username"><?php $username; ?></a>
Run Code Online (Sandbox Code Playgroud)

在Javascript代码中写下面的内容

$ .fn.editable.defaults.mode ='inline';

function setEditable(obj) {
    $(obj).editable({
        emptytext: $(obj).attr('data-value'),
        toggle: 'dblclick',
        mode: 'inline',
        anim: 200,
        onblur: 'cancel',
        validate: function(value) {
            /*Add Ur validation logic and message here*/
            if ($.trim(value) == '') {
                return 'Username is required!';
            }

        },
        params: function(params) {
            /*originally params contain pk, name and value you can pass extra parameters from here if required */
            //eg . params.active="false";
            return params;
        },
        success: function(response, newValue) {
            var result = $.parseJSON(response);
            $(obj).parent().parent().find('.edit-box').show();
            $(obj).attr('data-value', result.username);
            $(obj).attr('data-prev', result.username);

        },
        error: function(response, newValue) {
            $(obj).parent().parent().find('.edit-box').hide();
            if (!response.success) {
                return 'Service unavailable. Please try later.';
            } else {
                return response.msg;
            }

        },
        display: function(value) {
            /*If you want to truncate*/
            var strName = strname !== '' ? strname : $(obj).attr('data-value');
            var shortText = '';
            if (strName.length > 16)
            {
                shortText = jQuery.trim(strName).substring(0, 14).split("").slice(0, -1).join("") + "...";
            }
            else {
                shortText = strName;
            }
            $(this).text(shortText);
        }
    });
    $(obj).editable('option', 'value', $(obj).attr('data-value'));    
}
Run Code Online (Sandbox Code Playgroud)

在Controller站点中

<?php
class User extends CI_Controller
{

    function __construct()
    {
        parent::__construct();
    }

    function updateUserName()
    {
        $this->load->model('user_model');
        if ($this->input->is_ajax_request()) {

            $valueStr = $this->input->get_post('value') ? $this->input->get_post('value') : '';
            $new_nameStr = trim($valueStr);
            $result_arr['username'] = $new_nameStr;
            $userId = $this->input->get_post('pk') ? $this->input->get_post('pk') : '';
            $data['username'] = $new_nameStr;
            $result_arr['username'] = $new_nameStr;
            $this->user_model->userUpdateFunction($data, $userId);
        }
        echo json_encode($result_arr);
        exit;
    }
}
Run Code Online (Sandbox Code Playgroud)

您可以更改可编辑模式,我只设置了内联

  • 是的,这对我非常有帮助,谢谢兄弟 (2认同)