1

Following is the code I written for showing the device details in a table. each row can be selected using a checkbox. My problem is I want to save all the device names in a string separated by comma when the savedevice button is pressed. What can I do for that?

<table>
    <thead>
        <tr>
            <g:sortableColumn property="dname" title="${message(code: 'devices.dname.label', default: 'Dname')}" />

            <g:sortableColumn property="owner" title="${message(code: 'devices.owner.label', default: 'Owner')}" />

            <g:sortableColumn property="serailNumber" title="${message(code: 'devices.serailNumber.label', default: 'Serail Number')}" />

            <g:sortableColumn property="supportingAndroidVersion" title="${message(code: 'devices.supportingAndroidVersion.label', default: 'Supporting Android Version')}" />

        </tr>
    </thead>
    <tbody>
        <g:each in="${Devices.list()}" status="i" var="devicesInstance">
            <tr class="${(i % 2) == 0 ? 'even' : 'odd'}">

                <td>${fieldValue(bean: devicesInstance, field: "dname")}</td>

                <td>${fieldValue(bean: devicesInstance, field: "owner")}</td>

                <td>${fieldValue(bean: devicesInstance, field: "serailNumber")}</td>

                <td>${fieldValue(bean: devicesInstance, field: "supportingAndroidVersion")}</td>
                <td>
                    <g:checkBox name="myCheckbox${i}" value="${false}" />
                </td>
            </tr>
        </g:each>
    </tbody>
</table>
<button class="btn btn-xs btn-info pull-right" type="button" onclick=getvalue()>Save Device</button>

**Javascript**

function getvalue(){
                   var p = document.getElementById('mytable').rows.length;
                  alert(p)//I am getting the row count
                   var checkboxes = document.getElementsByName('myCheckbox[]');
                                   var vals = "";
                   for (var i=0, n=p-1;i<n;i++) {

                     if (checkboxes[i].checked)
                     {
                     vals += ","+checkboxes[i].value;
                    alert(val)
                     }
                   }

                 if (vals) vals = vals.substring(1);

                   }
Ash
  • 43
  • 7
  • 1
    either you use javascript to send the concatinated values or you concatinate them in your controller after parsing them – john Smith Apr 04 '14 at 10:10
  • 1
    @johnSmith I am using javascript. but alert is printing nothing. where i went wrong? I edited my post. see my javascript. – Ash Apr 04 '14 at 10:21
  • alert(checkboxes.length) before the loop ? looks like its not the right selector query, that would explain why theres no alert at all – john Smith Apr 04 '14 at 13:13
  • as theres no wildcard selector in native js, you must do sth like : http://stackoverflow.com/questions/4275071/javascript-getelementbyid-wildcard – john Smith Apr 04 '14 at 13:15
  • or you assign ${Devices.size()} to a javascript variable "n" – john Smith Apr 04 '14 at 13:17
  • @johnSmith See my edit. But now also I am not getting the alert(vals). – Ash Apr 05 '14 at 05:23

0 Answers0