我跟随系列在tutlus上的codeigniter中构建了一个cms。
当我点击保存按钮时,我收到错误:$(...)。nestedSortable不是函数
控制器:
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() {
// Fetch all pages
$this->data['pages'] = $this->page_m->get_nested();
// Load view
$this->load->view('admin/page/order_ajax', $this->data);
}
型号:
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.php
<section>
<h2>Order pages</h2>
<p class="alert alert-info">Drag to order pages and then click 'Save'</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');
$.post('<?php echo site_url('admin/page/order_ajax'); ?>', { sortable: oSortable }, function(data) {
$('#orderResult').html(data);
});
});
});
</script>
order_ajax.php:
<?php
echo get_ol($pages);
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['title'] . '</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 type="text/javascript">
$(document).ready(function(){
$('.sortable').nestedSortable({
handle: 'div',
items: 'li',
toleranceElement: '> div',
maxLevels: 2
});
});
</script>
我在头页中包含了jquery,jquery-ui,jquery.nested文件。