2014-12-29 1 views
0

나는이 ajax 호출에 붙어 있습니다. 내가 뭘 하려던 건 내 프로젝트의 django 뷰 파일에서 ajax 호출 (return_data, item_list라고도 함)에서 반환 된 데이터에서 데이터를 가져 오는 것이 었습니다. 항목은 내가 입력 한 장고 모델 모듈의 코드로 인해 알파벳순으로 가져옵니다. Django를 처음 접했을 때 내 문제는 "show more button"을 클릭하고 return_data/item_list 목록의 다음 3 개 항목을주고 페이지에로드하는 아약스 호출을 얻는 것입니다. 누군가 이렇게 할 수있는 방법을 무너 뜨릴 수 있습니까? 미리 감사드립니다. 이 같은Django 프로젝트의 AJAX 호출 : 목록에서 다음 항목 3 개로드 중

$(document).ready(function() { 
    var config = { 
     type: 'GET', 
     url: SUBMIT_URL, //written in script tags in template , var SUBMIT_URL = "{% //url 'get_next_concepts' %}" 
     data: { 

     }, 
     dataType: 'html', 
     success: function (return_data, textStatus_ignored, jqXHR_ignored) { 
      alert("The AJAX call worked!" + return_data); //Just checks to make sure  
//data I need is returned 
     } 
    }; 
    $("#showmore").click(function() { 
     $.ajax(config); 
     $("table").append("<tr></tr>"); 
     for (var i = 0; i < 3; i++) { 
      var itemdescription = return_data.index(i).description; 
      var icon = return_data.index(i).icon; 
      var itemName = return_data.index(i).icon; 
      $("table tr:last").append(
        generateCard(itemName, 
          itemdescription, 
          icon)); 
     } 
    }); 

function generateCard(itemNameC, descriptionC, iconC) {  // Function to make card 
    //containing item information (name, icon and description 
var card = "<td class='tablecells'><a class='tabletext' href='#'><span class='fa " 
     + iconC 
     + " concepticons'></span><h2 class='header'>" 
     + itemNameC 
     + "</h2><p>" 
     + descriptionC 
     + "<span class='fa fa-chevron-circle-right'></span></p></a></td>"; 
return card; 
} 

답변

0

아마 뭔가 :

var indexLast = 0; 
$("#showmore").click(function() { 
    $.ajax({ 
     type: 'GET', 
     url: SUBMIT_URL, 
     data: { 
      indexStart: indexLast //you'll need you backend to take this index and get the next 3 items from the list starting at this index count, so first time, get first 3, second call you would need to get 3-5, 6-9, etc 
     }, 
     dataType: 'html', 
     success: function (return_data, textStatus_ignored, jqXHR_ignored) { 
      indexLast += 3; //add 3 to the index 
      // Now Use the Data here, or save to a variable and use elsewhere. 
      // if your sever simply returns the table formated data already you can just call your jQuery.append(return_data) and your done. 
     } 
    }); 

}); 
+0

신난다! 나는 너가 이것으로 어디로 가는지에 관해 안다. 마지막 질문 중 하나는 이전에 서버에 데이터를 보내지 않았기 때문에 백엔드에서 indexLast를 어떻게 참조 할 수 있습니까? view.py 파일에서 코드를 편집하려고하는데 가변 오류가 발생합니다. – star

+0

python/django 이렇게 할 수 있습니다 : request.GET.get ('indexLast') try : int (request.GET.get ('indexLast') except : 블록으로 사용하기 전에 차단해야합니다. 변수에서 모델 쿼리. –

+0

감사합니다! – star

관련 문제