I am retrieving data from JSON, but whole of the Data are displayed on my page,
but i want to display it page wise, for eg. 1 to 10 on first page,
and as the user presses the button, rest of the record are showed, i was working
on that, but could not solve it, Is there are any concept of Paging in Corona,
Or please provide help regarding this issue, thanks..
Here is my json data structure:
My code for displaying JSON data:
local test=json.decode(event.response)
local datas=test.data
for name, users in pairs(datas) do
for names, usernames in pairs(users ) do
for tag,value in pairs(usernames) do
cid=value.customerid
cname=value.customername
print(cname)
print(cid)
end
end
end
this is my json data which is to be displayed page wise
{
"status":"success",
"data":{
"marks":[
{
"Marks":{
"first_name":"Amit",
"last_name":"Sharma",
"country_id":"20",
"Physics":"50",
"Chemistry":"35",
"Mathematics":"40"
}
},
{
"Marks":{
"first_name":"Amit",
"last_name":"Yadav",
"country_id":"21",
"Physics":"50",
"Chemistry":"35",
"Mathematics":"40"
}
},
{
"Marks":{
"first_name":"Pankaj",
"last_name":"Shukla",
"country_id":"22",
"Physics":"50",
"Chemistry":"35",
"Mathematics":"40"
}
},
{
"Marks":{
"first_name":"Abhishek",
"last_name":"Tiwari",
"country_id":"25",
"Physics":"50",
"Chemistry":"35",
"Mathematics":"40"
}
},
{
"Marks":{
"first_name":"Kashif",
"last_name":"Khan",
"country_id":"20",
"Physics":"50",
"Chemistry":"35",
"Mathematics":"40"
}
},
{
"Marks":{
"first_name":"Ankit",
"last_name":"Sharma",
"country_id":"19",
"Physics":"50",
"Chemistry":"35",
"Mathematics":"40"
}
},
{
"Marks":{
"first_name":"Rahul",
"last_name":"Vishwakarma",
"country_id":"27",
"Physics":"50",
"Chemistry":"35",
"Mathematics":"40"
}
},
{
"Marks":{
"first_name":"Amit",
"last_name":"Tiwari",
"country_id":"30",
"Physics":"50",
"Chemistry":"35",
"Mathematics":"40"
}
},
{
"Marks":{
"first_name":"Amit",
"last_name":"Sharma",
"country_id":"78",
"Physics":"50",
"Chemistry":"35",
"Mathematics":"40"
}
},
{
"Marks":{
"first_name":"Amit",
"last_name":"Sharma",
"country_id":"23",
"Physics":"50",
"Chemistry":"35",
"Mathematics":"40"
}
}
]
}
}
here's a hint on how you can get your JSON data i just get the first and last name here i just copy your json data and put it on a textfile name data
local json = require "json"
local txt
local path = system.pathForFile( "data.txt", system.ResourceDirectory )
data = {}
local file = io.open( path, "r" )
if file then
-- read all contents of file into a string
txt = file:read( "*a" )
io.close( file ) -- close the file after using it
end
local t = json.decode (txt)
for i = 1, table.getn(t["data"]["marks"]), 1 do
print(t["data"]["marks"][i]["Marks"]["first_name"])
print(t["data"]["marks"][i]["Marks"]["last_name"])
end
Your sample code above is just dumping prints to the console. I assume that in the end you want to actually display it on the screen. I would recommend using a widget.newTableView() and let the system handle the scrolling for you.
http://docs.coronalabs.com/api/library/widget/newTableView.html
Related
I am working on an app for iOS and Android, and I am using Cordovas File plugin to save some data to a file on the device.
This file will contain information about the user, so he/she does not have to log in every time they use the app, so the information has to be there even if the app is exited and relaunched.
On Android this works fine:
window.requestFileSystem(LocalFileSystem.PERSISTENT, 20*1024*1024, app.onFileSystemSuccess, fail);
On Android the data I save is there next time I open the app, but on iOS the file is empty (or not existing).
Does this not work for iOS?
Here is the whole source:
var app = {
initialize: function()
{
this.bindEvents();
},
bindEvents: function()
{
document.addEventListener('deviceready', this.onDeviceReady, false);
},
onDeviceReady: function()
{
app.initAPP();
},
initAPP: function()
{
window.requestFileSystem(LocalFileSystem.PERSISTENT, 20*1024*1024, app.onFileSystemSuccess, fail);
},
onFileSystemSuccess: function(FS)
{
FS.root.getDirectory('MyAPPDir', {create:true}, app.gotDir, fail);
},
gotDir: function(Dir)
{
app.appRoot = Dir;
try{
app.appRoot.getFile('authInfo.txt', {create:true}, function(fileEntry){
fileEntry.file(function(file){
alert('Got file authInfo.txt')
var Reader = new FileReader();
Reader.onloadend = function(e){
alert(e.target.result);
if( e.target.result == '' ){
$('#container').load('login.html');
}
else{
document.authInfo = e.target.result;
alert(e.target.result);
alert('You are now auhtorized');
}
}
Reader.readAsText(file);
})
})
} catch(e){
$('#container').load('login.html');
}
},
authUser: function()
{
email = $("#email").val();
password = $("#password").val();
$.ajax({
url: AUTH_URL,
type: 'POST',
data:{
email: email,
password: password
},
success: function(data){
resp = $.parseJSON(data);
if( resp.status == 'success' ){
try{
app.appRoot.getFile('authInfo.txt', {create:true}, function(fileEntry){
fileEntry.createWriter(function(writer){
writer.truncate(0);
writer.onwriteend = function(){
writer.write(resp);
writer.onwriteend = function(){
alert('Wrote '+JSON.stringify(resp)+' to '+fileEntry.toURL());
}
}
});
});
}
catch (e){
alert(e);
}
} else {
alert(resp.message);
}
}
});
}
};
function fail(a){
alert(a);
}
I get this message in iOS:
Wrote {.......myjson.....} to cdvfile://localhost/persistent/MyAPPDir/authInfo.txt
But then when it tries to read it on launch it seems like the file does'nt exist any more, or is empty?
I am trying to build a chart from JS that has multiple "y:" data points in multiple series. I have got the chart working fine using a static configuration, but am struggling to get it to work when adding the points via the API.
I am trying to acheive:
'series':[
{
'name':'Series 1',
'color':'#FF8500',
'data':[
{
'y':1.0002193448599428
},
{
'y':0.4999027241865406
},
{
'y':4.499986649534549
},
{
'y':0.4998817439627601
}
]
},
{
'name':'Series 2',
'color':'#66B3FF',
'data':[
{
'y':12.99999999901047
},
{
'y':13.00000000082946
},
{
'y':18.99999999841384
},
{
'y':5.000000001018634
}
]
},
My code so far looks like this:
var options = {
'chart':{
'renderTo':'MyChart',
'zoomType':'xy',
'defaultSeriesType':'bar',
'width':880
},
<snip>
...
</snip>
'yAxis':{
'title':{
'text':'Seconds'
},
},
'xAxis':{
'title':{
'text':'Objects'
},
'categories': [],
'opposite':true
},
'series':[
{
'name':'Series 1',
'color':'#FF8500',
'data':[]
},
'series':[
{
'name':'Series 2',
'color':'#66B3FF',
'data':[]
}
]
};
options.chart.renderTo = targetdiv;
//Categories add perfectly here:
$.each(harfile.log.entries, function(i, val) {
options.xAxis.categories.push(val.request.url);
console.log(val.request.url);
});
$.each(harfile.log.entries, function(i, val) {
//need to do some logic to catch "-1" for all these values...
options.series[0].data.push("y:" + val.timings.receive);
});
$.each(harfile.log.entries, function(i, val) {
//need to do some logic to catch "-1" for all these values...
options.series[1].data.push(y:" + val.timings.receive);
});
console.log(options.series[0].data);
This produces a "TypeError: Cannot call method 'push' of undefined"
Just use Highcharts API's series.addPoint().
I'm using carrier wave for image upload and trying to do upload
image through json,but its not going to save value into database
.While image name is coming in controller correct here
#Employee_events.external_doc=params[:external_doc] .But its not
going to set in this #Employee_events.external_doc variable , as i
thing may be issue will be multipart. But d'not know how to use in
json.
.js file
$(document).ready(function() {
var evnt = {};
$("#Event_dialog-form").dialog({
autoOpen : false,
height : 400,
width : 600,
modal : true,
buttons : {
"Enter" : function() {
evnt.external_doc = $('#imgid').val();
$.post("/employee_management/add_event", evnt, function(data) {
if (data) {
alert("Data entered successfully");
} else {
alert("Oops! some error occured");
}
});
return false;
},
Cancel : function() {
$(this).dialog("close");
}
},
close : function() {
//allFields.val( "" ).removeClass( "ui-state-error" );
}
});
$(".EventEntryDialog").click(function() {
var Ed = $(this).attr("id");
$('#user_id').val(Ed);
$("#Event_dialog-form").dialog("open");
});
});
Controller
def add_event
result=TRUE
#Employee_events=EmployeeEvent.new
#Employee_events.external_doc= params[:external_doc]
puts #Employee_events.external_doc.to_s
respond_to do |format|
if #Employee_events.save
format.json { render :json => result }
else
return FALSE
end
end
end
When I am searching my ElasticSearch documents using a nested filter -> and -> geo_distance I retrieve documents which are too far away (and I don't want returned.) You can see the query and a screenshot below of the results (raw results on the left and manually filtered results on the right).
Here's another copy of the query:
{
"query":{
"match_all":{
}
},
"filter":{
"and":[
{
"term":{
"PropertySubType":"Single Family"
}
},
{
"term":{
"City":"Los Angeles"
}
},
{
"geo_distance":{
"distance":"2.25miles",
"Location":[
34.111583657,
-118.324646099
]
}
},
{
"range":{
"BedroomsTotal":{
"gte":3
}
}
},
{
"range":{
"BuildingSize":{
"gte":3000
}
}
},
{
"range":{
"YearBuilt":{
"lte":2000
}
}
},
{
"terms":{
"ListingStatus":[
"Active",
"Pending",
"Closed"
]
}
}
]
},
"size":100
}
Adding the option "distance_type" and setting it to "plane" fixed this issue. See "distance_type" here:
http://www.elasticsearch.org/guide/reference/query-dsl/geo-distance-filter.html
In the edit Form is a select dropdown. When the user select an item, i want to load some values and fill them into the form.
My code so far:
var grid = $("#list").jqGrid({
parameters...,
colNames:[...],
colModel :[
...
]
});
$("#list").jqGrid(
'navGrid',
'#pager',
{
view:true,
edit:true,
del:true,
search:false,
},
/* EDIT */
{
closeAfterEdit: true,
afterSubmit: processAddEdit,
onInitializeForm: setFormEvents,
...
}
...
);
function setFormEvents(formid) {
/* It sometim works when using timeout..
* It seems to be a timing issue.
* But i have no idea why and how to solve
*/
setTimeout ( function(){
$('select#data_id', formid).unbind();
$('select#data_id', formid).change(function() {
$.getJSON("/URL?dataid=" + $('select#data_id option:selected').val(),
function(data){
$.each(data, function(i,item){
if (item.field == "anrede") { $("#anrede").val(item.value); }
else if (item.field == "titel") { $("#titel").val(item.value); }
else if (item.field == "vorname") { $("#vorname").val(item.value); }
else if (item.field == "nachname") { $("#nachname").val(item.value); }
else if (item.field == "firma") { $("#firma").val(item.value); }
else if (item.field == "strasse") { $("#strasse").val(item.value); }
else if (item.field == "hausnummer") { $("#hausnummer").val(item.value); }
else if (item.field == "plz") { $("#plz").val(item.value); }
else if (item.field == "ort") { $("#ort").val(item.value); }
else if (item.field == "land") { $("#land").val(item.value); }
});
});
});
}, 1000 );
}
To bind event (like change event in your case) to the edit field you should use dataEvents of the editoptions. See here, here or here examples. Moreover I recommend you to use recreateForm:true option additionally.