how to get all selected items of datagrid

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
kamran
Posts: 4
Joined: Thu Oct 31, 2013 7:15 pm

how to get all selected items of datagrid

Post by kamran » Thu Oct 31, 2013 7:29 pm

i have a datagrid having a field and checkbox in a row, i need to get the all selected row

i am using following code

Code: Select all

repeat with theLineNo = 1 to the dgNumberOfLines of group "DataGrid 1"
   if the hilite of button theLineNo  of group "DataGrid 1"  is truethen
             put tData[theLineNo]["Unit"]  into theUnit
            put "Insert into Unitss(ID,Units,UUID) values('" & theID &"','" & theUnit &"','" & UID &"');" into tSQL
                revExecuteSQL tDatabaseID, tSQL
         end if
         
         end repeat
but it is giving me wrong items all the time.

Please help me and let me know how to get the right indexs

Thank in Advance

Klaus
Posts: 13829
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: how to get all selected items of datagrid

Post by Klaus » Fri Nov 01, 2013 1:00 pm

Hi kamran,

1. welcome to the forum :D

2. You picked the most complex Livecode object, the DataGrid, for a start 8)

Unfortunately it is almost impossible to access single objects in the nested, nested and nested groups of a datagrid!

Do you also have a VALUE (true or false) for the hilite of that button in the ARRAY for the datagrid -> the dgdata?
Do you access the DGDATA when the user checks that button?
I mean do you set the new value (true or false for the hilite) when the user clicks that button?

As you might have guessed it is HIGHLY recommended to do so, otherwise you cannot get all the neccessary values,
-> the hilite of these buttons in your case.


Best

Klaus

kamran
Posts: 4
Joined: Thu Oct 31, 2013 7:15 pm

Re: how to get all selected items of datagrid

Post by kamran » Fri Nov 01, 2013 3:37 pm

Thanks for the Response Klaus.

My inital code was:

Code: Select all

put the dgData of group "DataGrid 1" into tData 

put the dgNumberOfLines of group "DataGrid 1" into count
repeat with theLineNo = 1 to the dgNumberOfLines of group "DataGrid 1"
   if the hilite of button"Chk" of group "DataGrid 1"  is true then
             put tData[count]["Unit"]  into theUnit
            put "Insert into Unitss(ID,Units,UUID) values('" & theID &"','" & theUnit &"','" & UID &"');" into tSQL
                revExecuteSQL tDatabaseID, tSQL
         end if
         
         end repeat
In this Button"Chk" is checkbox in the datagrid.

I am defining you ["Unit"] below :-

Code: Select all

command fillDataGrid x
    switch x
     case 1
        put "Sub Module of Main Module 1" into field "Field_sub"
        put "Unit1  of Sub module1" into theDataA[1]["Unit"]
        put "Unit2  of Sub module1" into theDataA[2]["Unit"]
        put "Unit3  of Sub module1" into theDataA[3]["Unit"]
        set the dgData of group "DataGrid 1" to theDataA

        break
        case 2
           put "Sub Module of Main Module 2" into field "Field_sub"
          put "Unit1  of Sub module2" into theDataA[1]["Unit"]
        put "Unit2  of Sub module2" into theDataA[2]["Unit"]
        put "Unit3  of Sub module2" into theDataA[3]["Unit"]
        put "Unit4  of Sub module2" into theDataA[4]["Unit"]
        set the dgData of group "DataGrid 1" to theDataA

        break
         case 3
               put "Sub Module of Main Module 3" into field "Field_sub"
          put "Unit1  of Sub module3" into theDataA[1]["Unit"]
        put "Unit2  of Sub module3" into theDataA[2]["Unit"]
        put "Unit3  of Sub module3" into theDataA[3]["Unit"]
        put "Unit4  of Sub module3" into theDataA[4]["Unit"]
        set the dgData of group "DataGrid 1" to theDataA
        break
        case 4
                 put "Sub Module of Main Module 4" into field "Field_sub"
          put "Unit1  of Sub module4" into theDataA[1]["Unit"]
        put "Unit2  of Sub module4" into theDataA[2]["Unit"]
        put "Unit3  of Sub module4" into theDataA[3]["Unit"]
        put "Unit4  of Sub module4" into theDataA[4]["Unit"]
         put "Unit5  of Sub module4" into theDataA[5]["Unit"]
        set the dgData of group "DataGrid 1" to theDataA
        break
  end switch
end fillDataGrid

I am fully messed up with the datagrid..

what exactly I want to do is.I want to insert the data of particular row on the hilite of checkbox (button"Chk") of that row.


My row Template contains:- A field and a Checkbox only.where i want to insert the the data of field on the hilite of checkbox.
and field contains "Unit 1 of sub module","Unit2..."
etc etc which is coming from ["Unit"]


Please suggest me code.I am messed up.
Thanks in Advance.

Klaus
Posts: 13829
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: how to get all selected items of datagrid

Post by Klaus » Sun Nov 03, 2013 5:01 pm

Hi kamran,

question: Is your datagrid of type FORM? This is mandatory for this purpose!

And be warned, this is no beginners stuff!

Ok, here is what you will need, beware, this is out of my head :D
1. You also need to leave a "slot" for the values (TRUE or FALSE) of the checkbox in the DGData!
I guess the "initial" values of these checkboxes will be -> UNchecked
If that is the case then you do:

Code: Select all

command fillDataGrid x
    switch x
     case 1
        put "Sub Module of Main Module 1" into field "Field_sub"
        put "Unit1  of Sub module1" into theDataA[1]["Unit"]
        put "Unit2  of Sub module1" into theDataA[2]["Unit"]
        put "Unit3  of Sub module1" into theDataA[3]["Unit"]
        put FALSE into theDataA[1]["CheckBox_Value"]
        put FALSE into theDataA[2]["CheckBox_Value"]
        put FALSE into theDataA[3]["CheckBox_Value"]
        set the dgData of group "DataGrid 1" to theDataA
## Same for you other CASE conditions!
...
2. Add a script to the checkbox in your TEMPLATE group of the datagrid.
This will add the new value of the checkbox into the DGdata:

Code: Select all

on mouseup
  put the dgindex of me into tIndex
  put the dgDataOfIndex[tIndex] of grp "Datagrid 1" into tData

  ## Write the hilite to the DGData!
  put the hilite of me into tData["CheckBox_Value"]
  set the dgdataOfIndex[tIndex] of grp "Datagrid 1" to tData
end mouseup
3. You need to add some line to the "on fillindata" in the datagrid "row behavior":

Code: Select all

on fillindata tDataArray
  ## 1. Fill your field with text:
  set the text of fld "name of your field here" to tDataArray["Unit"]

  ## Set hilite of checkbox:
  set the hilite of btn "checkbox" of me to tDataArray["Checkbox_Value"]
end fillindata
After this has been done and working, you can loop through the DGData and check all keys-> ["Checkbox_Value"]:

Code: Select all

...
put the dgData of grp "Datagrid 1" into tData
repeat for each key tKey in tData
  if tData[tKey]["Checkbox_Value"] = TRUE then
    put tData[tKey]["Unit"]  into theUnit
    ## Update database now! :-)
Well, that's it basically :D


Best

Klaus

kamran
Posts: 4
Joined: Thu Oct 31, 2013 7:15 pm

Re: how to get all selected items of datagrid

Post by kamran » Tue Nov 05, 2013 9:31 am

Hi Klaus,

I am so thankful to you.I have solved my issue.

Just a little mistake was that..I had to write this-

Code: Select all

put the hilite of Button"Chk" of me into tData["CheckBox_Value"]
instead of this.in mouse up of row behaviour.

Code: Select all

put the hilite of me into tData["CheckBox_Value"]

Rest All code was perfect.I did it.Yes,I did it :D

Thanx Thanx Thanx so much !! yay!

Klaus
Posts: 13829
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: how to get all selected items of datagrid

Post by Klaus » Tue Nov 05, 2013 12:20 pm

Hi kamran,

glad I could help :D


Best

Klaus

WebiWan
Posts: 22
Joined: Fri Apr 20, 2018 2:21 am

Re: how to get all selected items of datagrid

Post by WebiWan » Fri Mar 01, 2019 2:24 am

Once again, Klaus to the rescue. Although my data source was much different from this example I was able to get it to work by making logical assumptions to restructure the code. Thanks so much!

Post Reply

Return to “Talking LiveCode”