jQuery Nested Sortable in Codeigniter
I have adapted the nested sortable plugin to codeigniter, its updating the
order records in the database, but while i'm getting the all pages, its
not ordering in user interface, just ordering according to id there. I
have a get function in MY_Model, which is doing well, and ordering
everything else in the other pages, but only this one is lacking.
Order Controllers :
public function order ()
{
$this->data['sortable'] = TRUE;
$this->data['subview'] = 'admin/page/order';
$this->load->view('admin/_layout_main', $this->data);
}
public function order_ajax ()
{
// Save order from ajax call
if (isset($_POST['sortable'])) {
$this->page_m->save_order($_POST['sortable']);
}
// Fetch all pages
$this->data['pages'] = $this->page_m->get_nested();
// Load view
$this->load->view('admin/page/order_ajax', $this->data);
}
Order Model Functions:
public function save_order ($pages)
{
if (count($pages)) {
foreach ($pages as $order => $page) {
if ($page['item_id'] != '') {
$data = array('parent_id' => (int) $page['parent_id'],
'order' => $order);
$this->db->set($data)->where($this->_primary_key,
$page['item_id'])->update($this->_table_name);
}
}
}
}
public function get_nested ()
{
$pages = $this->db->get('pages')->result_array();
$array = array();
foreach ($pages as $page) {
if (! $page['parent_id']) {
$array[$page['id']] = $page;
}
else {
$array[$page['parent_id']]['children'][] = $page;
}
}
return $array;
}
Order Pages View :
<section>
<h2>Order Pages</h2>
<p class="alert alert-info">Hit the save, after you are done</p>
<div id="orderResult"></div>
<input type="button" id="save" value="Save" class="btn btn-primary" />
</section>
<script>
$(function() {
$.post('<?php echo site_url('admin/page/order_ajax'); ?>', {},
function(data){
$('#orderResult').html(data);
});
$('#save').click(function(){
oSortable = $('.sortable').nestedSortable('toArray');
$('#orderResult').slideUp(function(){
$.post('<?php echo site_url('admin/page/order_ajax'); ?>', {
sortable: oSortable }, function(data){
$('#orderResult').html(data);
$('#orderResult').slideDown();
});
});
});
});
</script>
Order Ajax View :
echo get_ol($pages);
function get_ol ($pages, $child = FALSE)
{
$str = '';
if (count($pages)) {
$str .= $child == FALSE ? '<ol class="sortable">' : '<ol>';
foreach ($pages as $page) {
#dump($item);
$str .= '<li id="list_' . $page['id'] .'">';
$str .= '<div>' . $page['title'] .'</div>';
// Do we have any children?
if (isset($page['children']) && count($page['children'])) {
$str .= get_ol($page['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>
Thanks
No comments:
Post a Comment