mrackley / StratusForms

Lightweight InfoPath alternative for SharePoint 2007,2010,2013,2016,2019 and Office 365
82 stars 36 forks source link

Yes/No Checkboxes do not work #11

Open candiceblash opened 6 years ago

candiceblash commented 6 years ago

If I try to change a checkbox when editing, it does not update in the StratusFormsData field.

warwickbuilds commented 6 years ago

what's the code of your checkbox look like?

btroop202 commented 6 years ago

I am also having a problem

In the form I have

Contacted

Then in the JS file I have

function SubmitForm() { if ($('#Contacted').is(':checked') == true){ $('#Contacted').val('Yes'); } else { $('#Contacted').val('No'); }

    $("#SFForm").StratusFormsSubmit({
        listName: listName_Var,
        completefunc: function(id) {
            alert("Save was successful. ID = " + id);
            // This reloads the form and the record data
            window.location = window.location.pathname + "?formID=" + id;
            //window.location = window.location.pathname;
        }
        });
}

It is setting to Yes and No, but in the SP list showing 1 or 0

Then on form open

$(document).ready(function() { $("#SFForm").StratusFormsInitialize({ htmlForm: "../New_Roster.html", queryStringVar: "formID", listName: listName_Var, completefunc: function() { if ($('#Contacted').val() == true){ $( "#Contacted" ).prop( "checked", true ); } else { $( "#Contacted" ).prop( "checked", false ); } } }); }); So if I alert the value of the check box I get 'on' no matter what.

Want to store Yes or No in the list, and then have the box check if 'Yes' or unchecked if 'No'

Any help would be great.

Thanks, Darrin

mellolr1 commented 6 years ago

When I have multiple radio inputs with the same name, the stratus data field stores the value of each input regardless if it is checked.

<input type="radio" id="deptleadYes" onchange="check(this.id)" name="deptleadYN" value="Yes" class="required">Yes <input type="radio" id="deptleadNo" onchange="check(this.id)" name="deptleadYN" value="No">No

First I saved the form with Yes checked and got "deptleadYes":"Yes" in the stratus field. Then I saved the form with No checked, got "deptleadYes":"Yes","deptleadNo":"No" in the stratus field.

I tried on 1.5 and 1.55a with the same results. Is something wrong with my html, how can I fix this?

davidcfk commented 6 years ago

Looking at the code, for the radio button it appears you might get away with keeping the ID the same name (or even just give the first radio button that ID and leave the second one without an ID) -- the name attribute will define the grouping... give it a try and see what you get?

mellolr1 commented 6 years ago

Thank you for the recommendation David, but after trying a few things the problem continues, Using the same ID for both radio inputs saves only the first value to the data field. Removing the ID from one or both causes the field to not get saved. I also tried using the "SFDontSave" class, removing it on-click if checked, but that didn't work either. I'll see if I can use something other than radio or check boxes, but I'm not so sure.

It seems that stratus isn't able to remove the unchecked field ID from it's data field. I found where the data field is created on save, but I don't see how it compares the data being saved with the data already in the field. Does that approach make sense to you?

davidcfk commented 6 years ago

Okaaaaayyyyy well after a bit of crazy testing, I came to the conclusion here!

There IS a change/hack that is required to stratus-forms-1.5.js (it's right at the bottom of PopulateFormData(...)), and you MUST use ListFieldName parameter (so you ultimately have a place to store the result) with this hack.

Replace the part in PopulateFormData() from this

if ($(element).attr("type") == "radio" || $(element).attr("type") == "checkbox") {
                    $(element).attr("checked", "checked");
                    if ($(element).attr("type") == "checkbox" && value == 0) {
                        $(element).removeAttr("checked");
    }
}

To this:

if ($(element).attr("type") == "radio" || $(element).attr("type") == "checkbox") {
    if ($(element).attr("type") == "radio" && $(element)[0].value == value) {
        $(element).attr("checked", "checked");  
    }
        if ($(element).attr("type") == "checkbox" && value == 0) {
            $(element).removeAttr("checked");
        }

Evidently this only corrects the radio button (the equivalent can be done for the checkboxes I guess...). Literally, the code originally already goes through each form element (for each unique id), the task is then to check through each element and match what value was presented in the Sharepoint Field (not StratusFormsData), and mark it as checked.

Then the rest of the explanation below (I guess this must be what Mark would have intended...)

Basically, each radio button should have its own ID. Evidently, your StratusFormsData will have multiple entries for each radioButton Option. Not to worry (or you can worry, if you aren't promoting this field to Sharepoint Fields), as it would be completely meaningless data if the user comes back to change the value for whatever reason :)

Now - ideally if you are promoting the field to a Sharepoint list field, you'll use the ListFieldName parameter against the radio button options...

    Yes <input type="radio" id="RadioOpt1" value="Yes" ListFieldName="Radio1" name="radio1" /> 
    No  <input type="radio" id="RadioOpt2" value="No" ListFieldName="Radio1" name="radio1" />

This achieves a few things: StratusFormsData will contain the following:

{"StratusFormsRepeatable":[],"RadioOpt1":"Yes","RadioOpt2":"No"}

And promoting the item to the Sharepoint LIst will result in the correct text value under the Radio Options.

The code will also select the correct element to mark as being checked. Yes, StratusFormsData will have a splatter of radio button options... but unless you have a program that's processing that, wouldn't worry too much :)

The other benefit about the library is all promoted sharepoint values WILL trump whatever that's in StratusFormsData --- so you'll find the data in StratusFormsData completely insignificant in the end if all fields are promoted...

I'll be frank though, you'd want to be mindful the field (if you are using a Choice field), will ideally need to match the options you have in your html, but then again, that's only if you have to use Sharepoint Forms in conjunction with the form you are creating for whatever reason: the docs did say you would be better off using a Text field for keeping the data on Sharepoint.

Hope this helps!

davidcfk commented 6 years ago

https://github.com/davidcfk/StratusForms/commit/4c7a1c91925a3b53a301dd29f63a6b196440e566

davidcfk commented 6 years ago

hey guys, this is beautiful, I don't even have to depend on Sharepoint Fields to save the radio boxes anymore (see previous comment), I linked to all the changes I need to make to make that happen... let me know how it goes!

mellolr1 commented 6 years ago

Nice! I ended up setting the radio inputs with class SPDontSave and using text fields with class radio and with display none to hold the checked values. I had to modify the StratusFormsValidate to iterate through the fields with class radio because of the display none. You solution is much cleaner!

saraa71a commented 5 years ago

David, thank you for clarifying the radio box, Do you have any idea on this problem for check boxes? I have multiple of check boxes and user is able to have multiple selections. But Stratus forms save 0 in my list, how can I pass the values of the selected checkboxes in to one ListFieldName?

Any modification in java or html?

` Paper Map

Digital Map (.pdf)

`

paparush commented 4 years ago

So, what do we do if we are using the StratusForms SharePoint Fx web part in SharePoint online? In repeating rows, the checkboxes do not render correctly on postback. The values get stored in the JSON, but aren't rendered correctly. The 1st checkbox is displayed as checked, the next two are displayed unchecked.

Apparently, it's storing the right values in the JSON , but it's not rendering back out: {"StratusFormsRepeatable":[{"ID":"repeatingRowNotes","chk":"1"},{"ID":"repeatingRowNotesStratusForms1","StratusFormsParent":"repeatingRowNotes","chk":"1"},{"ID":"repeatingRowNotesStratusForms2","StratusFormsParent":"repeatingRowNotes","chk":"1"}]}

Here's my table:

<table >
         <tr><td>check</td></tr>
        <tr id="repeatingRowNotes" data-StratusFormsRepeatable="Y" >
          <td>   <input class="form-check-input"  type="checkbox" id="chk" /></td>
         </tr>
         <tr>
              <td colspan="2"><a href="#" onclick="$().StratusFormsRepeat('repeatingRowNotes');">Add Note</a></td>
          </tr>
        </tbody>
     </table>

     <input type="button" class="btn btn-primary StratusForms-SubmitButton" value="Save"/>