Search inside a DataGrid - actionscript-3

I have been fighting with some code I wrote in the past and trying to tweak it. Currently my search only searches for a matching first character of a word. I need to search more in depth. For instance if I search for planet in this string:
"Earth is a planet"
I get nothing, but if I search for planet in this next string:
"Planet earth is amazing"
I get the data grid to show just that line.
I want to be able to search for a word the shows the line regardless of whether its in the beginning of a string or end of one.
Can someone help me out? Thanks in advance.
import fl.controls.DataGrid;
import fl.controls.TextInput;
import fl.controls.dataGridClasses.DataGridColumn;
import fl.data.DataProvider;
var voddb:DataProvider = new DataProvider();
voddb.addItem({title:"Earth is a planet", detail:"ALE 0110"});
voddb.addItem({title:"Planet Earth", detail:"ALE 0210"});
voddb.addItem({title:"Jupiter is a planet", detail:"ALE 0310"});
voddb.addItem({title:"Aplanet Jupiter", detail:"ALE 0410"});
voddb.addItem({title:"Another amazing planet is mars", detail:"ALE 0510"});
voddb.addItem({title:"Planets include earth and mars:", detail: "ALE 0610"});
vodTextInput.addEventListener(Event.CHANGE, changeHandler);
var titleCol:DataGridColumn = new DataGridColumn("title");
titleCol.headerText = "Title";
var detailCol:DataGridColumn = new DataGridColumn("detail");
detailCol.headerText = "Details";
detailCol.sortOptions = Array.DESCENDING;
vodDataGrid.addColumn(titleCol);
vodDataGrid.addColumn(detailCol);
vodDataGrid.dataProvider = voddb;
function changeHandler(event : Event) : void
{
var arr : Array = voddb.toArray();
var filteredArr : Array = arr.filter(filterDataProvider);
vodDataGrid.dataProvider = new DataProvider(filteredArr);
}
function filterDataProvider(obj : Object, idx : String, arr : Array) : Boolean
{
var txt1 : String = vodTextInput.text;
var txt2 : String = obj.title.substr(0, txt1.length);
if (txt1.toLowerCase() == txt2.toLowerCase())
{
return true;
}
return false;
}

You can use indexOf to search for the string.
function filterDataProvider(obj : Object, idx : int, arr : Array) : Boolean
{
var txt1 : String = vodTextInput.text.toLowerCase();
var txt2 : String = obj.title.toLowerCase();
return txt2.indexOf(txt1) >= 0;
}

Related

Kotlin - How to make a for loop that iterate and return multiple values

I created a function that iterates by a if statement over a list in order to find a match, when found I wanted to return the match value, but it only happen once, the return statements are at the end of the function and the if statement.
The question is, How can I avoid that this function stops after the first match?, is there another way?, other functions that im not using?
When i run this code I get this:
Anything
Not a match
Not a match
Here is my code:
class Class1(var self: String,var tipo: String,var element: String)
var test_class = Class1("","","")
fun giver(){
test_class.self = "Anything"
test_class.tipo = "Something"
test_class.element = "Nothing"
}
class Funciones(){
fun match_finder(texto: String): Any{
var lista = listOf<String>(test_class.self,test_class.tipo,test_class.element)
var lista_de_listas = listOf<String>("test_class.self","test_class.tipo","test_class.element")
var count = -1
var variable = ""
for (i in lista_de_listas){
count = count + 1
println(count)
if (texto == i){
lista_de_listas = lista
var variable = lista_de_listas[count]
return variable
}
}
return "Not a match"
}
}
fun main(){
giver()
var x = "test_class.self"
var z = "test.class.tipo"
var t = "test.class.element"
var funcion = Funciones()
var y = funcion.match_finder(x)
var c = funcion.match_finder(z)
var r = funcion.match_finder(t)
println(y)
println(c)
println(r)
}
You have some typos in your example. You query for test.class.tipo but in your lista_de_listas you have test_class.tipo with underline. The same is true for test.class.element.
But you should consider to use a Map instead of two list to the lookup:
fun match_finder(texto: String): Any{
val map = mapOf(
"test_class.self" to test_class.self,
"test_class.tipo" to test_class.tipo,
"test_class.element" to test_class.element
)
return map.getOrDefault(texto,"Not a match")
}

Access array element by string in AS3

As we know its possible to access field by name using indexer.
var obj:* = {name:"Object 1"};
trace(obj["name"]); // "Object 1"
But how to access an array element by String?
var arr:Array = new Array();
var obj:* = {items:arr};
trace(obj["items[0]"]); // Undefined
Ok, basically you want to be able to have a string be interpreted as actionscript. No elegant solution I'm afraid. You could write a parser that handles some simple syntax in a string and retrieves the value.
Here's a simple example:
var obj:Object = {
items:[1, 2, 3],
subObj: {
subitems: [4, 5, 6]
}
};
trace(getValueInObject(obj, "items[0]")); // 1
trace(getValueInObject(obj, "subObj.subitems[2]")); // 6
// takes an object and a "path", and returns the value stored at the specified path.
// Handles dot syntax and []
function getValueInObject(obj : Object, pathToValue : String) : * {
pathToValue = pathToValue.replace(/\[/g, ".").replace(/]/g, "");
var pathFractions : Array = pathToValue.split(".");
var currentObject : Object = obj;
while (pathFractions.length > 0 && currentObject != null) {
var fraction : String = pathFractions.shift();
currentObject = currentObject[fraction];
}
if (currentObject != null) {
return currentObject;
}
return null;
}

Unable to store and retrieve JSON value by JQUERY

I have two textbox(goalText and goalText1) and a button(goalreach) in my html.
My aim : When I enter numeric value in 1 textbox(goalText), it should be converted to json and be stored. So even after 5 days when I run the application, it should be stored. Now when I enter the numeric value, in other textbox(goalText1) and it matches, I am simply displaying the message match. This is the demo, I am trying so that I can know that value can be stored in json and can be retrieved when necessary. I have written the code as follow:
$("#goalreach").click(function () {
var contact = new Object();
contact.goalDist = "$("#goalText.value ").val()";
var jsonText = JSON.stringify(contact);
if (jsonText == ($("#goalText1.value").val())) {
document.getElementById('divid').innerHTML = 'Match';
}
});
I know, I have made many simple mistakes of brackets and " too, but I am a newbie, If you can help me out.
First, you have to compare either 2 objects or 2 strings, and in goalDist, you should store the value (BTW, you get the jQuery object with $("#goalText") and the value with somejQueryObject.val() moreover this is generally equivalent to document.getElementById("goalText").value)...
This can be done like this :
$("#goalreach").click(function () {
// Create an object with the single property "goalDist"
var contact = { goalDist : $("#goalText").val() };
// Makes it be a string (it will in this simple example : `"{"goalDist":<the value of goalTest>}"`
var jsonText = JSON.stringify(contact);
// Creates a string from an equivalent object bound on the second field
var jsonText2 = JSON.stringify({ goalDist : $("#goalText2").val() });
// Compares the 2 strings
if (jsonText === jsonText2) {
document.getElementById('divid').innerHTML = 'Match';
}
});
TRY THIS
$("#goalreach").click(function () {
var contact = new Object();
var goalDist = '$("#goalText.value").val()';
var jsonText = JSON.stringify(contact.goalDist);
if(jsonText==($("#goalText1.value").val()))
{
document.getElementById('divid').innerHTML = 'Match';
}
});
Try the following code:
$("#goalreach").click(function () {
var contact = new Object();
contact.goalDist = $("#goalText").val();
var jsonText = JSON.stringify(contact);
if (jsonText == ($("#goalText1").val())) {
document.getElementById('divid').innerHTML = 'Match';
}
});
OR
$("#goalreach").click(function () {
var goalText = $("#goalText").val();
var goalText1 = $("#goalText1").val();
if (goalText == goalText1) {
document.getElementById('divid').innerHTML = 'Match';
}
});

Reverse engineering - Flash app

I have that code:
private function handleFlashVarsXmlLoaded(event:Event) : void
{
var secondsplit:String = null;
var item:Array = null;
var string:* = XML(String(event.target.data));
var notsplited:* = string.vars_CDATA; //what is .vars_CDATA?
var splitted:* = notsplitted.split("&");
var datacontainer:Object = {};
var index:Number = 0;
item = secondsplit.split("=");
datacontainer[item[0]] = item[1];
this.parseFlashVars(datacontainer); // go next
return;
}
That function is loaded when URLLoader is loaded.
I think that this function parse a XML file to string(fe. param1=arg1&param2=arg2), then split it by "&" and then by "=" and add data to datacontainer by
datacontainer["param1"] = "arg1"
But how should the XML file look like and what is string.vars_CDATA
I think, vars_CDATA is just a name of XML field, becourse variable named "string" is contains whole XML. So var "notsplited" contains a String-typed data of this field (I think so, becourse of the line "var splitted:* = notsplitted.split("&");", which splits String to Array).

Scroll to alphabet in a List (ArrayCollection dataProvider) (Alphabet Jump)

Hopefully this is easy but that sometimes means its impossible in flex and I have searched quite a bit to no avail.
Say I have a list (LIST#1) of artists:
2Pac
Adele
Amerie
Beyonce
Jason Aldean
Shakira
The Trews
I also have a list (LIST#2) that has the values #,A-Z - how would I create an alphabet jump?
So If a user clicked on "A" in LIST#2 that would automatically scroll to "Adele" at the top of LIST#1 - not filter so he/she could scroll up to view 2Pac or down to view The Tews if they were not in the view yet.
Its a standard Flex Spark List with an ArrayCollection as the dataProvider - the artist field is called: "title" along with a unique id field that is not visible to the user.
Thanks!
Please see comments on marker answer for discussion on Dictionary that may be faster in some cases. See below for code (HAVE NOT CONFIRMED ITS FASTER! PLEASE TEST):
private function alphabet_listChange(evt:IndexChangeEvent) : void {
var letter:String;
letter = evt.currentTarget.selectedItems[0].toString();
trace(currentDictionary[letter]);
ui_lstLibraryList.ensureIndexIsVisible(currentDictionary[letter]);
}
public function createAlphabetJumpDictionary() : Dictionary {
//alphabetArray is a class level array containing, A-Z;
//alphabetDictionary is a class level dictionary that indexes A-z so alphabetDictionary["A"] = 0 and ["X"] = 25
var currentIndexDict:Dictionary = new Dictionary; //Dictionary is like an array - just indexed for quick searches - limited to key & element
var searchArray:Array = new Array;
searchArray = currentArrayCollection.source; //currentArrayCollection is the main array of objects that contains the titles.
var currentIndex:Number; //Current index of interation
var currentAlphabetIndex:Number = 0; //Current index of alphabet
for (currentIndex = 0; currentIndex < searchArray.length; currentIndex++) {
var titleFirstLetter:String = searchArray[currentIndex].title.toString().toUpperCase().charAt(0);
if (titleFirstLetter == alphabetArray[currentAlphabetIndex]) {
currentIndexDict[titleFirstLetter] = currentIndex;
trace(titleFirstLetter + " - " + currentIndex);
currentAlphabetIndex++;
} else if (alphabetDictionary[titleFirstLetter] > alphabetDictionary[alphabetArray[currentAlphabetIndex]]) {
trace(titleFirstLetter + " - " + currentIndex);
currentIndexDict[titleFirstLetter] = currentIndex;
currentAlphabetIndex = Number(alphabetDictionary[titleFirstLetter] + 1);
}
}
return currentIndexDict;
}
private function build_alphabeticalArray() : Array {
var alphabetList:String;
alphabetList = "A.B.C.D.E.F.G.H.I.J.K.L.M.N.O.P.Q.R.S.T.U.V.W.X.Y.Z";
alphabetArray = new Array;
alphabetArray = alphabetList.split(".");
return alphabetArray;
}
private function build_alphabetDictionary() : Dictionary {
var tmpAlphabetDictionary:Dictionary = new Dictionary;
for (var i:int=0; i < alphabetArray.length; i++) {
tmpAlphabetDictionary[alphabetArray[i]] = i;
trace(alphabetArray[i] + " - " + i);
}
return tmpAlphabetDictionary;
}
private function buildCurrentDictionary() : void {
trace("Collection Changed");
currentDictionary = new Dictionary;
currentDictionary = createAlphabetJumpDictionary();
}
The Flex Spark list has a very convenient method called ensureIndexIsVisible(index). Check the Flex reference documentation. All you have to do is to find the index of the first artist for the corresponding selected alphabet letter:
public function findAlphabetJumpIndex():Number
{
var jumpToIndex:Number;
var selectedLetter:String = alphabethList.selectedItem;
for (var i:int=0; i < artists.length; i++)
{
var artistName:String = artists.getItemAt(i);
var artistFirstLetter:String = artistName.toUpperCase().charAt(0);
if (artistFirstLetter == selectedLetter)
{
jumpToIndex = i;
break;
}
}
return jumpToIndex;
}
You can iterate your artist list data provider and check if artist name starts with selected alphabet from list two. When corresponding artist is found, set artist list selected index a value what you get from iterating data.