无法保存订购数据库 - Codeigniter

时间:2015-08-31 01:57:58

标签: jquery codeigniter

我正在使用Codeigniter,在其中我试图通过先拖放重新排序和单击保存以保存重新排序来保存列表排序。但是,列表不会被保存。任何帮助,将不胜感激。

查看文件:

<div class="container">
    <div class="row">
        <div class="col-md-8 col-md-offset-2">
            <section>
                <h2>Order Songs</h2>
                <p class="alert alert-info">Drag to order Songs and click 'Save'</p>
                <div id="orderResult"></div>
                <? echo form_button('save', 'Save', 'class="btn btn-default" id="save" value="Save"')?>
            </section>
        </div>
    </div>
</div>



<script>
$(function() {
        $.post('<?php echo site_url('admin/songs/order_ajax'); ?>', {}, function(data){
                $('#orderResult').html(data);
        });

        $('#save').click(function(){

                oSortable = $('.sortable').nestedSortable('toArray');

                $('#orderResult').slideUp(function(){
                        $.post('<?php echo site_url('admin/songs/order_ajax'); ?>', { sortable: oSortable }, function(data){
                                $('#orderResult').html(data);
                                $('#orderResult').slideDown();
                        });
                });

        });
});
</script>

order_ajax文件:

<?php
echo get_ol($songs);

function get_ol ($array, $child = FALSE)
{
        $str = '';

        if (count($array)) {
                $str .= $child == FALSE ? '<ol class="sortable">' : '<ol>';

                foreach ($array as $item) {
                        $str .= '<li id="list_' . $item['id'] .'">';
                        $str .= '<div>' . $item['songname'] .'</div>';

                        // Do we have any children?
                        if (isset($item['children']) && count($item['children'])) {
                                $str .= get_ol($item['children'], TRUE);
                        }

                        $str .= '</li>' . PHP_EOL;
                }

                $str .= '</ol>' . PHP_EOL;
        }

        return $str;
}
?>

<script>
$(document).ready(function(){

    $('.sortable').nestedSortable({
        handle: 'div',
        items: 'li',
        toleranceElement: '> div',
        maxLevels: 2
    });

});
</script>

控制器:

public function order() {
    $data['sortable'] = TRUE;
    $data['title'] = 'Re-Order';
    $data['module'] = 'admin';
    $data['header_file'] = 'admin_header';
    $data['nav_file'] = 'admin_nav';
    $data['view_file'] = 'song_order';
    $data['script_file'] = 'admin_script';
    echo Modules::run('template/template_admin', $data);
}

public function order_ajax() {
    // Save order from ajax call
    if (isset($_POST['sortable'])) {
        $this->mdl_admin_songs->save_order($_POST['sortable']);
    }

    // Fetch all pages
    $data['songs'] = $this->mdl_admin_songs->get_nested();

    // Load view
    $this->load->view('song_order_ajax', $data);
}

型号:

public function get_nested() {
    $songs = $this->db->get('songrequest')->result_array();

    $array  = array();
    foreach ($songs as $song) {
        if (!$song['parent_id']) {
            $array[$song['id']] = $song;
        }
        else {
            $array[$song['parent_id']]['children'][] = $song;
        }
    }
    return $array;
}

public function save_order($songs) {
    if (count($songs)) {
        foreach ($songs as $order => $song) {
            if ($song['item_id'] != '') {
                $data = array('parent_id' => (int) $song['parent_id'], 'order' => $order);
                $this->db->set($data)->where($this->_primary_key, $song['item_id'])->update($this->_table_name);
            }
        }
    }
}

谢谢, 纳温

0 个答案:

没有答案