简体   繁体   中英

Grails richui autocomplete passing object to function or updating object ID

I've got a table with a load of auto complete boxes in it which look like so...

<richui:autoComplete style="width:500px" name="objSelect[${newRow-1}].id" value= "" action="${createLinkTo('dir': 'object/searchAJAX')}" forceSelection = "true" maxResultsDisplayed="20" minQueryLength ="3" onItemSelect="updateHiddenInput(id,${newRow-1})" />

I've got it to call a function called updateHiddenInput when a user selects a value passing in the id selected as well as the row the autocomplete is on (this function then updates a hidden field in the same row, using the values passed in, with the ID). The function looks like so: -

function updateHiddenInput(id, num){
    var objID = "objectID[" + num + "].id";
    $(document.getElementById(objID)).val(id);
}

Everything works until I add a new row within my table, this pushes everything down one row and stops the autocomplete from updating the right rows hidden field (as its still referencing the old row).

Currently I have another piece of code that goes through and renames all the fields when a new row is inserted, but I have no idea how to update the autocomplete so that it passes through the right row number, anyone know how I can alter this?

The only other alternative I could think of would be to just pass through the object itself as well as the ID I can then locate the hidden based off the object, but I can't work out how to do this, any suggestions gratefully received! :S

I've tried changing onItemSelect="updateHiddenInput(id,${newRow-1})" to onItemSelect="updateHiddenInput(id,this)"

Theoretically so I can just pass through the autocomplete object and from there just traverse the page to find the hidden field I want to update. However when I then attempt to use that object in my function, for example with something like: -

var mynumber = $(myobject).closest('td').find('input').val();

I always get an "undefined" returned when I try to alert back the value...

If I just put in an alert(myobject) in the function it returns AutoComplete instance0 autoLook[0].id but if I've inserted new lines the id value doesn't change (ie the objects id is now autoLook[3].id but it still shows [0], which I think could be part of the problem but I've got now idea how I can update this value...

I notice when looking in firebug at the html there is a /script linked to the autocomplete which could be the problem as this doesn't get updated when new lines are added and I can see multiple references to the old/original id value (see below) so maybe the passing through of this isn't passing the current objects values through...?

<script type="text/javascript">
var autoCompleteDataSource = new YAHOO.util.XHRDataSource("/Framework/object/searchAJAX");
autoCompleteDataSource.responseType = YAHOO.util.XHRDataSource.TYPE_XML;
autoCompleteDataSource.responseSchema = {
resultNode : "result",
fields : [
{ key: "name" },
{ key: "id" }
]
};
;
autoComplete = new YAHOO.widget.AutoComplete('autoLook[0].id','ad186a42e45d14d5cde8281514f877e42', autoCompleteDataSource);
autoComplete.queryDelay = 0;
autoComplete.prehighlightClassName = 'yui-ac-prehighlight';
autoComplete.useShadow = false;
autoComplete.minQueryLength = 3;
autoComplete.typeAhead = false;
autoComplete.forceSelection = true;
autoComplete.maxResultsDisplayed = 20;
autoComplete.shadow = false;
var itemSelectHandler = function(sType, args) {
var autoCompleteInstance = args[0];
var selectedItem = args[1];
var data = args[2];
var id = data[1];
updateHiddenInput(id,this) };
autoComplete.itemSelectEvent.subscribe(itemSelectHandler);
</script>

My thanks so far to user1690588 for all his help thus far! :)

On further digging I'm convinced that my issues is down to the line autoComplete = new YAHOO.widget.AutoComplete('autoLook[0].id','a5b57b386a2d1c283068b796834050186', autoCompleteDataSource); specifically the part where its inputting autoLook[].id and if I could change this I'd then be ok, but this line is auto generated and I've got no idea how to update it, anyone have any similar experience?

I have not much idea about your gsp page but I tried it on my side:

My gsp:

<!DOCTYPE html>
<html>
<head>
<resource:autoComplete skin="default"/>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript">
    var counter = ${list.size()};

    function asd() {
        jQuery.ajax({
            url: " ${createLink(controller: 'oauthCallBack', action: 'testAuto')}",
            data: "idx=" + counter++,
            success: function (data) {
                jQuery("#tableId").append("<tr><td>" + data + "</td></tr>");
            }
        });
    }

    function updateHiddenInput(id, tg) {
        jQuery(tg).val(id);
    }
</script>
</head>

<body>
<g:form>
<table id="tableId">
    <g:each in="${list}" var="vr" status="idx">
        <tr>
            <td>
                <richui:autoComplete name="name" id="uniqueId${idx}" action="${createLinkTo('dir': 'oauthCallBack/test')}" onItemSelect="updateHiddenInput(id, someId${idx})"/>
                <g:hiddenField name="someName" id="someId${idx}" value=""/>
            </td>
        </tr>
    </g:each>
</table>
</g:form>
<button onclick="asd()">Add</button>
</body>
</html>

My action:

def testAuto() {
    render template: 'addNew', model: [idx: params.idx]
}

My template(addNew):

<richui:autoComplete name="name" id="uniqueId${idx}" action="${createLinkTo('dir': 'oauthCallBack/test')}"
                 onItemSelect="updateHiddenInput(id, someId${idx})"/>
<g:hiddenField name="someName" id="someId${idx}" value=""/>

Try this..,.



EDIT.....................................................................................

I supposed that you have successfully updated all the input field names. Then you can edit hidden field like:

View:

<tr class="dummyClass">
    <td>
        <richui:autoComplete name="name[${idx}]" id="uniqueId[${idx}]" action="${createLinkTo('dir': 'oauthCallBack/test')}" onItemSelect="updateHiddenInput(id, this)"/>
        <g:hiddenField name="someName[${idx}]" id="someId[${idx}]" value=""/>
    </td>
</tr>

jQuery:

function updateHiddenInput(id, tg) {
    jQuery(tg._elTextbox).closest("tr.dummyClass").find("input[type=hidden]").val(id);
}


EDIT.....................................................................................

Why you need to change the 'id'? Changing name is sufficient to send values in order. And you can update the hidden field without id as above edit.

If you still need to change the id then you can change it by cloning the tr and then use regex. See this answer for full working example.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM