What is considered the best practice for determining whether there are any rows bound?
Currently, I'm using the client-side OnDataBound event, and code similar to the following:
gridDataBound: function (event)
{
var rows = $('tbody tr:has(td)', this);
if (rows.length == 0 || (rows.length == 1 && rows[0].innerText == "No records to display'))
$('#GridSection').hide("slow");
}
There has got to be a better way!
I can suggest a shorter version:
if ($(this).find(".t-no-data").length) {
$("#GridSection").hide("slow");
}
Ah, a few minutes poking around and I think I have a solution that really feels better-
if ($("tbody tr:has(td).t-no-data", this).length != 0) {
$("#GridSection").hide("slow");
}
$('#grid-name').data('tGrid').data is an array of all of the records.
So, you can get the number of records using:
$('#grid-name').data('tGrid').data.length;
Related
I am having a complex object, and I am trying to loop through the objects and add to an another list.
But as I have few if statements inside the loop to check whether the object inside is null OR not, the iteration is taking lot of time. Also I am looping through around 70000 items.
Below is the code,
var Product = model; //complex object
Parallel.ForEach({model, product => {
if(product.Type != null)//type a
{ A = a.Loca;//do something }
if(product.Type != null)//type b
{ B = b.Loca;//do something }
if(product.Type != null)//type c
{ A = c.Loca;//do something }
dataAsset.Push(new assetItems(A, B, C));
}
});
I am trying to improve the performance.
Improve the performance by only checking if product.Type != null once. You do not need to check it three separate times. i.e.
Parallel.ForEach({model, product => {
if(product.Type != null)
{ a;//do something
b;//do something
c;//do something
}
dataAsset.Push(new assetItems(a, b, c));
}
By using Elvis operator I was able to improve the performance.
Using Spock 0.7 with Grails 2.04. Trying to set up a testing environment. I need some help in regards to testing a list of objects.
I have a list of location objects. I want to test a date on each of those objects. I am iterating over but not sure how to make the test fail if the dates are not equal. Is there a good way to test objects in a list? I have listed below my then block of code.
then:
weatherList != null
weatherList.empty != null
weatherList.size() == 3
weatherList.each {
Calendar today = Calendar.getInstance();
today.clearTime()
if(it.forecastDate != today) {
return false
}
}
A solution could look like this (comments inlined):
// avoid testing with real dates if possible
def today = Calendar.getInstance().clearTime()
when:
...
then:
weatherList != null
weatherList.size() == 3
// does this list really contain Calendar objects?
weatherList.every { it.forecastDate == today }
// OR, for a potentially better error message
weatherList.each { assert it.forecastDate == today }
in Flex I have something like that:
var dg:DataGrid = new DataGrid();
if (something) dg = dg1 else if (something_2) dg = dg2;
dg.dataProvider.getItemAt(3).id;
and dg is ALWAYS pointing at DataGrid (even if dg1 has name DataGrid_test and dg2 = DataGrid_test2) and finally action is made on my first DataGrid (DataGrid_test).
Why?
How can I pass dg1 or dg2 to dg?
Here is pasted almost full code of this part of application. I edited it to make that more clear.
var dg:DataGrid = null;
if ( currentState == "state1" ) { //if this condition is true then app. go into if and
dg = dataGrid_first; // make dg = DataGrid (1)
test.text = "inco"; // shows "inco" in "test" label
} else if ( currentState == "state2" ) { // if this is true then app. go..
dg = dataGrid_second; //here and set dg as DataGrid (exactly!) (2)
test.text = "outgo"; // and change test label into blank text (earlier text disapears)
}
search(dg);
It is modified with advice of '#splash'
Still not working.
EDIT:
I made this sceond edit to answer for all You who are helping me with that :) I think that it will be the best way. In codeblock above I added comments. (please read now comments and after that come back here :) )
Now I will explain exactly what happens.
I debug it many times and here are results:
dg is pointing at DataGrid (as component in flex, not as my dataGrid_first), I needed to extend DataGrid so now it is ColorColumn component (I don't know if I called it properly), not DataGrid. And dg is pointing at ColorColumn not at dataGrid_first or dataGrid_second. I even tried today the same thing what suggest #splash:
if ( currentState == "state1" ) {
test.text = "inco";
search(dataGrid_first);
} else if ( currentState == "state2" ) {
test.text = "outgo";
search(dataGrid_second);
}
and search still points at ColorColumn :/ My problem is really easy- I just want to pass to search different dataGrid on each state. If You have other ideas how I can do that in right way then I will pleased to hear about it. :)
But still I don't understand why it doesn't work. My search function uses algorhitm Boyer-Moor for searching through dataGrid.dataProvider for some text. If it find something then it is pushed into new array and after passing whole dataProvider I colorize rows with searched word.
If dg is never pointing to dg1 and dg2 then your (something) expressions may be evaluate to false. Check the value of your if-conditions - this should be easy to debug.
This should work:
var dg:DataGrid = null;
if (something)
dg = dg1;
else if (something_2)
dg = dg2;
if (dg)
{
// do something with dg
}
[Update]
I still can't see why your code isn't working, but you could simplify it like this:
if ( currentState == "state1" ) {
test.text = "inco";
search(dataGrid_first);
} else if ( currentState == "state2" ) {
test.text = "outgo";
search(dataGrid_second);
}
I'd propose to write this - since I guess either dg1 or dg2 should be assigned:
if (something) {
dg = dg1;
} else {
dg = dg2;
}
There may be cases, where if () {} else () {} neither executes the first or the second conditional block.
Finally a small hint, which structurally eliminates unwanted assignments in if conditions: Always write the literal left of the comparison operation: if ( "state1" == currentState ). If you accidentally typed = instead of ==, the flex compiler emits an error. The other notation silently assigns a value.
Additionally: Did you single-stepped through your code and watched the variables dg1, dg2 and dg? If not, set a breakpoint a few line before the if-statement and run the code step by step from there on. What do you see?
Here's a another tip: Use assertions to check for inconistencies:
package my.company.utilities {
public function assert(expression:Boolean):void {
// probably conditionally compile this statement
if (!expression) {
throw new Error("Assertion failed!");
}
} // assert
}
Use it e.g. at the beginning of a method like this:
public function doTransaction( fromAccount:int, toAccount:int ) {
assert( 0 < fromAccount );
assert( 0 < toAccount );
}
A typically good use of assert is to check variables regarding their range. As of the above example, fromAccount and toAccount should always be positive. Due to a bug, bad values might get passed to doTransaction(). In this case, the assertion fires an error.
How can I break out of an if statement?
Exit only works for "for", "sub", etc.
In VB.net:
if i > 0 then
do stuff here!
end if
In C#:
if (i > 0)
{
do stuff here!
}
You can't 'break out' of an if statement. If you are attempting this, your logic is wrong and you are approaching it from the wrong angle.
An example of what you are trying to achieve would help clarify, but I suspect you are structuring it incorrectly.
There isn't such an equivalent but you should't really need to with an If statement. You might want to look into using Select Case (VB) or Switch (C#) statements.
In C# .NET:
if (x > y)
{
if (x > z)
{
return;
}
Console.Writeline("cool");
}
Or you could use the goto statement.
You can use
bool result = false;
if (i < 10)
{
if (i == 7)
{
result = true;
break;
}
}
return result;
I have to admit, that in some cases you really wanna have something like an exit sub or a break. On a rare occasion is I use "Goto End" and jump over the "End If" with the def. End:
I know this is an old post but I have been looking for the same answer then eventually I figured it out
try{
if (i > 0) // the outer if condition
{
Console.WriteLine("Will work everytime");
if (i == 10)//inner if condition.when its true it will break out of the outer if condition
{
throw new Exception();
}
Console.WriteLine("Will only work when the inner if is not true");
}
}
catch (Exception ex)
{
// you can add something if you want
}
`
I am using a Flex dataGrid, and need to sort some of my columns numerically.
Looking at the sortCompareFunction, it seems like i need to create a different function for each column that i want to sort, because my sort function has to know what field it is sorting on.
Is there any way that I can pass the field to be sorted on into the function? so that I only need one numeric sorting function.
I did it using this function:
function fieldNumericSorter(field:String):Function {
return function (obj1:Object, obj2:Object):int {
return sign( int(obj1[field]) - int(obj2[field]) );
}
}
and for each column that needed sorting set
colToBeSorted.sortCompareFunction = fieldNumericSorter("fieldname");
If you are using a DataGrid with an XML dataProvider, this worked for me (modified from Alex's answer):
private function xmlDataGridNumericSorter(field:String):Function
{
return function (obj1:Object, obj2:Object):int
{
var num:Number = ((Number)(obj1.attribute(field)) - (Number)(obj2.attribute(field)));
return (num > 0) ? 1 : ((num < 0) ? -1 : 0);
}
}
and
dataGridColumn.sortCompareFunction = xmlDataGridNumericSorter(xmlAttribute.name().toString());
A very nice solution, considering how common a procedure this probably is..
Thanks Alex, hope this helps people further.
I did not use a numeric function to sort - instead did the following:
arrayCollObject.addItem({Col1: rowData[0], Col2: parseFloat(rowData[1])});
This seems to do the sorting correctly.