简体   繁体   English

jQuery Ajax,在列表之间移动列表项

[英]jQuery Ajax, moving list items between lists

I'm not sure if returning html is the way to go here but I'm not experienced at all with JSON and maybe that's the answer to my problem. 我不确定返回html是否可以去这里,但我对JSON没有任何经验,也许这就是我的问题的答案。

I have two ul lists with items, they all have a select in them and the first list contains all the items with the selected option equal to cero, the other list has the rest: 我有两个包含项目的ul列表,它们都有一个select,第一个列表包含所选项目等于cero的所有项目,另一个列表包含其余项目:

<ul id="invlistsinmesa">
  <li id="inv-56">
      <label>Name 1</label>
        <select>
            <option  value="1">Mesa 1</option>
            <option  value="2">Mesa 2</option>
            <option  value="3">Mesa 3</option>
                    <!-- ... -->                               
            <option selected="selected" value="0">Sin mesa</option>
        </select>
       <div class="delete_box"><a href="#invlistsinmesa" class="close">Delete</a></div>                  
 </li><li id="inv-57">
      <label>Name 2</label>
        <select>
            <option  value="1">Mesa 1</option>
            <option  value="2">Mesa 2</option>
            <option  value="3">Mesa 3</option>
                    <!-- ... -->                               
            <option selected="selected" value="0">Sin mesa</option>
        </select>
       <div class="delete_box"><a href="#invlistsinmesa" class="close">Delete</a></div>                  
 </li>
</ul>
<ul id="invlist">
  <li id="inv-36">
      <label>Name 1</label>
        <select>
            <option  value="1">Mesa 1</option>
            <option  value="2">Mesa 2</option>
            <option selected="selected" value="3">Mesa 3</option>
                    <!-- ... -->                               
            <option value="0">Sin mesa</option>
        </select>
       <div class="delete_box"><a href="#invlistsinmesa" class="close">Delete</a></div>                  
 </li><li id="inv-37">
      <label>Name 2</label>
        <select>
            <option selected="selected" value="1">Mesa 1</option>
            <option value="2">Mesa 2</option>
            <option value="3">Mesa 3</option>
                    <!-- ... -->                               
            <option value="0">Sin mesa</option>
        </select>
       <div class="delete_box"><a href="#invlistsinmesa" class="close">Delete</a></div>                  
 </li>
</ul>

As you see there's a lot of code and it all comes from a database. 如您所见,有很多代码,它们都来自数据库。 I have a few ajax calls going on here (deleting, adding new items) but the one I have a problem with is when I change the selected option. 我在这里进行了一些ajax调用(删除,添加新项目),但我遇到问题的是当我更改所选选项时。 This is my code now (on change of the select): 这是我的代码(更改选择时):

EDIT: Added the click function (it's not change because I'm using jqTransform to style the selects but it's the same as using change with a regular select) 编辑:添加了点击功能(它没有改变,因为我使用jqTransform来设置选项的样式,但它与使用常规选择的更改相同)

$('li[id^="inv-"] div.jqTransformSelectWrapper ul li a').on('click', function(){
  var item=($(this).closest("li[id^='inv-']")).attr('id');
  var id = parseInt(item.replace("inv-", ""));
  var nummesa= ($(this).closest('li')).attr('class');

$.ajax({
  type: "POST",
  url: "ajax-invi.php",
  dataType: "html",
  data: "id="+id,
  success: function(data) {
    if(data>=0){
      if(data!=0){
        noty({"text":"Person has been moved"});
      }else{
        //Here I need to control if the list item needs to 
        //change to the other list or not
      }          
    }else{
      if(data=='-1'){
        noty({"text":"Table is full, person cannot move there","type":"error"});
      }
    }
  }
 }); 
});

In my ajax-invi.php file I query de database to see if the person can move to the new selected option (basically you put people in tables, you can't place a person in a full table and that needs to be controlled) and I return -1 if the table is full or 0 if it isn't, but if I need to move it then I have a problem because I'm not returning the html that I need to move. 在我的ajax-invi.php文件中,我查询数据库以查看该人是否可以移动到新选择的选项(基本上你将人放在表中,你不能将一个人放在一个完整的表中并且需要控制)如果表已满,我返回-1,如果不满,则返回0,但如果我需要移动它,那么我有一个问题,因为我没有返回我需要移动的html。

I'm making some assumptions about what you want here, but assuming you want to move the LI from the invlistsinmesa to invlist here is the code you want for moving the data. 我在这里做了一些你想要的假设,但是假设你想把LI从invlistsinmesa移到invlist这里是你想要移动数据的代码。 You don't need any html returned from your call because the HTML is already present in your document. 您不需要从调用返回任何html,因为HTML已存在于您的文档中。 Assuming the id in your .ajax call is the LI id, such as inv-56 then here is the code: 假设.ajax调用中的id是LI id,例如inv-56,那么这里是代码:

var $item = $('#' + id);
$('#invlist').append('<li id=' + id + '>' + $item.html() + '</li>');
$item.remove();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM