<td class="td-operation" align="center">
<a href="###" id="updates" class="operation-update theme_text_color" data-id="<?php echo $v['id'];?>">编辑</a>
<input type="hidden" value="<?= $v['id']; ?>" id='categoryid' name="categoryid"/>
<?php if( ($k+1)!=1 ): ?>
<a style="padding-left:0;" href="javascript:void(0)" title="上移" data-id="<?= $v['id']; ?>" class="operation-update up theme_text_color" data-pid="<?= $v['pid']; ?>">上移</a>
<?php endif; ?>
<?php if( ($k+1)!= $num): ?>
<a style="padding-left:0;" href="javascript:void(0)" title="下移" data-id="<?= $v['id']; ?>" class="operation-update down theme_text_color" data-pid="<?= $v['pid']; ?>">下移</a>
<?php endif; ?>
<a class="operation-update theme_text_color" href="#" onclick="f1(<?php echo $v['id'] ?>)">删除</a>
</td>
<script>
$(function () {
$(".instroction").each(function(){
var maxwidth=30;
if($(this).text().length>maxwidth){
$(this).text($(this).text().substring(0,maxwidth));
$(this).html($(this).html()+"...");
}
});
$('.view-index-list').on("click",'.down', function() {
var $tr = $(this).parents("tr");
var len = $tr.parent('tbody').children('tr').size();
var dataid = $(this).data('id');
var siblingsId = $(this).parents("tr").next('tr').children('td').children("#categoryid").val();
var requestUrl = '<?php echo $this->createUrl('questionsSort');?>/type/' + 'down' + '/sortid/' + dataid + '/siblingsId/'+ siblingsId;
$.ajax( {
type: 'get',
url: requestUrl,
success:function (data) {
var datas = eval("("+ data +")");
if(datas.code == 200){
history.go(0);
}else if (datas.code == 400 && datas.status == 2){
alert('已经是最下层级了');
}else if(datas.code == 500){
alert('只能同级移动');
}
}
})
});
$('.view-index-list').on("click",'.up', function() {
console.log($(this).data())
var $tr = $(this).parents("tr");
var len = $tr.parent('tbody').children('tr').size();
var dataid = $(this).data('id');
var siblingsId = $(this).parents("tr").prev('tr').children('td').children("#categoryid").val();
requestUrl = '<?php echo $this->createUrl('questionsSort');?>/type/' + 'up' + '/sortid/' + dataid + '/siblingsId/'+ siblingsId;
$.ajax({
type:'get',
url:requestUrl,
success:function (data) {
var datas = eval("("+ data +")");
console.log(datas);
console.log(typeof (datas));
if(datas.code==200){
history.go(0);
}else if(datas.code == 400 && datas.status == 1){
alert('已经是最上层级了');
} else if(datas.code == 500){
alert('只能同级移动');
}
}
})
});
function downDocument($tr, len) {
number_pre = $tr.children('td').eq(0).html();
number_next = $tr.next('tr').children('td').eq(0).html();
$tr.fadeOut().fadeIn();
$tr.next().after($tr);
$tr.children('td').eq(0).html(number_next);
$tr.prev('tr').children('td').eq(0).html(number_pre);
if ($tr.prev('tr').copyindex() == 0) {
$tr.prev('tr').children('td').eq(3).find('.up').remove();
}
var this_index = $tr.copyindex();
if (this_index == 1) {
var categoryid = $tr.children('td').eq(3).find("#categoryid").val();
var string = '<a style="padding-left:0;" href="javascript:void(0)" title="上移" data-id=' + categoryid + ' class="operation-update up">上移</a>';
$tr.children('td').eq(3).find('#categoryid').after(string);
}
if ((this_index + 1) == len) {
$tr.children('td').eq(3).find('.down').remove();
var categoryid = $tr.prev('tr').children('td').eq(3).find("#categoryid").val();
var string = '<a style="padding-left:0;" href="javascript:void(0)" title="下移" data-id=' + categoryid + ' class="operation-update down"> 下移</a>';
$tr.prev('tr').children('td').eq(3).find('.up').after(string);
}
}
function UpDocument($tr, len) {
number_pre = $tr.children('td').eq(0).html();
number_parent = $tr.prev('tr').children('td').eq(0).html();
$tr.fadeOut().fadeIn();
$tr.prev().before($tr);
$tr.children('td').eq(0).html(number_parent);
$tr.next('tr').children('td').eq(0).html(number_pre);
var this_index = $tr.copyindex();
if (this_index == 0) {
$tr.children('td').eq(3).find('.up').remove();
var categoryid = $tr.next().children('td').eq(3).find("#categoryid").val();
var string = '<a style="padding-left:0;" href="javascript:void(0)" title="上移" data-id=' + categoryid + ' class="operation-update up">上移</a>';
$tr.next().children('td').eq(3).find('#categoryid').after(string);
}
if ((this_index + 1) == (len - 1)) {
$tr.next('tr').children('td').eq(3).find('.down').remove();
var categoryid = $tr.children('td').eq(3).find("#categoryid").val();
var string = '<a style="padding-left:0;" href="javascript:void(0)" title="下移" data-id=' + categoryid + ' class="operation-update down"> 下移</a>';
$tr.children('td').eq(3).find('.up').after(string);
}
}
})
function gobacke(){
location.href= "<?= $this->createUrl('/train/train/index'); ?>";
}
</script>
public function getCategorySort($config){
$sortData = $this->sortData($config['sortId']);
$sortInt = $config['type'] == 'up' ? $sortData->listorder -1 :$sortData->listorder + 1;
$superiorId = $this->selectAll($config['sortId'],$sortInt);
$data = [];
if(!empty($superiorId)){
$transaction = Yii::app()->db->beginTransaction();
try {
$superiorId->listorder = $sortData->listorder;
$sortData->listorder = $sortInt;
if(!$sortData->save() || !$superiorId->save()){
throw new \yii\base\ErrorException('error');
}
$transaction->commit();
$title = '排序成功';
}catch (Exception $e){
$transaction->rollback();
$title = '排序失败';
}
$data['code'] = ($title== '排序成功') ? 200 : 201;
$data['msg'] = $title;
echo json_encode($data, true);
}else{
$parentId = TrainingCategory::model()->find('eid = :eid AND id = :id',[':eid'=>Yii::app()->user->eid,':id'=>$config['siblingsId']]);
if(($config['type'] == 'up' && $config['siblingsId'] == 'undefined') || ($config['type'] == 'down' && $config['siblingsId'] == 'undefined')){
$data['code'] = 400;
$data['msg'] = '排序失败';
$data['status'] = $config['type'] == 'up' ? 1 : 2;
}else if(($config['type'] == 'up' && $sortData->pid == $config['siblingsId']) || ($config['type'] == 'down' && $sortData->pid != $parentId->pid)){
$data['code'] = 500;
$data['msg'] = '排序失败';
}else{
$transaction = Yii::app()->db->beginTransaction();
try {
$sortData->listorder = $parentId->listorder;
$parentId->listorder = $sortInt;
if(!$parentId->save() || !$sortData->save()){
throw new \yii\base\ErrorException('error');
}
$transaction->commit();
$data['code'] = 200;
$data['msg'] = '成功';
}catch (Exception $e){
$transaction->rollback();
$title = '排序失败';
}
}
}
return $data;
}