Dynamic Column plus IF statement
Dynamic Column plus IF statement
I have a datatable which works perfectly.
I am trying it add in an IF statement without any luck.
Part of my current code is this:
[code]
function search(field,searchTxt) {
db.transaction(function (tx) {
try{
tx.executeSql("SELECT * FROM books where "+field+" like ?", ['%'+searchTxt+'%'], function (tx, results) {
var html = [];
len = results.rows.length;
for (var i = 0; i < len; i++) {
var row=results.rows.item(i);
var a=[];
a[0]=row.author;
a[1]=row.title;
a[2]=row.file;
html.push( a);
}
for(i=data.length-1;i>0;--i)
data.pop();
data[0]=html[0];
try{
$('#example').html("");
}catch(e){}
$('#example').dataTable({
"bJQueryUI": true,
"sPaginationType": "full_numbers",
"bDestroy":true,
"aaData": html,
"bFilter": false,
"aoColumns": [
{ "sTitle": "Author", "sWidth": "33%" },
{ "sTitle": "Title", "sWidth": "47%" },
{
"bSort": false,
"sTitle": "Link",
"sWidth": "20%",
"sClass": "center",
"fnRender": function(obj) {
var sReturn = obj.aData[ obj.iDataColumn ];
sReturn = 'Get book';
return sReturn;
}
}
]
} );
});
}catch(e){
alert(e);
}
});
}
[/code]
My WEBSQL has 4 cols: title, author, file, ind
What I'd like to do for the last part of my function which creates the link:
[code]
"fnRender": function(obj) {
var sReturn = obj.aData[ obj.iDataColumn ];
sReturn = 'Get book';
return sReturn;
}
[/code]
Is, in laymans terms say:
If ind = 1 then sReturn = 'Get book';
else
sReturn = 'NEW book';
Where ind is the column value in my WEBSQL
Any help would be most appreciated!
I am trying it add in an IF statement without any luck.
Part of my current code is this:
[code]
function search(field,searchTxt) {
db.transaction(function (tx) {
try{
tx.executeSql("SELECT * FROM books where "+field+" like ?", ['%'+searchTxt+'%'], function (tx, results) {
var html = [];
len = results.rows.length;
for (var i = 0; i < len; i++) {
var row=results.rows.item(i);
var a=[];
a[0]=row.author;
a[1]=row.title;
a[2]=row.file;
html.push( a);
}
for(i=data.length-1;i>0;--i)
data.pop();
data[0]=html[0];
try{
$('#example').html("");
}catch(e){}
$('#example').dataTable({
"bJQueryUI": true,
"sPaginationType": "full_numbers",
"bDestroy":true,
"aaData": html,
"bFilter": false,
"aoColumns": [
{ "sTitle": "Author", "sWidth": "33%" },
{ "sTitle": "Title", "sWidth": "47%" },
{
"bSort": false,
"sTitle": "Link",
"sWidth": "20%",
"sClass": "center",
"fnRender": function(obj) {
var sReturn = obj.aData[ obj.iDataColumn ];
sReturn = 'Get book';
return sReturn;
}
}
]
} );
});
}catch(e){
alert(e);
}
});
}
[/code]
My WEBSQL has 4 cols: title, author, file, ind
What I'd like to do for the last part of my function which creates the link:
[code]
"fnRender": function(obj) {
var sReturn = obj.aData[ obj.iDataColumn ];
sReturn = 'Get book';
return sReturn;
}
[/code]
Is, in laymans terms say:
If ind = 1 then sReturn = 'Get book';
else
sReturn = 'NEW book';
Where ind is the column value in my WEBSQL
Any help would be most appreciated!
This discussion has been closed.
Replies
If anyone had the same similar issues this is what i did:
[code]
{
"bSort": false,
"sTitle": "Link",
"sWidth": "20%",
"sClass": "center",
"fnRender": function (obj) {
var sReturn = obj.aData[obj.iDataColumn];
var sInd = obj.aData[obj.iDataColumn + 1];
if (sInd == 1) {
sReturn = 'Get book';
return sReturn;
}
else {
sReturn = 'New book';
return sReturn;
}
}
[/code]
After a lot of googling i found somewhere that someone had done:
[code]
obj.aData[obj.iDataColumn + 1];
[/code]
Thought I'd test this just in case and seems to have worked.
By doing this i am able to reference other columns in the datatable and build up some logic behind that..