Ответ 1
У меня есть те же требования, что и у вас, и тренировка с этим взглядом на мой код, который почти такой же, как у вас,
Вот мой контроллер:
public function select_menu_priority() {
$data['product'] = $this->menu_model->select_menu_priority();
$data['li'] = $this->generate_li($data['product']);
$this->load->view("set_menu_priority_table", $data);
}
function generate_li($product,$parent = NULL){
$li = "";
$p1 = array_filter($product, function($a)use($parent){ return $a['parent_menu_id'] == $parent; });
foreach ($p1 as $p){
$inner_li = "";
$p2 = array_filter($product,function($a)use($p){ return $a['parent_menu_id'] == $p['id']; });
if($p2){
$inner_li = $this->generate_li($product,$p['id']);
}
$li .= "<li class='dd-item' data-id='".$p['id']."'><div class='dd-handle'>".$p['title']."</div>".$inner_li."</li>";
}
$ol = "<ol class='dd-list'>".$li."</ol>";
return $ol;
}
Просмотр set_menu_priority_table.php:
<?php
if (isset($product)) {
$entity = $this->input->post("entity");
$entity = $entity['id'];
if (count($product) > 0) {
?>
<div class="row-fluid" style="margin-bottom: 10px;">
<button class="btn btn-success btn-sm" tabindex="4" id="save">
<i class="fa fa-check"></i> Save
</button>
<p class="pull-right" style="margin-bottom: 10px;"><?php if ($entity == "product") { ?><a href="javascript:void(0)" id="show_category" class="text-success" style="margin-right:10px;font-weight: bold;text-decoration: underline">Go to Category Priority</a><?php } ?><span class="label label-info ">Drag Menu to set Priority.</span></p>
<div class="clear"></div>
</div>
<div class="dd" id="product_list">
<input type="hidden" id="entity_type" name="entity" value="<?php echo $entity ?>" />
<?php echo $li; ?>
</div>
<?php } else { ?>
<p><span class="label label-warning">No <?php echo ($entity == "product") ? "product" : "category"; ?> found.</span><?php if ($entity == "product") { ?><a href="javascript:void(0)" id="show_category" class="text-success" style="margin-left:15px;font-weight: bold;text-decoration: underline">Go to Category Priority</a><?php } ?></p>
<?php
}
} else {
?>
<p class="label label-info">Please select Category to Set Priority within the Category.</p>
<?php } ?>
<script type="text/javascript">
$("#save").off("click").on("click", function() {
var product_data = $("#product_list").nestable("serialize");
var data = {product_data: product_data, entity: $("#entity_type").val()};
if ($.bbq.getState("product_category") !== undefined) {
data['product_category'] = $.bbq.getState("product_category");
}
ajax_call({
url: '<?php echo site_url("admin/menu/update_menu_priority");?>',
type: "post",
dataType: "json",
data: data,
beforeSend: function() { },
success: function(result) {
if (result['status'] == "success") {
alert("Priority Updated!");
}
});
});
</script>
Обновить функцию приоритета добавления update_menu_priority
в контроллере:
public function update_menu_priority() {
$data = $this->input->post("product_data");
if (count($data)) {
$update = $this->menu_model->update_priority_data($data);
if ($update) {
$result['status'] = "success";
} else {
$result['status'] = "error";
}
} else {
$result['status'] = "error";
}
echo json_encode($result);
}
И, наконец, функция объявления для этой update_priority_data
:
function update_priority_data($data, $parent = NULL) {
$i = 1;
foreach ($data as $d) {
if (array_key_exists("children", $d)) {
$this->update_priority_data($d['children'], $d['id']);
}
$update_array = array("priority" => $i, "parent_menu_id" => $parent);
$update = $this->db->where("id", $d['id'])->update("menu", $update_array);
$i++;
}
return $update;
}
Надеюсь, это поможет вам, спасибо.