Changing the order of Listview randomly on page refresh
First,Add this script files in Head Tag
Next,Add the Basic html code to display the listview items
Now, Use the following script to bind items in listview.Page Title
Page Footer
$('#Itemslistpage').live('pageshow', function (event) { var serviceURL = 'Service.asmx/GetList'; $.ajax({ type: "POST", url: serviceURL, data: param = "", contentType: "application/json; charset=utf-8", dataType: "json", success: successFunc, error: errorFunc }); function successFunc(data, status) { // parse it as object var ItemsListArray = JSON.parse(data.d); var ArrayRank = new Array(); // creating html string var listString = '
- ';
var k = 0;
// running a loop
$.each(ItemsListArray, function (index, value) {
ArrayRank[k] = k;
k++;
});
random(ArrayRank);
$.each(ItemsListArray, function (index, value) {
listString += '
- ' + ItemsListArray[ArrayRank[index]].Desgination + '
' + ItemsListArray[ArrayRank[index]].Name + ' ' + ItemsListArray[ArrayRank[index]].Rank + '
';
});
listString += '
function random(list) { var i, j, t; for (i = 1; i < list.length; i++) { j = Math.floor(Math.random() * (1 + i)); if (j != i) { t = list[i]; list[i] = list[j]; list[j] = t; } } }That's it.Now The listview items order will change on every time page refresh.See the below output Here is the output
Labels: javascript, listview, random, tricks
<< Home