Search code examples
codeigniterx-editable

How to use x-editable with CodeIgniter?


I would like to understand using x-editable in my CodeIgniter first project. I tried to read x-editable docs but I'm beginner in JavaScript too so I can't understand

I make simple controller to collect data from JavaScript but I didn't complete it or data not updated in database.

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

How to get submitted data in controller or model to process database update query

I want to passing data submitted from x-editable to model to update it in database.


Solution

  • You can follow this simple steps Assume that $userId = 5 ; $username = "admin"; Consider you html look like this

    <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>
    

    In Javascript code write following

    $.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'));    
    }
    

    In Controller site

    <?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;
        }
    }
    

    You can change editable mode , i have set inline only