2016-08-01 3 views
1

У меня есть список выбора, который заполняется моим файлом журнала. Каждый второй javascript отправляет запрос GET серверу, который считывает файл журнала и заполняет список. Но после каждого запроса GET список прокручивается вверх. Я хочу, чтобы запросы не влияли на прокрутку, поэтому я могу свободно прокручивать список.Поддержание позиции прокрутки в списке

<select id = "list" name=servers size=38 style=width:1028px> 

<script type="text/javascript"> 
window.onload = function() { 

    if (bytes === undefined) { 
     var bytes=0; 
    } 
    var url = "/test/log.php?q="; 
    function httpGet(url) 
    { 
    var xhttp = new XMLHttpRequest(); 
    xhttp.open("GET", url, true); 
    xhttp.onload = function (e) { 
     if (xhttp.readyState === 4) { 
      if (xhttp.status === 200) { 
       var list = ""; 
       console.log(xhttp.responseText); 
       obj = JSON.parse(xhttp.responseText); 
       for(var key in obj) { 
       list += obj[key]; 
       if (sessionStorage.logfile == null) { 
        sessionStorage.logfile == "Log"; 
       } 

      } 
       bytes = parseInt(list); 
       document.getElementById("list").innerHTML = sessionStorage.logfile + list; 
       sessionStorage.logfile += list; 
      } 
     }; 
     xhttp.onerror = function (e) { 
     console.error(xhttp.statusText); 
     } 
    }; 


    xhttp.send(); 
    } 

    var updateInterval = 1000; 
    function update() { 

    httpGet(url + bytes); 
     setTimeout(update, updateInterval); 
    } 

    update(); 
} 
</script> 
+0

Получить выбранный ключ всегда при обновлении списка и установить .опции [ключ] .выбран после обновления. –

+0

@GovindSamrow Можете ли вы уточнить? – Mirakurun

+0

вы заменяете весь список, чтобы сохранить свою позицию прокрутки, вы должны обновить значение элементов в списке. Можете ли вы опубликовать типичный ответ ajax, чтобы мы могли видеть формат, с которым вы работаете? – Trey

ответ

1

Может быть, вы должны использовать SSE, проверить это: http://www.w3schools.com/html/html5_serversentevents.asp, но если вам просто нужно, чтобы сделать код работает, вот как:

<select id = "list" name=servers size=38 style=width:1028px> 

<script type="text/javascript"> 

//here, a new global var to keep the index; 
var old_index=-1; 


window.onload = function() { 
//every change on select list, we register in this function.. 
document.getElementById("list").onchange = keepValue; 



    if (bytes === undefined) { 
     var bytes=0; 
    } 
var url = "/test/log.php?q="; 
function httpGet(url) 
{ 
    var xhttp = new XMLHttpRequest(); 
    xhttp.open("GET", url, true); 
    xhttp.onload = function (e) { 
     if (xhttp.readyState === 4) { 
      if (xhttp.status === 200) { 
       var list = ""; 
       console.log(xhttp.responseText); 
       obj = JSON.parse(xhttp.responseText); 
       for(var key in obj) { 
       list += obj[key]; 
       if (sessionStorage.logfile == null) { 
        sessionStorage.logfile == "Log"; 
       } 

      } 
      bytes = parseInt(list); 
      document.getElementById("list").innerHTML = sessionStorage.logfile + list; 
      sessionStorage.logfile += list; 
      //here, back it to the old selected index 
      //when old_index=-1, means first execution 
      if (old_index==-1) 
      {old_index = document.getElementById("list").length-1;} 
      document.getElementById("list").selectedIndex = old_index; 
      } 
     }; 
     xhttp.onerror = function (e) { 
     console.error(xhttp.statusText); 
    } 
    }; 


xhttp.send(); 
} 

var updateInterval = 1000; 
function update() { 
    httpGet(url + bytes); 
    //i will not change your logic here, but you can write it using setInterval instead. 
    setTimeout(update, updateInterval); 
} 

update(); 
} 

//here, the function to register the list index 
function keepValue(evt) 
{ 

old_index = evt.target.selectedIndex; 
//or document.getElementById('list').selectedIndex; 

} 

</script> 

EDIT:

ResponseText в JSON.

{"key":"2186 <option> 18:42:19.716 7963  [DEBUG main() cnet.cpp:167] Using public ip: 192.168.0.107 </option> 
<option> 18:42:19.716 7963  [DEBUG main() cnet.cpp:168] Using local ip: 192.168.0.107 </option> 
<option> 18:42:19.717 7963  [DEBUG init() redis.cpp:75] Initializing redis client application </option>"} 
+0

Нет, к сожалению, это не работает. – Mirakurun

+0

Чтобы помочь вам решить проблему, не могли бы вы отредактировать вопрос и добавить responseText? – danielarend

+0

Хорошо Я отредактировал – Mirakurun

Смежные вопросы